Opening the Settings app from another app

2018-12-31 04:12发布

Okay, I know that there are many question about it, but they are all from many time ago.

So. I know that it is possible because the Map app does it.

In the Map app if I turn off the localization for this app, it send me a message, and if I press okay, the "Settings App" will be open. And my question is, how is this possible? How can I open the "Setting app" from my own app?

Basically I need to do the same thing, if the user turn off the location for my app, then I'll show him a message saying something that will open the "Setting app"

15条回答
妖精总统
2楼-- · 2018-12-31 04:34

SWIFT 4.0

'openURL' was deprecated in iOS 10.0: Please use openURL:options:completionHandler: instead

UIApplication.shared.open(URL.init(string: UIApplicationOpenSettingsURLString)! , options: [:], completionHandler: nil)
查看更多
路过你的时光
3楼-- · 2018-12-31 04:37

From @Yatheeshaless's answer:

You can open settings app programmatically in iOS8, but not in earlier versions of iOS.

Swift:

   UIApplication.sharedApplication().openURL(NSURL(string:UIApplicationOpenSettingsURLString)!)

Swift 4:

if let url = NSURL(string: UIApplicationOpenSettingsURLString) as URL? {
    UIApplication.shared.openURL(url)
}

Swift 4.2 (BETA):

if let url = NSURL(string: UIApplication.openSettingsURLString) as URL? {
    UIApplication.shared.open(url, options: [:], completionHandler: nil)
}

Objective-C:

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
查看更多
冷夜・残月
4楼-- · 2018-12-31 04:38

In Swift 3 / iOS 10+ this now looks like

if let url = URL(string: "App-Prefs:root=LOCATION_SERVICES") {
    UIApplication.shared.open(url, completionHandler: .none)
}
查看更多
唯独是你
5楼-- · 2018-12-31 04:39

YES!! you can launch Device Settings screen, I have tested on iOS 9.2

Step 1. we need to add URL schemes

Go to Project settings --> Info --> URL Types --> Add New URL Schemes

enter image description here

Step 2. Launch Settings programmatically Thanks to @davidcann

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"prefs://"]];

Also we can launch sub-screens like Music, Location etc. as well by just using proper name

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"prefs:root=MUSIC"]];

See this full name list here shared by Henri Normak


Update:

As per the comment everyone wants to know what happens after this change to my application submission status?

So YES!! I got successful update submission and application is available on store without any complain.

Just to confirm, I Just downloaded this morning and disabled Location services, and then started the app, which asked me for location permission and then my alert popup was there to send me on settings -> location services page --> Enabled --> That's it!!

查看更多
长期被迫恋爱
6楼-- · 2018-12-31 04:46

You can use this on iOS 5.0 and later: This no longer works.

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"prefs://"]];
查看更多
零度萤火
7楼-- · 2018-12-31 04:46

UIApplicationOpenSettingsURLString this will only work if you have previously allowed for any permission. For example Location, Photo, Contact, Push notification access. So if you have not such permission(s) from the user:

If iOS 10 or above,

It will open the Settings but then crash it. The reason, there's nothing in settings for your app.

Below code will open your application settings inside the iOS Setting.

NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
if ([[UIApplication sharedApplication] canOpenURL:url]) {
    [[UIApplication sharedApplication] openURL:url];
}

Due to device unavailability, I couldn't check this on iOS < 10.

Also, I could find below code from some gist and it works fine on iOS 10 as well. But I am not sure if this will approve by Apple review team or not.

https://gist.github.com/johnny77221/bcaa5384a242b64bfd0b8a715f48e69f

查看更多
登录 后发表回答