application:openURL:options: not called

Jab*_*son -1 ios swift

I have created html with following body:

<html>
    <head>
        <meta http-equiv="Refresh" content="3; URL='MyApp://?Success'">
    </head>
    <body>
      success
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

When i try to open page where html uploaded. from my iphone (safari) my app successfully opened but

application:openURL:options:
Run Code Online (Sandbox Code Playgroud)

does not called. Have u any ideas?

my info plist look like this:

<array>
        <string>MyApp</string>
    </array>
    <key>CFBundleURLTypes</key>
    <array>
        <dict>
            <key>CFBundleURLSchemes</key>
            <array>
                <string>MyApp</string>
            </array>
            <key>CFBundleURLName</key>
            <string>url here</string>
        </dict>
    </array>
Run Code Online (Sandbox Code Playgroud)

My appDelegate Methods:

func application(_ app: UIApplication, open url: URL, options [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
    print("application openURL options is called")
}
Run Code Online (Sandbox Code Playgroud)

Project is created in xCode 11.1, testing on iOS 13

Jit*_*dra 23

这是 SceneDelegate.swift 中的方法。适用于iOS13 的Swift 版本

@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)
    }
}
Run Code Online (Sandbox Code Playgroud)


Unc*_*rks 20

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) {

    }
}
Run Code Online (Sandbox Code Playgroud)


Sag*_*tel 17

使用最新的 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
}
Run Code Online (Sandbox Code Playgroud)

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

func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
    ...
}
Run Code Online (Sandbox Code Playgroud)

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

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])        
}
Run Code Online (Sandbox Code Playgroud)


mat*_*att 5

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

如果该网址启动了您的应用,则会显示,scene(_:willConnectTo:options:)而您将获得它options


Pet*_*rks 5

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

\n\n

在 SceneDelegate.m 上

\n\n
- (void)场景:(UIScene *)场景 willConnectToSession:(UISceneSession *)会话选项:(UISceneConnectionOptions *)connectionOptions {\n\n NSURL *url = connectionOptions.URLContexts.allObjects.firstObject.URL;\n NSLog(@"当应用程序不在内存中时 ::::: %@",url);\n\n}\n\n\xe2\x80\x8b\n- (void)scene:(UIScene *)scene openURLContexts:(NSSet *)URLContexts {\n\n NSURL *url = URLContexts.allObjects.firstObject.URL; \n NSLog(@"当应用程序位于内存上时 ::::: %@",url);\n\n}
\n