[英]iOS handle dynamic link while app is inactive
我的申請代表:
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
這個樣本:
當我的應用程序在后台運行時,它運行良好。 當我點擊動態鏈接時,打開應用程序並顯示帶有 url 的警報。
但如果我的應用程序處於非活動狀態(未運行),則它無法正常工作。 只需打開應用程序,什么都不做。
我的問題是:應用程序處於非活動狀態時如何處理動態鏈接?
對不起我的英語
斯威夫特 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
}
我有這樣做...
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool
{
if let option = launchOptions
{
if option.keys.contains(UIApplicationLaunchOptionsKey.userActivityDictionary)
{
if let userActivityDict = option[UIApplicationLaunchOptionsKey.userActivityDictionary] as? [AnyHashable:Any]
{
if userActivityDict.keys.contains(UIApplicationLaunchOptionsKey.userActivityType)
{
if let userActivity = userActivityDict["UIApplicationLaunchOptionsUserActivityKey"] as? NSUserActivity {
if let webpageURL = userActivity.webpageURL
{
// Write your code here.
}
}
}
}
}
}
}
當應用程序在后台時未調用application(_:open:options:)
時我遇到了問題。 Firebase SDK 調配方法,這就是原因。
我通過在Info.plist
設置FirebaseAppDelegateProxyEnabled = NO
解決了這個問題
有關其工作原理和影響的更多詳細信息,您可以在此處閱讀https://firebase.google.com/docs/cloud-messaging/ios/client#method_swizzling_in
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.