如何解决如何在Swift5.3中使用AppDelegate处理URL
我希望处理这样的网址,例如“ myApp:// oauth-callback / xxxx”
在 Swift 5.3 中,我们不再有“ AppDelegate ”文件,并且该文档现在已过时。 因此,我通过遵循其他文档来做到这一点,但是它不起作用 ...我的打印从未出现
有什么想法吗? (我是初学者)
谢谢
@main
struct MyApp: App {
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
class AppDelegate: NSObject,UIApplicationDelegate {
func application(_ app: UIApplication,open url: URL,options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
print("open via url")
if url.host == "oauth-callback" {
OAuthSwift.handle(url: url)
}
return true
}
}
解决方法
您需要在 scene(_:openURLContexts:)
中实现 SceneDelegate
方法。
func scene(_ scene: UIScene,openURLContexts URLContexts: Set<UIOpenURLContext>) {
if let url = URLContexts.first?.url else {
print(url)
if url.host == "oauth-callback" {
OAuthSwift.handle(url: url)
}
}
}
,
我在 ContentView
上使用了 onOpenURL@main
struct MyApp: App {
var body: some Scene {
WindowGroup {
ContentView()
.onOpenURL(perform: { url in
print("URL")
print(url)
})
}
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。