参考:IOS应用内跳转到系统设置
考虑到目前基本上不需要考虑iOS8之前的系统了,所以本文只介绍iOS8+的情况
主要步骤
1. 设置URL Scheme
进入info.plist文件,设置URL Types属性
2. 创建跳转路径url
UIApplicationOpenSettingsURLString:用来创建跳转路径url的string
Used to create a URL that you can pass to the
open<wbr>URL(_:)
method. When you open the URL built from this string, the system launches the Settings app and displays the app’s custom settings, if it has any.
适用版本:iOS 8.0+ / tvOS 9.0+
let url = URL(string: UIApplicationOpenSettingsURLString)
3. 执行跳转
openURL(_:):(iOS 2 ~ iOS 10)
open(_:options:completionHandler:):(iOS 10 ~)
UIApplication.shared.openURL(url)
or
UIApplication.shared.open(URL(string: url options: [:], completionHandler: { (granted) in
if granted {
print("jump success")
} else {
print("jump failure")
}
})
Tips:理论上以上代码的跳转结果是
- 如果系统为:iOS 8 ~ iOS 10,跳转到设置界面
- 如果系统为:iOS 10 ~,跳转到当前应用的设置界面
由于身边没有iOS 10以下系统的真机,故哪位大兄弟发现有错误,那就错了吧!!!哈哈哈~~~ -
注意千万不要把
UIApplicationOpenSettingsURLString
写成“ UIApplicationOpenSettingsURLString”
,UIApplicationOpenSettingsURLString
本身就是字符串类型。
网友评论