在didFinishLaunchingWithOptions之后几秒钟调用应用程序openURL

时间:2017-02-20 13:16:34

标签: ios deep-linking uiapplicationdelegate

我的应用中有一个深层链接功能,可以在一个案例旁边正常工作。 根据打开应用程序的网址,我有3个不同的入门页面。 因此,当应用程序启动时,我需要知道打开应用程序的链接(如果有的话),然后显示正确的入门页面。问题是我需要知道方法中出现的屏幕:

let statusBar: UIView = UIApplication.shared.value(forKey: "statusBar") as! UIView
if statusBar.responds(to:#selector(setter: UIView.backgroundColor)) {
    statusBar.backgroundColor = UIColor.red
}
UIApplication.shared.statusBarStyle = .lightContent

但我只能知道深层链接是否在

中打开了应用
backgroundColor
调用statusBarStyle后5秒钟调用

(我计算秒数)。所以我有5秒钟,我看到一个错误的入门页面,直到- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 被调用(如果它将被调用)。

所以我的问题是:有没有办法知道应用是在- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation 之前或期间从网址启动的?

当应用从深层链接打开时,didFinishLaunchingWithOptions中的openURL为{0}

2 个答案:

答案 0 :(得分:2)

您正在寻找的启动选项键是 UIApplicationLaunchOptionsURLKey (Objective-C)/ UIApplicationLaunchOptionsKey.url (Swift)。
如果您要定位iOS 9及更高版本,则只需截取

中的启动网址即可
  • application:didFinishLaunchingWithOptions:(如果应用程序尚未在内存中)
  • application:openURL:options:(如果应用已在后台)。

这里是UIApplicationDelegate的简约实现,应涵盖这两种情况 - 请注意,为清晰起见,省略了许多不相关的逻辑:

目标-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

斯威夫特3:

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

        if let url = launchOptions?[UIApplicationLaunchOptionsKey.url] as? URL {
            // TODO: handle URL from here
        }

        return true
    }

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

        // TODO: handle URL from here

        return true
    }
}

答案 1 :(得分:0)

以前的好主意。 我做了一些测试。.

我确认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

您可以打开附件(例如,..) 您将看到:

enter image description here

相关问题