首页 > 解决方案 > 登录 Snapchat 后,SnapKit 无法返回应用

问题描述

在 Snapchat 上进行身份验证后,我一直试图将 Snapchat 引导回我的应用程序,但没有成功。这是我的应用程序委托中的代码,我认为它不起作用:

import UIKit
import SCSDKLoginKit


@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {


    func application(_ app: UIApplication,
                     open url: URL,
                     options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
        return SCSDKLoginClient.application(app, open: url, options: options)
    }
  @available (iOS 13, *)
   func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
       for urlContext in URLContexts {
           let options: [UIApplication.OpenURLOptionsKey : Any] = [
               .openInPlace: urlContext.options.openInPlace,
               .sourceApplication: urlContext.options.sourceApplication!,
               .annotation: urlContext.options.annotation!
           ]
    SCSDKLoginClient.application(UIApplication.shared, open: urlContext.url, options: options)
       }
}

我在 SnapKitDevPortal 中设置了 URL 方案和重定向 URL。有人知道我做错了什么吗?提前致谢。

更新:找到解决方案 首先,如果使用 iOS 13,我必须在 SceneDelegate 中实现这段代码:

func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
     for urlContext in URLContexts {
         let url = urlContext.url
         var options: [UIApplication.OpenURLOptionsKey : Any] = [:]
         options[.openInPlace] = urlContext.options.openInPlace
         options[.sourceApplication] = urlContext.options.sourceApplication
         options[.annotation] = urlContext.options.annotation
         SCSDKLoginClient.application(UIApplication.shared, open: url, options: options)
     }
 }

然后,在拔掉头发几个小时后,我发现了我在设置 info.plist 时犯的一个简单错误。在 Snapchat 的文档中,在“CFBundleURLSchemes”下,它说这应该包含值“myapp”。我不明白这是指 URL Scheme,而不是应用程序的名称。因此,如果您的应用程序名称是“mycoolapp”并且您的 URL 方案是“myapp://snapauth”,那么您应该将“myapp”放在“CFLBundleURLSchemes”下,而不是“mycoolapp”下。希望这可以帮助。

标签: iosswiftios13

解决方案


更新:由于您已经有了 in 的实现openURLAppDelegate您只需从 调用它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])        
}

<array>您需要在of下的 info.plist 文件中添加您希望应用打开的 url 方案LSApplicationQueriesSchemes,如下所示:

<key>LSApplicationQueriesSchemes</key>
<array>
    <string>snapchat</string>
<array>

推荐阅读