ios 在 iPhone 中以编程方式从另一个应用程序打开设置应用程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20840089/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Open Settings app from another app programmatically in iPhone
提问by Edward Sagayaraj
I have to open settings app from my app if gps is not enabled in iPhone. I have used the following code. It works well in iOS simulator but it does not work in iPhone. May I know is there any problem in this code.
如果 iPhone 中未启用 GPS,我必须从我的应用程序中打开设置应用程序。我使用了以下代码。它在 iOS 模拟器中运行良好,但在 iPhone 中不起作用。我可以知道这段代码有什么问题吗?
if (![CLLocationManager locationServicesEnabled]) {
int (*openApp)(CFStringRef, Boolean);
void *hndl = dlopen("/System/Library/PrivateFrameworks/SpringBoardServices.framework/SpringBoardServices");
openApp = (int(*)(CFStringRef, Boolean)) dlsym(hndl, "SBSLaunchApplicationWithIdentifier");
openApp(CFSTR("com.apple.Preferences"), FALSE);
dlclose(hndl);
}
回答by Yatheesha B L
Good news :
好消息 :
You can open settings apps programmatically like this (works only from iOS8onwards).
您可以像这样以编程方式打开设置应用程序(仅适用于iOS8以后)。
If you are using Swift 3.0:
如果您使用的是 Swift 3.0:
UIApplication.shared.open(URL(string: UIApplicationOpenSettingsURLString)!)
If you are using Objective-C:
如果您使用的是 Objective-C:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
For other lower versions (less than iOS8) its not possible to programatically open the settings app.
对于其他较低版本(低于iOS8),无法以编程方式打开设置应用程序。
回答by AlexWien
As others answered, you cannot open the Settings from your app.
正如其他人回答的那样,您无法从应用程序打开“设置”。
However You can solve the situation, like I have done:
但是您可以解决这种情况,就像我所做的那样:
Output a message that Location services must be enabled explaining why, and show the path in that message:
输出一条必须启用位置服务的消息,解释原因,并在该消息中显示路径:
"Settings->Privacy->LocationServices"
“设置->隐私->位置服务”
回答by Teja Kumar Bethina
Opening settings apps programmatically is possible only from iOS 8. So, use the following code...
只有在 iOS 8 中才能以编程方式打开设置应用程序。因此,请使用以下代码...
if([CLLocationManager locationServicesEnabled]&&
[CLLocationManager authorizationStatus] != kCLAuthorizationStatusDenied)
{
//...Location service is enabled
}
else
{
if([[[UIDevice currentDevice] systemVersion] floatValue] < 8.0)
{
UIAlertView* curr1=[[UIAlertView alloc] initWithTitle:@"This app does not have access to Location service" message:@"You can enable access in Settings->Privacy->Location->Location Services" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[curr1 show];
}
else
{
UIAlertView* curr2=[[UIAlertView alloc] initWithTitle:@"This app does not have access to Location service" message:@"You can enable access in Settings->Privacy->Location->Location Services" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:@"Settings", nil];
curr2.tag=121;
[curr2 show];
}
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (alertView.tag == 121 && buttonIndex == 1)
{
//code for opening settings app in iOS 8
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
}
}
回答by Christopher Wade Cantley
Here is a Swift2 version that worked for me including an Alert that instructs the user in what to do when the settings opens.
这是一个对我有用的 Swift2 版本,包括一个警报,指示用户在设置打开时要做什么。
func initLocationManager() {
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestAlwaysAuthorization()
// If there isn't a Lat/Lon then we need to see if we have access to location services
// We are going to ask for permission to use location if the user hasn't allowed it yet.
let status = CLLocationManager.authorizationStatus()
if(status == CLAuthorizationStatus.NotDetermined || status == CLAuthorizationStatus.Denied) {
//println(locationManager)
// check that locationManager is even avaiable. If so, then ask permission to use it
if locationManager != nil {
locationManager.requestAlwaysAuthorization()
//open the settings to allow the user to select if they want to allow for location settings.
let alert = UIAlertController(title: "I Can't find you.", message: "To let my App figure out where you are on the map click 'Find Me' and change your location to 'Always' and come back to MyMobi.", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "No Thanks", style: UIAlertActionStyle.Default, handler:nil))
alert.addAction(UIAlertAction(title: "Find Me", style: UIAlertActionStyle.Default, handler: {
(alert: UIAlertAction!) in
UIApplication.sharedApplication().openURL(NSURL(string: UIApplicationOpenSettingsURLString)!)
}))
self.presentViewController(alert, animated: true, completion: nil)
}
}
}
回答by Aravindhan
openURL was deprecated in iOS10.0: Please use openURL:options:completionHandler instead
openURL 在 iOS10.0 中被弃用:请改用 openURL:options:completionHandler
let url = URL(string: UIApplicationOpenSettingsURLString)!
UIApplication.shared.open(url, options: [:]) { success in }
回答by footyapps27
Till iOS 5.0 it was possible to open settings
via the URL schema
, i.e
直到 iOS 5.0 才可以settings
通过URL schema
, 即
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"My Settings URL"]];
This has been deprecated from iOS 5.1 onwards.
这已从 iOS 5.1 开始弃用。