如何处理从另一个应用程序共享到我自己的 iOS 应用程序的文件?
How to handle a file shared from another app to my own iOS app?
我按照上一个问题 () 将功能集成到我的应用程序中 Open in...
。例如,我想使用 Files
应用程序 select 文档文件(例如 PDF)并在我自己的应用程序中打开。
但是,AppDelegate
中的以下代码不会被调用。请问这是不是因为Swift的版本不同? Apple 更改了源代码以针对 iOS 15
或更早版本执行此操作?如果我能在 Swift 5
、iOS 15
.
中听到如何实现这一点,我将不胜感激
func application(_ app: UIApplication,
open url: URL,
options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool{
do {
let data = try Data(contentsOf: url)
// Do something with the file
print ("File was shared!")
print (data)
} catch {
print("Unable to load data: \(error)")
}
return true
}
我已经按照其他应用可以尝试共享文件的方式设置了该应用。当尝试在我的应用程序中打开时,我的应用程序打开了,但上面的代码似乎没有被调用。
你要找的是 URL 方案。只有当您为该应用程序注册了 url 方案时,您才能从当前打开的应用程序打开另一个应用程序。如果该应用未提供 URL 方案来打开它,则您无法打开它。
检查以下示例:
https://developer.apple.com/documentation/xcode/defining-a-custom-url-scheme-for-your-app
https://medium.com/@MdNiks/custom-url-scheme-deep-link-fa3e701a6295
-
并且,无法启动任意应用程序,但可以启动注册了 URL 方案的本机应用程序。
看起来您只是部分遵循了 中的方法。在那种情况下,该人已经成功实施了自定义 URL 方案的 3 部分解决方案中的 2 部分,因此答案仅提供了第 3 部分。
正如https://developer.apple.com/documentation/xcode/defining-a-custom-url-scheme-for-your-app所说:
To support a custom URL scheme:
- Define the format for your app’s URLs.
- Register your scheme so that the system directs appropriate URLs to your app.
- Handle the URLs that your app receives.
他们的问题是:
My app is being displayed in the available applications to which I can send the file to. My question is what happens after? When I choose to send it to my app it then switches over to my app and from there, I don't know how to receive the file and read it to extract its content.
所以他们已经完成了第 1 步和第 2 步(因此,他们的应用程序在可用应用程序等中正确显示)。
就您而言,您似乎还没有完成第 1 步和第 2 步?
另一种可能性是(也来自 https://developer.apple.com/documentation/xcode/defining-a-custom-url-scheme-for-your-app)如果您正在使用场景,请注意您的应用程序还有其他入口点。
If your app has opted into Scenes, and your app is not running, the system delivers the URL to the scene(:willConnectTo:options:) delegate method after launch, and to scene(:openURLContexts:) when your app opens a URL while running or suspended in memory.
类似
func scene(_ scene: UIScene,
willConnectTo session: UISceneSession,
options connectionOptions: UIScene.ConnectionOptions) {
// Determine who sent the URL.
if let urlContext = connectionOptions.urlContexts.first {
let sendingAppID = urlContext.options.sourceApplication
let url = urlContext.url
print("source application = \(sendingAppID ?? "Unknown")")
print("url = \(url)")
// Process the URL similarly to the UIApplicationDelegate example.
}
/*
*
*/
}
并且正如 Apple 文档所说,对于应用程序在后台的情况,然后实现类似
func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>){
let url = URLContexts.first?.url
print("url = \(url)")
// Process the URL similarly to the UIApplicationDelegate example.
}
我按照上一个问题 (Open in...
。例如,我想使用 Files
应用程序 select 文档文件(例如 PDF)并在我自己的应用程序中打开。
但是,AppDelegate
中的以下代码不会被调用。请问这是不是因为Swift的版本不同? Apple 更改了源代码以针对 iOS 15
或更早版本执行此操作?如果我能在 Swift 5
、iOS 15
.
func application(_ app: UIApplication,
open url: URL,
options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool{
do {
let data = try Data(contentsOf: url)
// Do something with the file
print ("File was shared!")
print (data)
} catch {
print("Unable to load data: \(error)")
}
return true
}
我已经按照其他应用可以尝试共享文件的方式设置了该应用。当尝试在我的应用程序中打开时,我的应用程序打开了,但上面的代码似乎没有被调用。
你要找的是 URL 方案。只有当您为该应用程序注册了 url 方案时,您才能从当前打开的应用程序打开另一个应用程序。如果该应用未提供 URL 方案来打开它,则您无法打开它。
检查以下示例:
https://developer.apple.com/documentation/xcode/defining-a-custom-url-scheme-for-your-app
https://medium.com/@MdNiks/custom-url-scheme-deep-link-fa3e701a6295
并且,无法启动任意应用程序,但可以启动注册了 URL 方案的本机应用程序。
看起来您只是部分遵循了
正如https://developer.apple.com/documentation/xcode/defining-a-custom-url-scheme-for-your-app所说:
To support a custom URL scheme:
- Define the format for your app’s URLs.
- Register your scheme so that the system directs appropriate URLs to your app.
- Handle the URLs that your app receives.
他们的问题是:
My app is being displayed in the available applications to which I can send the file to. My question is what happens after? When I choose to send it to my app it then switches over to my app and from there, I don't know how to receive the file and read it to extract its content.
所以他们已经完成了第 1 步和第 2 步(因此,他们的应用程序在可用应用程序等中正确显示)。
就您而言,您似乎还没有完成第 1 步和第 2 步?
另一种可能性是(也来自 https://developer.apple.com/documentation/xcode/defining-a-custom-url-scheme-for-your-app)如果您正在使用场景,请注意您的应用程序还有其他入口点。
If your app has opted into Scenes, and your app is not running, the system delivers the URL to the scene(:willConnectTo:options:) delegate method after launch, and to scene(:openURLContexts:) when your app opens a URL while running or suspended in memory.
类似
func scene(_ scene: UIScene,
willConnectTo session: UISceneSession,
options connectionOptions: UIScene.ConnectionOptions) {
// Determine who sent the URL.
if let urlContext = connectionOptions.urlContexts.first {
let sendingAppID = urlContext.options.sourceApplication
let url = urlContext.url
print("source application = \(sendingAppID ?? "Unknown")")
print("url = \(url)")
// Process the URL similarly to the UIApplicationDelegate example.
}
/*
*
*/
}
并且正如 Apple 文档所说,对于应用程序在后台的情况,然后实现类似
func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>){
let url = URLContexts.first?.url
print("url = \(url)")
// Process the URL similarly to the UIApplicationDelegate example.
}