好的,我知道有许多关于这个问题的疑问,但它们都是很久以前的。
所以,在地图应用中,如果我关闭该应用的本地化,它会发送一条消息。如果我按下“确定”按钮,“设置应用程序”将被打开。 我的问题是,这是如何可能的? 我怎样能够从我的应用程序中打开“设置应用程序”?
基本上,如果用户关闭了我的应用程序的位置信息功能,我需要做同样的事情,那么我会向他显示一条消息,告诉他将打开“设置应用程序”。
好的,我知道有许多关于这个问题的疑问,但它们都是很久以前的。
所以,在地图应用中,如果我关闭该应用的本地化,它会发送一条消息。如果我按下“确定”按钮,“设置应用程序”将被打开。 我的问题是,这是如何可能的? 我怎样能够从我的应用程序中打开“设置应用程序”?
基本上,如果用户关闭了我的应用程序的位置信息功能,我需要做同样的事情,那么我会向他显示一条消息,告诉他将打开“设置应用程序”。
UIApplicationOpenSettingsURLString
仅在您之前已允许任何权限(例如位置,照片,联系人,推送通知)时才有效。因此,如果用户没有授予此类权限:
如果iOS 10或更高版本,
它将打开设置,但会导致崩溃。这是因为您的应用程序在设置中没有内容。
以下代码将在iOS设置中打开您的应用程序设置。
NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
if ([[UIApplication sharedApplication] canOpenURL:url]) {
[[UIApplication sharedApplication] openURL:url];
}
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
SWIFT 4.0
'openURL'在iOS 10.0中已弃用:请使用 openURL:options:completionHandler: 替代。
UIApplication.shared.open(URL.init(string: UIApplicationOpenSettingsURLString)! , options: [:], completionHandler: nil)
测试通过iOS 10。工作正常
NSArray* urlStrings = @[@"prefs:root=WIFI", @"App-Prefs:root=WIFI"];
for(NSString* urlString in urlStrings){
NSURL* url = [NSURL URLWithString:urlString];
if([[UIApplication sharedApplication] canOpenURL:url]){
[[UIApplication sharedApplication] openURL:url];
break;
}
}
快乐编程 :)
Swift 4
我更喜欢以更安全的方式打开设置,
if let settingUrl = URL(string:UIApplicationOpenSettingsURLString) {
UIApplication.shared.open(settingUrl)
}
else {
print("Setting URL invalid")
}
从iOS 16开始,您可以使用openSettingsURLString
if let url = URL(string: UIApplication. openSettingsURLString) {
await UIApplication.shared.open(url)
}
Swift 你可以使用以下函数打开设置应用程序并进入蓝牙页面
func openSettingsApp(){
if let settings = NSURL(string: "prefs:root=Bluetooth") {
UIApplication.sharedApplication().openURL(settings)
}
}
这不会打开应用程序的设置。这将通过深度链接到蓝牙打开设置应用程序。
public class func showSettingsAlert(title:String,message:String,onVC viewController:UIViewController,onCancel:(()->())?){
YourClass.show2ButtonsAlert(onVC: viewController, title: title, message: message, button1Title: "Settings", button2Title: "Cancel", onButton1Click: {
if let settingsURL = NSURL(string: UIApplicationOpenSettingsURLString){
UIApplication.sharedApplication().openURL(settingsURL)
}
}, onButton2Click: {
onCancel?()
})
}
public class func show2ButtonsAlert(onVC viewController:UIViewController,title:String,message:String,button1Title:String,button2Title:String,onButton1Click:(()->())?,onButton2Click:(()->())?){
dispatch_async(dispatch_get_main_queue()) {
let alert : UIAlertController = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: button1Title, style:.Default, handler: { (action:UIAlertAction) in
onButton1Click?()
}))
alert.addAction(UIAlertAction(title: button2Title, style:.Default, handler: { (action:UIAlertAction) in
onButton2Click?()
}))
viewController.presentViewController(alert, animated: true, completion: nil)
}
}
像这样调用:
YourClass.showSettingsAlert("App would like to access camera", message: "App would like to access camera desc", onVC: fromViewController, onCancel: {
print("canceled")
})