方法在用户单击通用链接后启动应用程序时不会调用scene(_ scene: UIScene, continue userActivity: NSUserActivity)。
当已经启动的应用程序在用户单击通用链接后再次打开时,它工作得很好。示例代码:
func scene(_ scene: UIScene, continue userActivity: NSUserActivity) {
guard userActivity.activityType == NSUserActivityTypeBrowsingWeb,
let incomingURL = userActivity.webpageURL,
let components = NSURLComponents(url: incomingURL, resolvingAgainstBaseURL: true),
let path = components.path else {
return
}
let params = components.queryItems ?? [URLQueryItem]()
print("path = \(path)")
print("params = \(params)")
}我尝试使用application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration,但是当用户单击链接时,它就不会被调用:
func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
if let scene = connectingSceneSession.scene, let userActivity = scene.userActivity {
if userActivity.activityType == NSUserActivityTypeBrowsingWeb {
if let incomingURL = userActivity.webpageURL,
let components = NSURLComponents(url: incomingURL, resolvingAgainstBaseURL: true),
let path = components.path {
let params = components.queryItems ?? [URLQueryItem]()
print("path = \(path)")
print("params = \(params)")
}
}
}
return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
}我试着用scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions)
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
if let userActivity = scene.userActivity {
self.scene(scene, continue: userActivity)
}
}我还尝试了以下方法:
func sceneDidBecomeActive(_ scene: UIScene) {
if let userActivity = scene.userActivity {
self.scene(scene, continue: userActivity)
}
}
func sceneWillEnterForeground(_ scene: UIScene) {
if let userActivity = scene.userActivity {
self.scene(scene, continue: userActivity)
}
}但是scene.userActivity总是不存在,我不能得到userActivity.webpageURL。
我们如何才能识别出链接是被点击并启动的(而不仅仅是打开)?
发布于 2019-10-28 01:25:27
你差点就有了:
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
if let userActivity = scene.userActivity { // <-- not quite
self.scene(scene, continue: userActivity)
}
}它不在现场,它在connectionOptions里。看看connectionOptions.userActivities。(尽管所发生的事情是用户单击一个链接来启动我们,我还是希望在connectionOptions.urlContexts中找到这个URL。)
发布于 2021-12-30 19:54:06
马特接受的答案是,当应用程序尚未打开时,可以启动通用链接。
如果您还想在打开应用程序时处理通用链接,则需要两个函数,如下所示:
// SceneDelegate.swift
// This function is called when your app launches.
// Check to see if our app was launched with a universal link.
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
// See if our app is being launched via universal link.
for userActivity in connectionOptions.userActivities {
if let universalLink = userActivity.webpageURL {
// Do whatever you want with the universal link here.
// NOTE: if you're navigating a web view, know that the web view will not be initialized here yet.
// To navigate a web view, store the URL in a variable and navigate to it once the web view is initialized.
}
}
}
// SceneDelegate.swift
// This function is called when your app is already running and a universal link to your app is clicked.
func scene(_ scene: UIScene, continue userActivity: NSUserActivity) {
// Ensure we're trying to launch a link.
guard userActivity.activityType == NSUserActivityTypeBrowsingWeb,
let universalLink = userActivity.webpageURL else {
return
}
// Handle the universal link here.
// If you're navigating a web view, here's how I do it:
//MyApp.webView.evaluateJavaScript("location.href = '\(universalLink)'")
}我已经证实了这对我的应用程序是有效的。有关更多详细信息,请参阅这个Github线程。
发布于 2020-10-14 11:35:44
这对我起了作用:
func scene(_ scene: UIScene, willConnectTo _: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
for userActivity in connectionOptions.userActivities {
if let url = userActivity.webpageURL { //ADD WHATEVER CONDITION YOU NEED
//DO WHAT YOU NEED HERE
break
}
}
}基本上问题是,通用链接在connectionOptions中是“隐藏的”,所以您必须使用循环搜索它。
https://stackoverflow.com/questions/58243884
复制相似问题