You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

iOS Swift如何在设置应用中打开App通知设置(全版本适配)

解决iOS全版本跳转App通知设置的问题

你的代码核心问题在于错误拼接了bundleIdentifier,且未利用iOS10+官方提供的更可靠API,以下是修正后的全版本适配方案:

问题根源

  1. iOS15.4+ 提供的UIApplicationOpenNotificationSettingsURLString和iOS16+的UIApplication.openNotificationSettingsURLString本身已包含当前App的bundle信息,手动拼接会导致URL无效,只能跳转至设置主界面。
  2. iOS10+ 苹果官方提供了UNUserNotificationCenter的openSettings()方法,可直接跳转至通知设置,比构造URL更稳定。
  3. iOS8-9 无官方直接跳转通知设置的API,只能跳转至App设置主界面。

修正后的代码

首先确保导入UserNotifications框架:

import UserNotifications

然后修改按钮点击方法:

@IBAction func buttonTapped(_ sender: Any) {
    if #available(iOS 10.0, *) {
        // iOS10+ 官方API直接跳转通知设置
        UNUserNotificationCenter.current().openSettings(completionHandler: nil)
    } else {
        // iOS8-9 只能跳转至App设置主界面
        guard let appSettingsURL = URL(string: UIApplication.openSettingsURLString) else { return }
        if UIApplication.shared.canOpenURL(appSettingsURL) {
            UIApplication.shared.open(appSettingsURL)
        }
    }
}

补充说明

  • iOS10及以上:调用openSettings()是苹果官方推荐的方式,无需手动处理URL,兼容性和稳定性最佳。
  • iOS8-9:受限于系统API,只能引导用户到App设置主界面,需用户手动进入通知设置。
  • 无需保留原有的UIApplication扩展,上述代码已覆盖所有iOS8+版本的适配需求。

内容的提问来源于stack exchange,提问作者DayPhan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 15:00:49