从 URL 方案中打开完全退出的应用程序未从 iOS 中的 postNotificationName 调用函数

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 保持为空。

那我该如何解决呢?谢谢!

已通过检查 UIApplicationLaunchOptionsURLKey 也在 didFinishLaunchingWithOptions 内修复。

(延迟是等待视图加载)

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)
            }
        })
    }
}