在 App.swift 文件中放置应用程序委托代码的位置? [英] Where to place app delegate code in App.swift file?

查看:30
本文介绍了在 App.swift 文件中放置应用程序委托代码的位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to get Snapkit working with SwiftUI to allow logins via SnapChat. I'm following along with this StackOverflow question (Can I use the Snapchat SDK (SnapKit) with SwiftUI?) but I'm having trouble getting the accepted solution to work. The code posted as the answer was intended to go in the app delegate file but as of the latest version of XCode they are no longer used. Instead, the code snippet needs to be placed in the AppName.swift file but my breakpoint doesn't trigger. Here's my current version of my App.swift file:

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

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

Any help is greatly appreciated. Thanks!

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

Here's the solution that worked thanks to Asperi! Updated code here in case anyone runs into this:

import SwiftUI
import SCSDKLoginKit

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

推荐答案

你应该使用 .onOpenURL 代替,比如

You should use .onOpenURL instead, like

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

这篇关于在 App.swift 文件中放置应用程序委托代码的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆