方法'应用程序:打开URL:选项:'不被调用



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

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

在场景代理中实现scene(_:openURLContexts:)

如果URL启动你的应用程序,你会得到scene(_:willConnectTo:options:),它在options中。

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

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

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

iOS 14及以上版本
对于SwiftUI应用程序
注意:此方法处理通用链接的接收
public func onOpenURL(perform action: @escaping (URL) -> ()) -> some View

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

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

在SceneDelegate.m 上

-(void(场景:(UIScene*(场景将连接到会话:(UISereneSession*(会话选项:(UICeneConnectionOptions*(连接选项{NSURL*url=connectionOptions.URLContents.allObjects.firstObject.url;NSLog(@"当应用程序不在内存中时::::%@",url(;}​-(void(场景:(UIScene*(场景打开URLContext:(NSSet*(URLContext{NSURL*url=URLContents.allObjects.firstObject.url;NSLog(@"当应用程序在内存上时::::%@",url(;}

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

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

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

我遇到了一个问题,使用safari快捷方式启动应用程序,多次打开URLContext回调。

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

最新更新