gpt4 book ai didi

iOS 在应用程序处于非事件状态时处理动态链接

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:15:05 25 4
gpt4 key购买 nike

我的应用委托(delegate):

let customURLScheme = "dlscheme"

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

FIROptions.default().deepLinkURLScheme = self.customURLScheme
FIRApp.configure()

return true
}

func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any]) -> Bool {
return application(app, open: url, sourceApplication: nil, annotation: [:])
}

func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
let dynamicLink = FIRDynamicLinks.dynamicLinks()?.dynamicLink(fromCustomSchemeURL: url)
if let dynamicLink = dynamicLink {
let message = generateDynamicLinkMessage(dynamicLink)
if #available(iOS 8.0, *) {
showDeepLinkAlertView(withMessage: message)
}
return true
}
if #available(iOS 8.0, *) {
showDeepLinkAlertView(withMessage: "openURL:\n\(url)")
} else {
}
return false
}
@available(iOS 8.0, *)
func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([Any]?) -> Void) -> Bool {
guard let dynamicLinks = FIRDynamicLinks.dynamicLinks() else {
return false
}
let handled = dynamicLinks.handleUniversalLink(userActivity.webpageURL!) { (dynamiclink, error) in
let message = self.generateDynamicLinkMessage(dynamiclink!)
self.showDeepLinkAlertView(withMessage: message)
}
return handled
}

func generateDynamicLinkMessage(_ dynamicLink: FIRDynamicLink) -> String {
let matchConfidence: String
if dynamicLink.matchConfidence == .weak {
matchConfidence = "Weak"
} else {
matchConfidence = "Strong"
}
let message = "App URL: \(dynamicLink.url)\nMatch Confidence: \(matchConfidence)\n"
return message
}

@available(iOS 8.0, *)
func showDeepLinkAlertView(withMessage message: String) {
let okAction = UIAlertAction.init(title: "OK", style: .default) { (action) -> Void in
print("OK")
}

let alertController = UIAlertController.init(title: "Deep-link Data", message: message, preferredStyle: .alert)
alertController.addAction(okAction)
self.window?.rootViewController?.present(alertController, animated: true, completion: nil)
}

这是我的代码。我正在使用 firebase 动态链接,并像本教程一样实现它:

https://firebase.google.com/docs/dynamic-links/ios

还有这个示例:

https://github.com/firebase/quickstart-ios/blob/master/dynamiclinks/DynamicLinksExampleSwift/AppDelegate.swift

当我的应用程序处于后台时,它运行良好。打开应用程序并在我点击动态链接时显示带有 url 的警报。

但是如果我的应用程序处于非事件状态(未运行),它就无法运行。只需打开应用程序,什么都不做。

我的问题是:应用处于非事件状态时如何处理动态链接?
对不起我的英语

最佳答案

swift 5

你可以这样处理:

AppDelegate.swift

import Firebase
import FirebaseDynamicLinks

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

// ... your other code here

FirebaseApp.configure()

let activityKey = NSString(string: "UIApplicationLaunchOptionsUserActivityKey")
if let userActivityDict = launchOptions?[.userActivityDictionary] as? [NSObject : AnyObject], let userActivity = userActivityDict[activityKey] as? NSUserActivity, let webPageUrl = userActivity.webpageURL {

DynamicLinks.dynamicLinks().handleUniversalLink(webPageUrl) { (dynamiclink, error) in

// do some stuff with dynamiclink

}

}

return true
}

关于iOS 在应用程序处于非事件状态时处理动态链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42081645/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com