好吧,我知道关于它有很多问题,但它们都是很久以前的事了。
所以。我知道这是可能的,因为地图应用程序做到了。
在地图应用程序中,如果我关闭这个应用程序的本地化,它会给我发送一条消息,如果我按ok,“设置应用程序”就会打开。 我的问题是,这怎么可能呢? 如何从我自己的应用程序打开“设置应用程序”?
基本上我需要做同样的事情,如果用户关闭了我的应用程序的位置,那么我就会给他显示一条信息,上面写着将打开“设置应用程序”
好吧,我知道关于它有很多问题,但它们都是很久以前的事了。
所以。我知道这是可能的,因为地图应用程序做到了。
在地图应用程序中,如果我关闭这个应用程序的本地化,它会给我发送一条消息,如果我按ok,“设置应用程序”就会打开。 我的问题是,这怎么可能呢? 如何从我自己的应用程序打开“设置应用程序”?
基本上我需要做同样的事情,如果用户关闭了我的应用程序的位置,那么我就会给他显示一条信息,上面写着将打开“设置应用程序”
当前回答
把这个加到你的课上,
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")
})
其他回答
正如Karan Dua提到的,这现在可以在iOS8中使用UIApplicationOpenSettingsURLString,参见苹果的文档。
例子:
斯威夫特4.2
UIApplication.shared.open(URL(string: UIApplication.openSettingsURLString)!)
在Swift 3中:
UIApplication.shared.open(URL(string:UIApplicationOpenSettingsURLString)!)
在Swift 2中:
UIApplication.sharedApplication().openURL(NSURL(string:UIApplicationOpenSettingsURLString)!)
在objective - c中
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
iOS 8之前:
你不能。正如你所说,这已经被报道了很多次,弹出窗口要求你打开位置服务是由苹果公司提供的,而不是由应用程序本身。这就是为什么它能够打开设置应用程序。
以下是一些相关的问题和文章:
是否可以使用openURL打开设置应用程序?
以编程方式打开设置应用程序(iPhone)
当用户按下按钮时,如何打开设置应用程序?
iPhone:从应用程序打开应用程序首选项面板
通过单击应用程序首选项中的一个条目打开UIPickerView -如何?
打开设置应用程序?
iOS:你的设置是错误的
iOS 10更新
苹果将该方法更改为在主线程上打开async。但是,从现在开始,只能在本机设置中打开应用程序设置。
[[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
iOS 9更新
现在可以直接进入子设置菜单。但是,需要创建URL方案。有两种方法:
XCode -你会在目标,信息,URL方案中找到它。然后,输入prefs。 直接添加到*-Info.plist。增加如下内容: <键> CFBundleURLTypes关键> < / < >数组 < dict > <键> CFBundleTypeRole关键> < / <字符串>编辑字符串> < / <键> CFBundleURLSchemes关键> < / < >数组 <字符串>首选项> < /字符串 > < /数组 < / dict > > < /数组
然后代码:
斯威夫特
UIApplication.sharedApplication () .openURL (NSURL(弦:“首选项:根=一般路径=键盘”)!)
objective - c
[eapplication sharedApplication] openURL:[NSURL优先字符串:
斯威夫特4
我更喜欢以更安全的方式打开设置,
if let settingUrl = URL(string:UIApplicationOpenSettingsURLString) {
UIApplication.shared.open(settingUrl)
}
else {
print("Setting URL invalid")
}
你可以在iOS 5.0及更高版本上使用这个:这个不再有效。
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"prefs://"]];
您可以使用下面的代码。
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];