我想打开一个带有可操作通知按钮的 URL,我的代码在我的设备未锁定时工作,但是当设备被锁定并且我想单击操作按钮时,应用程序崩溃并出现此错误消息
NSLocalizedFailureReason=The request was denied by service delegate (SBMainWorkspace) for reason: Locked ("com.apple.mobilesafari cannot be launched because the device is locked")
我的代码如下所示:
func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void) {
let defaultsSettings = UserDefaults.standard
let customToken = defaultsSettings.value(forKey: "customToken")
let userInfo = response.notification.request.content.userInfo
// Perform the task associated with the action.
switch response.actionIdentifier {
case "open":
if (userInfo["type"] != nil){
if(userInfo["type"] as! String == "campfireOpened"){
let title = userInfo["title"] as! String
if let url = URL(string: "https://www.reloadium.com/news?search=\(title.replacingOccurrences(of: "#", with: "."))&customToken=\(customToken ?? "No Token")"){
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
break
}
else if (userInfo["type"] as! String == "conversationOpened"){
let title = userInfo["title"] as! String
if let url = URL(string: "https://www.reloadium.com/news?search=\(title.replacingOccurrences(of: "#", with: "."))&customToken=\(customToken ?? "No Token")"){
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
break
}
else if (userInfo["type"] as! String == "message"){
let conversationId = userInfo["conversationId"] as! String
if let url = URL(string: "https://www.reloadium.com/conversations?setActiveConversation=\(conversationId)&customToken=\(customToken ?? "No Token")"){
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
break
}
}
else {
print("Type is null")
print("User Info", userInfo)
break
}
case "cancel":
print("cancel button")
break
default:
break
}
print(userInfo)
// Always call the completion handler when done.
completionHandler()
}
}
当设备被锁定时,您是否有任何想法打开带有可操作通知的 URL?