Open phone settings when button is clicked in my app

亡梦爱人 提交于 2019-12-05 16:58:53
Vladimir Vozniak

Swift 3 iOS 10

let settingsUrl = NSURL(string:UIApplicationOpenSettingsURLString) as! URL
UIApplication.shared.open(settingsUrl, options: [:], completionHandler: nil)

@persianBlue: This is working on Xcode8 + iOS10.

UIApplication.shared.openURL(URL(string:UIApplicationOpenSettingsURLString)!)

UIApplicationOpenSettingsURLString provides a link to the app's settings. Previous to iOS10, if the app lacked a settings.Bundle, it would link to the settings home page. The logs would show:

_BSMachError: (os/kern) invalid capability (20) _BSMachError: (os/kern) invalid name (15)

If your intention is to link to the app's settings, you simply need to add a settings bundle. Apple Documentation

I have yet to find a way to link to the phone's setting's Home page.

chawki

This is no possible in iOS11 anymore, we can just open Settings like:

if let url = URL(string:UIApplicationOpenSettingsURLString), UIApplication.shared.canOpenURL(url) {
   //iOS 10 +
   UIApplication.shared.open(url, options: [:], completionHandler:{ didOpen in
      print("Opened \(didOpen)") 
   })
   //iOS 9 +
   UIApplication.shared.open(url)
}

If you see a white screen just like @PersianBlue your app might be missing custom settings. Here's what the documentation has to say about UIApplicationOpenSettingsURLString:

Used to create a URL that you can pass to the openURL(_:) 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.

I wrote the below function, it worked for me. I help it will help you guys.

func openLocationSettings(){ let scheme:String = UIApplicationOpenSettingsURLString if let url = URL(string: scheme) { if #available(iOS 10, *) { UIApplication.shared.open(url, options: [:], completionHandler: { (success) in print("Open \(scheme): \(success)") }) } else { let success = UIApplication.shared.openURL(url) print("Open \(scheme): \(success)") } } }

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!