方法 'application:openURL:options:' 未被调用

Method 'application:openURL:options:' is not called

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

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.

上进行测试

在您的场景委托中实施 scene(_:openURLContexts:)

如果 URL 启动您的应用程序,您将获得 scene(_:willConnectTo:options:),它位于 options

感谢@Matt,这就是我解决问题的方法。

在 SceneDelegate.m

- (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions {

    NSURL *url = connectionOptions.URLContexts.allObjects.firstObject.URL;
    NSLog(@"When app is not on memory  ::::: %@",url);

}

​
- (void)scene:(UIScene *)scene openURLContexts:(NSSet *)URLContexts {

    NSURL *url = URLContexts.allObjects.firstObject.URL;    
    NSLog(@"When app is on memory ::::: %@",url);

}

Here is the method in SceneDelegate.swift. Swift version For iOS13

@available(iOS 13.0, *)
func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
    if let url = URLContexts.first?.url {
        // Handle URL
        WXApi.handleOpen(url, delegate: AppDelegate.shared)
    }
}

使用最新的 SDK,如果您不使用 SceneDelegate,这也能正常工作。

如果您使用的是 sceneDelegate,则不会调用以下 AppDelegate 方法,因此无法处理登录。

func application(_ application: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
    let handled = ApplicationDelegate.shared.application(
        application,
        open: url,
        sourceApplication: options[UIApplication.OpenURLOptionsKey.sourceApplication] as? String,
        annotation: options[UIApplication.OpenURLOptionsKey.annotation])
    return handled
}

这是因为,此方法(可以理解)延迟到 SceneDelegate 中的以下方法:

func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
    ...
}

我可以确认适用于 iOS 13 个实现 SceneDelegate 的应用程序的解决方案是:

func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
    guard let url = URLContexts.first?.url else {
        return
    }
    let _ = ApplicationDelegate.shared.application(
        UIApplication.shared,
        open: url,
        sourceApplication: nil,
        annotation: [UIApplication.OpenURLOptionsKey.annotation])        
}

遇到问题,使用safari快捷方式启动应用,openURLContexts回调多次。

2019-12-09 18:33:41.257088+0800 OCTEST[5019:1468254] openURLContextsopenURLContexts {(
    <UIOpenURLContext: 0x2805a71c0; URL: appoctest://action=123123123; options: <UISceneOpenURLOptions: 0x280bd4750; sourceApp: (null); annotation: (null); openInPlace: NO>>
)}
2019-12-09 18:33:42.022132+0800 OCTEST[5019:1468254] openURLContextsopenURLContexts {(
    <UIOpenURLContext: 0x2805a6d40; URL: appoctest://action=123123123; options: <UISceneOpenURLOptions: 0x280bd6f70; sourceApp: (null); annotation: (null); openInPlace: NO>>
)}

我遇到了同样的问题,scene(_ scene:, continue userActivity:)scene(_ scene:, openURLContexts URLContexts:)都没有被调用。

当我检查传递给方法 scene(_ scene:, willConnectTo session, options connectionOptions:)connectionOptions 时,它有一个用户 activity 具有预期的 URL。所以,我最终手动调用了该方法:

func scene(_ scene: UIScene,
           willConnectTo session: UISceneSession,
           options connectionOptions: UIScene.ConnectionOptions) {

    if let userActivity = connectionOptions.userActivities.first {
        self.scene(scene, continue: userActivity)
    }
}

iOS 14岁及以上
对于 SwiftUI 应用
注意:此方法处理通用链接的接收
public func onOpenURL(perform action: @escaping (URL) -> ()) -> some View

@main
MyApp: App {
    @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate

    var body: some Scene {
        WindowGroup {
            RootView()
                .onOpenURL(perform: handleURL)
        }
    }

    func handleURL(_ url: URL) {

    }
}