Dmi*_*try 5 ios ios-universal-links ios13 uiscenedelegate uiscene
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)")
}
Run Code Online (Sandbox Code Playgroud)
我尝试使用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)
}
Run Code Online (Sandbox Code Playgroud)
我尝试使用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)
}
}
Run Code Online (Sandbox Code Playgroud)
我还尝试了以下方法:
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)
}
}
Run Code Online (Sandbox Code Playgroud)
但是scene.userActivity那里总是零,我无法得到userActivity.webpageURL.
我们如何识别链接被点击并启动了应用程序(不仅仅是打开)?
Jud*_*ngo 14
马特接受的答案适用于在应用程序尚未打开时启动通用链接。
如果您还想在应用程序打开时处理通用链接,则需要如下所示的两个函数:
// 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)'")
}
Run Code Online (Sandbox Code Playgroud)
我已经验证这适用于我的应用程序。有关更多详细信息,请参阅此 Github 线程。
mat*_*att 11
你几乎拥有它:
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
if let userActivity = scene.userActivity { // <-- not quite
self.scene(scene, continue: userActivity)
}
}
Run Code Online (Sandbox Code Playgroud)
它不在场景中;它在connectionOptions . 在connectionOptions.userActivities. (虽然如果发生的情况是用户单击了一个链接来启动我们,我希望能在connectionOptions.urlContexts.
苹果回应确认了 iOS 13 中的该问题。
这对我有用:
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
}
}
}
Run Code Online (Sandbox Code Playgroud)
基本上问题是通用链接“隐藏”在connectionOptions内,因此您必须使用循环来搜索它。
| 归档时间: |
|
| 查看次数: |
2559 次 |
| 最近记录: |