使用swift3在xcode8上运行
以下是来自AppDelegate.swift的代码
from time import sleep
f = open('testfile.txt', 'w', buffering=1)
while True:
f.write("A, B, C, D, E\n")
sleep(1)
当我收到通知并点击它时,我的调试区域将如下所示
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
print("==== didReceiveRemoteNotification ====")
print(userInfo)
}
我的问题是我如何使用userInfo从AnyHashable(“url”)获取数据,这是“www.google.com”?
我试试
==== didReceiveRemoteNotification ====
[AnyHashable("type"): order, AnyHashable("aps"): {
alert = "[TestMessage]";
badge = "<null>";
category = alert;
sound = default;
}, AnyHashable("url"): http://www.google.com]
但调试区域的输出是
print(userInfo[AnyHashable("url")])
答案 0 :(得分:3)
尝试print(userInfo[AnyHashable("url")] as? String ?? "")
或者您可以将其转换为URL
答案 1 :(得分:3)
if let url = userInfo["url"] as? String {
//do whatever you need with the url
}
答案 2 :(得分:2)
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
if let aps = userInfo["aps"] as? NSDictionary {
print(aps)
let alert = (aps.object(forKey: "alert") as? String)!
}
let url = userInfo?["url"] as? String {
print(url)
}
}