简体   繁体   中英

Handling files opened from outside sources in iOS/Swift

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.

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.

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 这将使我的应用程序可以打开文件.myapp

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM