Handling files opened from outside sources in iOS/Swift

半城伤御伤魂 提交于 2019-12-10 11:38:49

问题


I am having trouble with the basic handling of files opened from outside sources in iOS using Swift. I am attempting to export/import data from a custom file type (a simple text file with a custom extension) via email. I am having no problem with exporting the file and sending as an attachment from within the app. I have also been able to associate the filetype with my application by editing the info.plist file. However, I have no idea how/where to implement the function to handle the file once I have chosen to open it with my app.

After doing some searching I have found this tutorial: https://www.raywenderlich.com/1980/email-tutorial-for-ios-how-to-import-and-export-app-data-via-email-in-your-ios-app

However, all of the instructions on file handling are presented in Objective C.

Any help with this would be greatly appreciated.


回答1:


The only part that matters is this part:

// Add at end of application:didFinishLaunchingWithOptions
NSURL *url = (NSURL *)[launchOptions valueForKey:UIApplicationLaunchOptionsURLKey];
if (url != nil && [url isFileURL]) {
        [rootController handleOpenURL:url];                
} 

// Add new method
-(BOOL) application:(UIApplication *)application handleOpenURL:(NSURL *)url {

    RootViewController *rootController = (RootViewController *) [navigationController.viewControllers objectAtIndex:0];
    if (url != nil && [url isFileURL]) {
        [rootController handleOpenURL:url];                
    }     
    return YES;

}

The first code block is added to your AppDelegate's application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?)

The Swift equivalent is

if let options = launchOptions, let url = options[.url] as? URL, url.isFileURL {
    // call some code to handle the URL
}

and this new function for the AppDelegate:

func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
    if url.isFileURL {
        // call some code to handle the URL
    }
    return true // if successful
}

All of the rest of the code in the article is a way to route the handling code to the root view controller. You could just handle it right in the AppDelegate or route it to another class if you wish.




回答2:


In my case I want to open a json file in my app. I Did this code:

App Delegate:

 func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool{

    do {
        let data = try Data(contentsOf: url, options: .mappedIfSafe)
        let jsonResult = try JSONSerialization.jsonObject(with: data, options: .mutableLeaves)
        if let jsonResult = jsonResult as? Dictionary<String, AnyObject> {
            let obj = MyObject(json: jsonResult)
            save(obj) //Save is a function that will save my object to the data base
        }
    } catch{
       return false
    }

    return true
 }

Don't forget to enable your "custom file type" in the project settings.

This will enable my app to open files .myapp



来源:https://stackoverflow.com/questions/44285506/handling-files-opened-from-outside-sources-in-ios-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!