How can I open the Settings app when the user presses a button?

浪尽此生 提交于 2019-11-27 23:06:56
Greg

As per @bnduati's answer, in iOS 8 you can use the following code to open your app's settings in the settings app:

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];

Old answer:

From iOS 5.1 through iOS 7, it was not possible to open the settings app from another application.

You might want to investigate this framework for providing in-app settings.

There are quite a few other questions that discuss this topic:

iPhone - how to put Settings bundle seen through System Settings App into your own App?

Launch iPhone setting screen from Application?

How to send a user to the main iPhone Settings screen from within your iPhone App

bnduati

In iOS 8 and later you can send the user to your app's settings in the following way:

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];

You can use this on iOS 5.0 and later:

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

I don't know if this is possible or not, but one issue is that the user would then need to navigate back to your app. You can instead have your own preferences view that can write to the same file that the preferences app will use by using NSUserDefaults.

[[NSUserDefaults standardUserDefaults] setObject:value forKey:key];

[[NSUserDefaults standardUserDefaults] stringForKey:key]

Swift Syntax:

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

Swift 3

private func showAlertPrivacy() {
    let alertController = UIAlertController(title: nil, message: "messagePrivacy", preferredStyle: .alert)
    let alertNo = UIAlertAction(title: "No", style: .default) { (_) in

    }
    alertController.addAction(alertNo)

    let alertSetting = UIAlertAction(title: "Settings", style: .default) { (_) in

        UIApplication.shared.open(URL(string:UIApplicationOpenSettingsURLString)!, options: [:], completionHandler: { (_) in

        })
    }
    alertController.addAction(alertSetting)

    present(alertController, animated: true) { 

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