iOS 在应用程序处于非活动状态时处理动态链接
Posted
技术标签:
【中文标题】iOS 在应用程序处于非活动状态时处理动态链接【英文标题】:iOS handle dynamic link while app is inactive 【发布时间】:2017-02-07 04:19:18 【问题描述】:我的应用程序委托:
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 的警报。
但如果我的应用程序处于非活动状态(未运行),它就无法工作。只需打开应用程序,什么都不做。
还有我的问题:当应用处于非活动状态时如何处理动态链接? 对不起我的英语
【问题讨论】:
你成功了吗? 【参考方案1】:斯威夫特 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
【讨论】:
// do some stuff with dynamiclink
的代码是什么?
@TomSawyer 取决于您的需求。以我的方式:if let url = dynamiclink?.url, let contenttype = url["contenttype"], let contentid = url["contentid"] ...
我不使用 Firebase,但从 didFinishLaunchingWithOptions
调用 openURL 不起作用【参考方案2】:
我也有过这样的经历……
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.
【讨论】:
您拥有的 6 级 (!!) 条件检查可以用单级if
或 guard
表示。 17 行变成 5 行。【参考方案3】:
当应用程序处于后台时未调用 application(_:open:options:)
时,我遇到了问题。 Firebase SDK 调配方法,这就是原因。
我通过在Info.plist
中设置FirebaseAppDelegateProxyEnabled = NO
解决了这个问题
有关其工作原理及其影响的更多详细信息,您可以在此处阅读https://firebase.google.com/docs/cloud-messaging/ios/client#method_swizzling_in
【讨论】:
以上是关于iOS 在应用程序处于非活动状态时处理动态链接的主要内容,如果未能解决你的问题,请参考以下文章
如何在应用程序处于非活动状态时获取推送通知ios swift3
Ios:当应用程序处于非活动状态时,如何让我的应用程序返回根视图
iOS 7+ 应用程序处于非活动状态时是不是可以通过网络发送数据?
iOS 8 Beta 6 - 当应用程序处于后台时处理 UILocalNotification