[英]Open fully quitted app from URL scheme did not call the function from postNotificationName in iOS
在這里,我為URL方案設置了代碼:
func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool {
if let userUrl = String(url) as String? {
print("\(userUrl)")
if (userUrl == "count://fromClipBoard") {
NSNotificationCenter.defaultCenter().postNotificationName("com.getContentFromClipBoard", object: self)
}
}
return false
}
並將以下代碼添加到我的ViewController
:
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "setContentFromClipBoard", name: "com.getContentFromClipBoard", object: nil)
//............
}
func setContentFromClipBoard() {
tv.text = "WAHAHA"
if let clipBoard = UIPasteboard.generalPasteboard().string {
tv.text = clipBoard
}
}
當我的應用程序沒有完全退出時, com.getContentFromClipBoard
調用很好,而tv.text
成為clipBoard
。
然而,當我完全退出應用程序,然后使用這個URL方案中, com.getContentFromClipBoard
不會被調用和tv.text
保持為空。
那么我該如何解決呢? 謝謝!
通過在didFinishLaunchingWithOptions
內部檢查UIApplicationLaunchOptionsURLKey
修復。
(延遲正在等待視圖加載)
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
if let userUrl = launchOptions?[UIApplicationLaunchOptionsURLKey] as? NSURL {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(2 * Double(NSEC_PER_SEC))), dispatch_get_main_queue(), {
if (userUrl.absoluteString == "count://fromClipBoard") {
NSNotificationCenter.defaultCenter().postNotificationName("com.getContentFromClipBoard", object: self)
}
})
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.