【问题标题】:com.apple.mobilesafari cannot be launched because the device is lockedcom.apple.mobilesafari 无法启动,因为设备已锁定
【发布时间】:2021-03-16 20:09:57
【问题描述】:

我想打开一个带有可操作通知按钮的 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?

【问题讨论】:

    标签: ios swift swiftui appdelegate actionable-notification


    【解决方案1】:

    要打开带有可操作通知按钮的 URL,当设备被锁定时,您需要在声明 UNNotificationAction 时设置 .foreground 选项,如下所示:

       let open = UNNotificationAction(
            identifier: "open",
            title: "Open",
            options: .foreground
        )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-04-10
      • 2014-07-30
      • 2018-05-06
      • 2013-09-09
      • 1970-01-01
      • 1970-01-01
      • 2015-11-29
      • 1970-01-01
      相关资源
      最近更新 更多