我在我的应用程序中有一个深度链接功能,在一个案例旁边工作得很好。我有3个不同的入职页面根据网址打开的应用程序。因此,当应用程序启动时,我需要知道是什么链接(如果有)打开了应用程序,然后显示正确的入职页面。问题是,我需要知道在该方法中显示什么屏幕:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
但我只能知道深度链接是否打开了应用程序
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation
它在did didFinishLaunchingWithOptions
被调用5秒后被调用(我数了秒数)。因此,在调用openURL
之前,我有5秒钟的时间看到错误的入网页面(如果将被调用)。
所以我的问题是:有没有办法知道应用程序是在didFinishLaunchingWithOptions
之前还是期间通过url启动的?
顺便说一句,当应用程序从深层链接打开时,didFinishLaunchingWithOptions
中的launchOptions
为空
发布于 2017-02-20 23:15:56
您正在寻找的启动选项密钥是UIApplicationLaunchOptionsURLKey
(Objective-C) / UIApplicationLaunchOptionsKey.url
(Swift)。
如果您的目标是iOS 9和更高版本,则只需从
application:didFinishLaunchingWithOptions:
(如果应用程序还没有在内存中) application:openURL:options:
(如果应用程序已经在后台)。这里是一个最简单的UIApplicationDelegate
实现,应该涵盖这两种情况-请注意,为了清楚起见,许多不相关的逻辑被省略了:
Objective-C:
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSURL *url = launchOptions[UIApplicationLaunchOptionsURLKey];
if (url) {
// TODO: handle URL from here
}
return YES;
}
- (BOOL)application:(UIApplication *)app
openURL:(NSURL *)url
options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options {
// TODO: handle URL from here
return YES;
}
@end
Swift 5:
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
if let url = launchOptions?[.url] as? URL {
// TODO: handle URL from here
}
return true
}
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey: Any] = [:]) -> Bool {
// TODO: handle URL from here
return true
}
}
发布于 2020-01-21 01:07:37
我在iOS 13中也遇到了类似的问题,但iOS 13中的情况发生了变化,因为UIWindowSceneDelegate已经引入,现在可能会做一些以前由UIApplicationDelegate
完成的工作(取决于你的应用程序设置)。
@Olivier在这个帖子中的answer对我来说仍然非常有用,因为它指出了处理URL方案的两种情况;即当应用程序还没有进入内存时,调用application:didFinishLaunchingWithOptions:
,以及当应用程序已经加载并在后台时,第二种情况调用application:openURL:options:
。
所以,正如我在上面提到的,如果你使用的是由XCode 11生成的默认应用程序模板,那么从iOS 13开始,情况会有一些不同。我不会在这里详细介绍,所以这里有一个关于这个主题的有用的教程:Understanding the iOS 13 Scene Delegate。
但是,如果将新方法用于场景,则需要修改的关键方法是scene(_:willConnectTo:options:)
(文档here)和scene(_:openURLContexts:)
(文档here)。前者是在应用程序尚未加载时执行URL方案的地方(所以它在某种程度上取代了application:didFinishLaunchingWithOptions:
),而后者是在调用URL方案时应用程序已经在后台时获取URL的地方(所以这个取代了application:openURL:options:
)。
使用scene(_:willConnectTo:options:)
,您可以查找URL方案的URL (如果有的话),执行以下操作:
if let url = connectionOptions.urlContexts.first?.url {
// handle
}
对于scene(_:openURLContexts:)
,您可以查看URLContexts
集的内部。
我希望这能帮到你!
发布于 2020-01-30 15:59:49
上一篇中的好主意。我做了一些测试。
我确认了iOS 13的缺陷和缺陷。
前言:我在plist上启用了所有标志:(来自https://forums.developer.apple.com/thread/118932)
..。UIFileSharingEnabled LSSupportsOpeningDocumentsInPlace UISupportsDocumentBrowser ..并在plist中添加了所有类型:
<key>CFBundleDocumentTypes</key>
<array>
<dict>
<key>CFBundleTypeIconFiles</key>
<array/>
<key>CFBundleTypeName</key>
<string>abc File</string>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>LSHandlerRank</key>
<string>Owner</string>
<key>LSItemContentTypes</key>
<array>
<string>org.example.app.document.abc</string>
</array>
</dict>
</array>
<key>UTExportedTypeDeclarations</key>
<array>
<dict>
<key>UTTypeConformsTo</key>
<array>
<string>public.data</string>
</array>
<key>UTTypeDescription</key>
<string>abc File</string>
<key>UTTypeIconFiles</key>
<array/>
<key>UTTypeIdentifier</key>
<string>org.example.app.document.abc</string>
<key>UTTypeTagSpecification</key>
<dict>
<key>public.filename-extension</key>
<array>
<string>abc</string>
</array>
</dict>
</dict>
</array>
我在这里登录:
1)
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
print(documentsDir())
if let url = launchOptions?[.url] as? URL {
// TODO: handle URL from here
openWriteAndCloseLog(msg: "1 " + url.absoluteString, withTimestamp: true)
}
return true
}
2)
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey: Any] = [:]) -> Bool {
// TODO: handle URL from here
openWriteAndCloseLog(msg: "2 " + url.absoluteString, withTimestamp: true)
return true
}
3)
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
if let url = connectionOptions.urlContexts.first?.url {
// handle
openWriteAndCloseLog(msg: "3 " + url.absoluteString, withTimestamp: true)
}
guard let _ = (scene as? UIWindowScene) else { return }
}
似乎我们只通过了3次(根据我的调试日志,我可以看到内部文档,因为我通过iTunes分享)
我做了一个小的演示程序来测试它。
https://github.com/ingconti/DocumentBroswerSampleApp
您可以从打开附件(例如..)您将看到:
https://stackoverflow.com/questions/42345586
复制相似问题