Mat*_*att 9 xcode application-settings ios swift
如果用户可能意外拒绝接收通知并希望稍后再发送通知,我如何使用NSURL将IOS设置应用程序打开到我的应用程序的通知页面,在那里他们可以选择允许通知?
Gli*_*gor 44
我发现这个问题的答案(虽然很有帮助)有一些假设的逻辑。如果其他人偶然发现这个问题,这是一个简单明了的 Swift 5 实现:
if let bundleIdentifier = Bundle.main.bundleIdentifier, let appSettings = URL(string: UIApplication.openSettingsURLString + bundleIdentifier) {
    if UIApplication.shared.canOpenURL(appSettings) {
        UIApplication.shared.open(appSettings)
    }
}
Cod*_*ide 15
对于Swift 3,用于UIApplicationOpenSettingsURLString转到应用程序的设置,其中显示通知状态和"蜂窝数据"
let settingsButton = NSLocalizedString("Settings", comment: "")
let cancelButton = NSLocalizedString("Cancel", comment: "")
let message = NSLocalizedString("Your need to give a permission from notification settings.", comment: "")
let goToSettingsAlert = UIAlertController(title: "", message: message, preferredStyle: UIAlertControllerStyle.alert)
goToSettingsAlert.addAction(UIAlertAction(title: settingsButton, style: .destructive, handler: { (action: UIAlertAction) in
    DispatchQueue.main.async {
        guard let settingsUrl = URL(string: UIApplicationOpenSettingsURLString) else {
            return
        }
        if UIApplication.shared.canOpenURL(settingsUrl) {
            if #available(iOS 10.0, *) {
                UIApplication.shared.open(settingsUrl, completionHandler: { (success) in
                    print("Settings opened: \(success)") // Prints true
                })
            } else {
                UIApplication.shared.openURL(settingsUrl as URL)
            } 
        }
    }
}))
logoutUserAlert.addAction(UIAlertAction(title: cancelButton, style: .cancel, handler: nil))
Eri*_*uan 10
斯威夫特5:
if let url = URL(string: UIApplication.openSettingsURLString) {
    UIApplication.shared.open(url)
}
这应该可以让您覆盖所有 iOS 版本,包括 iOS 15 和 iOS 16。
extension UIApplication {
  private static let notificationSettingsURLString: String? = {
    if #available(iOS 16, *) {
      return UIApplication.openNotificationSettingsURLString
    }
    if #available(iOS 15.4, *) {
      return UIApplicationOpenNotificationSettingsURLString
    }
    if #available(iOS 8.0, *) {
      // just opens settings
      return UIApplication.openSettingsURLString
    }
    // lol bruh
    return nil
  }()
  private static let appNotificationSettingsURL = URL(
    string: notificationSettingsURLString ?? ""
  )
  func openAppNotificationSettings() -> Bool {
    guard
      let url = UIApplication.appNotificationSettingsURL,
      self.canOpen(url) else { return false }
    return self.open(url)
  }
}
用法:
Button {
  let opened = UIApplication.shared.openAppNotificationSettings()
  if !opened {
    print("lol fail")
  }
} label: {
  Text("Notifications")
}
从 iOS 15.4 开始,我们可以直接使用深度链接到 notif 设置屏幕UIApplicationOpenNotificationSettingsURLString:
| 归档时间: | 
 | 
| 查看次数: | 8986 次 | 
| 最近记录: |