swift 在设置应用中打开应用的通知设置

nkoocmlb  于 2022-10-31  发布在  Swift
关注(0)|答案(6)|浏览(357)

如果用户可能意外拒绝接收通知并希望稍后打开通知,如何使用NSURL打开IOS设置应用程序并转到我的应用程序的通知页面,在该页面中他们可以选择允许通知?

ar7v8xwq

ar7v8xwq1#

2021年12月8日更新:

此方法将打开“设置”〉“您的应用”。它将显示所有可用的隐私切换,如相机、照片、通知、Hive数据等。
在下面@Mischa的评论之后,测试并更新了对此的回答(更简洁):

if let appSettings = URL(string: UIApplication.openSettingsURLString), UIApplication.shared.canOpenURL(appSettings) {
    UIApplication.shared.open(appSettings)
}

上一个答案:

我发现这个问题的答案(虽然很有帮助)有太多的假设逻辑。如果有人碰到这个问题,这里有一个简单明了的Swift 5实现:

if let bundleIdentifier = Bundle.main.bundleIdentifier, let appSettings = URL(string: UIApplication.openSettingsURLString + bundleIdentifier) {
    if UIApplication.shared.canOpenURL(appSettings) {
        UIApplication.shared.open(appSettings)
    }
}
sauutmhj

sauutmhj2#

对于Swift 3,使用UIApplicationOpenSettingsURLString转到应用程序的设置,其中显示通知状态和“Hive数据”

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

twh00eeo3#

雨燕五:

if let url = URL(string: UIApplication.openSettingsURLString) {
    UIApplication.shared.open(url)
}
vuktfyat

vuktfyat4#

**更新:**这将被Apple拒绝。

要打开设置的通知部分,请使用此

UIApplication.shared.open(URL(string:"App-Prefs:root=NOTIFICATIONS_ID")!, options: [:], completionHandler: nil)
chy5wohz

chy5wohz5#

从iOS 15.4开始,我们可以直接使用UIApplicationOpenNotificationSettingsURLString深入链接到通知设置屏幕:
https://developer.apple.com/documentation/uikit/uiapplicationopennotificationsettingsurlstring?language=objc

00jrzges

00jrzges6#

这应该可以让你覆盖所有的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")
}

相关问题