2015-04-01 2 views
3

Я создал файл, и я хочу поделиться им с помощью UIDOcumentInteractionControllerUIDocumentInteractionController() быстрое

Я не уверен в том, как получить URL с пути documentsPath и назначения, где я сохранил мой файл

 let someText = NSString(string: "Test") 
     let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as String 

     let destinationPath = documentsPath.stringByAppendingPathComponent("Data.txt") 
     var error:NSError? 

     let written = someText.writeToFile(destinationPath, 
      atomically: true, 
      encoding: NSUTF8StringEncoding, 
      error: &error) 

     if written{ 
      println("Successfully stored the file at path \(destinationPath)") 

    let dic = UIDocumentInteractionController() 

    self.dic.URL = url 
      let v = sender as UIView 
      let ok = self.dic.presentOpenInMenuFromRect(
       v.bounds, inView: v, animated: true) 

ответ

7

Перейдите к следующему

import UIKit 

class ViewController: UIViewController { 
    var docController:UIDocumentInteractionController! 

    override func viewDidLoad() { 
     let someText = NSString(string: "Test") 
     if let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as? String { 

     let destinationPath = documentsPath.stringByAppendingPathComponent("Data.txt") 
     var error:NSError? 

     let written = someText.writeToFile(destinationPath, 
      atomically: true, 
      encoding: NSUTF8StringEncoding, 
      error: &error) 

     if written{ 
      println("Successfully stored the file at path \(destinationPath)") 
      } 
     if let url = NSURL(fileURLWithPath: destinationPath) { 
      docController = UIDocumentInteractionController(URL: url) 
     } 
     } 
    } 

    @IBAction func sendFile(sender:AnyObject) { 
    docController.presentOptionsMenuFromRect(sender.frame, inView:self.view, animated:true) 
    } 

} 

Теперь подключите IBAction к кнопке в раскадровке. Далее:

  1. нажмите на синий значок проекта в левой боковой панели, а затем выберите Info из горизонтального меню.
  2. расширить Типы документов и введите «.txt» в Название поля и «public.data, public.content» в Типы поле
  3. Теперь расширьте экспортированной ИМП и введите «.txt» в Описание поле, "kUTTypeText" в идентификатора поля и "public.data, public.content" в соответствует полю

Так что все выглядит следующим образом:

enter image description here

Теперь вы можете построить и запустить приложение для проверки.

Смежные вопросы