处理从iOS / Swift中的外部源打开的文件 [英] Handling files opened from outside sources in iOS/Swift

查看:702
本文介绍了处理从iOS / Swift中的外部源打开的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用Swift从iOS外部源打开的文件的基本处理方面遇到了问题。我试图通过电子邮件从自定义文件类型(带有自定义扩展名的简单文本文件)导出/导入数据。我在导出文件并从应用程序中作为附件发送时没有问题。我也可以通过编辑info.plist文件将filetype与我的应用程序相关联。但是,一旦我选择使用我的应用程序打开它,我不知道如何/在哪里实现处理文件的函数。

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.

在做了一些搜索后,我找到了这个教程:
https://www.raywenderlich.com/1980/email -tutorial-for-ios-how-to-import-and-export-app-data-via-email-in-your-ios-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

但是,关于文件处理的所有说明都在目标C中提供。

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;

}

第一个代码块被添加到AppDelegate的 application(_ application:UIApplication,didFinishLaunchingWithOptions launchOptions:[UIApplicationLaunchOptionsKey:Any]?)

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

Swift等价物是

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

这个AppDelegate的新功能:

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
}

本文中的所有其余代码都是将处理代码路由到根视图控制器的一种方法。您可以直接在AppDelegate中处理它,或者如果您愿意,可以将其路由到另一个类。

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.

这篇关于处理从iOS / Swift中的外部源打开的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆