【问题标题】:Method 'application:openURL:options:' is not called方法 'application:openURL:options:' 未被调用
【发布时间】:2020-02-25 17:13:36
【问题描述】:

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

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 上进行测试。

【问题讨论】:

  • 你在 iOS13 上吗?
  • 是的。有关系吗?
  • 如果您有 iOS 13 应用程序,则会调用 SceneDelegate 方法。
  • 是的,我愿意...我会检查并让您知道,请像答案一样发布...
  • 是的,看看thisQ&A

标签: ios swift ios13 custom-scheme-url


【解决方案1】:

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

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

【讨论】:

  • 我想知道为什么没有调用下面的方法。 application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
  • 你能回答这个问题吗? stackoverflow.com/questions/58973143/…
  • 这是给我的小费。谢谢!
  • 您能否详细说明如何在scene(_:willConnectTo:options:) 中实现它?谢谢。
  • @matt 我只是希望你能在这里提供一个代码 sn-p :)
【解决方案2】:

这里是 SceneDelegate.swift 中的方法。 Swift 版本适用于 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)
    }
}

【讨论】:

    【解决方案3】:

    使用最新的 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>) {
        ...
    }
    

    我可以确认适用于实现 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])        
    }
    

    【讨论】:

      【解决方案4】:

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

      在 SceneDelegate.m 上

      - (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions {
      
          NSURL *url = connectionOptions.URLContexts.allObjects.firstObject.URL;
          NSLog(@"当应用不在内存中时 ::::: %@",url);
      
      }
      
      ​​​
      - (void)scene:(UIScene *)scene openURLContexts:(NSSet *)URLContexts {
      
          NSURL *url = URLContexts.allObjects.firstObject.URL;
          NSLog(@"当应用在内存中时 ::::: %@",url);
      
      }

      【讨论】:

      【解决方案5】:

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

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

      func scene(_ scene: UIScene,
                 willConnectTo session: UISceneSession,
                 options connectionOptions: UIScene.ConnectionOptions) {
      
          if let userActivity = connectionOptions.userActivities.first {
              self.scene(scene, continue: userActivity)
          }
      }
      

      【讨论】:

        【解决方案6】:

        遇到问题,使用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>>
        )}
        

        【讨论】:

          【解决方案7】:

          iOS 14

          MyApp: App {
              @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
          
              var body: some Scene {
                  WindowGroup {
                      RootView()
                          .onOpenURL(perform: handleURL)
                  }
              }
          
              func handleURL(_ url: URL) {
          
              }
          }
          

          【讨论】:

            猜你喜欢
            • 2014-03-04
            • 2012-10-16
            • 2021-08-12
            • 2012-01-28
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多