【问题标题】:Where to place app delegate code in App.swift file?在 App.swift 文件中的哪里放置应用程序委托代码?
【发布时间】:2020-09-24 00:44:25
【问题描述】:

我正在尝试让 Snapkit 与 SwiftUI 一起使用,以允许通过 SnapChat 登录。我正在关注这个 StackOverflow 问题 (Can I use the Snapchat SDK (SnapKit) with SwiftUI?),但我无法让公认的解决方案正常工作。作为答案发布的代码旨在放入应用程序委托文件中,但从最新版本的 XCode 开始,它们不再使用。相反,代码 sn-p 需要放在 AppName.swift 文件中,但我的断点不会触发。这是我的 App.swift 文件的当前版本:

import SwiftUI
import SCSDKLoginKit

@main
struct SampleApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

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

非常感谢任何帮助。谢谢!

编辑:感谢 Asperi,这是有效的解决方案!在此处更新代码以防有人遇到此问题:

import SwiftUI
import SCSDKLoginKit

@main
struct SampleApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
                .onOpenURL { url in
                    SCSDKLoginClient.application(UIApplication.shared, open: url)
                }
            }
        }
    }

【问题讨论】:

    标签: swift swiftui snapchat


    【解决方案1】:

    你应该改用.onOpenURL,比如

    @main
    struct SampleApp: App {
        var body: some Scene {
            WindowGroup {
                ContentView()
                  .onOpenURL { url in 
                      // .. do whatever needed here
                  }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2021-02-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多