猿问

不调用方法“ application:openURL:options:”

我正在尝试使用自定义方案从网页打开我的应用程序。该应用已打开,但未调用以下方法:


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

    // This is not called

}

我的info.plist样子如下:


    <key>CFBundleURLTypes</key>

    <array>

        <dict>

            <key>CFBundleURLSchemes</key>

            <array>

                <string>MyApp</string>

            </array>

            <key>CFBundleURLName</key>

            <string>url here</string>

        </dict>

    </array>

该项目是使用Xcode 11.1创建的,我正在iOS 13上进行测试。


海绵宝宝撒
浏览 3127回答 3
3回答

潇湘沐

scene(_:openURLContexts:)在您的场景委托中实施。如果该网址启动了您的应用,则您将获得该网址scene(_:willConnectTo:options:),它位于中options。

慕后森

使用最新的SDK,如果您不使用SceneDelegate,则可以正常工作。如果使用的是SceneDelegate,则不会调用以下AppDelegate方法,因此无法处理登录。func application(_ application: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {&nbsp; &nbsp; let handled = ApplicationDelegate.shared.application(&nbsp; &nbsp; &nbsp; &nbsp; application,&nbsp; &nbsp; &nbsp; &nbsp; open: url,&nbsp; &nbsp; &nbsp; &nbsp; sourceApplication: options[UIApplication.OpenURLOptionsKey.sourceApplication] as? String,&nbsp; &nbsp; &nbsp; &nbsp; annotation: options[UIApplication.OpenURLOptionsKey.annotation])&nbsp; &nbsp; return handled}这是因为(可以理解)将此方法推迟到SceneDelegate中的以下方法:func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {&nbsp; &nbsp; ...}我可以确认适用于实现SceneDelegate的iOS 13应用程序的解决方案是:func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {&nbsp; &nbsp; guard let url = URLContexts.first?.url else {&nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; &nbsp; }&nbsp; &nbsp; let _ = ApplicationDelegate.shared.application(&nbsp; &nbsp; &nbsp; &nbsp; UIApplication.shared,&nbsp; &nbsp; &nbsp; &nbsp; open: url,&nbsp; &nbsp; &nbsp; &nbsp; sourceApplication: nil,&nbsp; &nbsp; &nbsp; &nbsp; annotation: [UIApplication.OpenURLOptionsKey.annotation])&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;}
随时随地看视频慕课网APP

相关分类

iOS
我要回答