Откройте приложение "Настройки" из другого приложения программно в iPhone
Мне нужно открыть приложение настроек из моего приложения, если gps не включен в iPhone. Я использовал следующий код. Он отлично работает в симуляторе 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);
}
Ответы
Ответ 1
Хорошая новость:
Вы можете программным способом открывать приложения приложений (работает только с iOS8).
Если вы используете Swift 3.0:
UIApplication.shared.open(URL(string: UIApplicationOpenSettingsURLString)!)
Если вы используете Objective-C:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
Для других более низких версий (менее iOS8) невозможно программно открыть приложение настроек.
Ответ 2
Как уже было сказано, вы не можете открыть настройки из своего приложения.
Однако вы можете решить ситуацию, как я это сделал:
Вывести сообщение о том, что службы определения местоположения должны быть включены с объяснением причины и показать путь в этом сообщении:
"Настройки- > Privacy- > LocationServices"
Ответ 3
Открытие приложений приложений программно возможно только с 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]];
}
}
Ответ 4
До iOS 5.0 можно было открыть settings
через URL schema
, i.e
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"My Settings URL"]];
Это устарело от iOS 5.1 и далее.
Ответ 5
Вот версия Swift2, которая работала для меня, включая Alert, которая указывает пользователю, что делать, когда откроются настройки.
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)
}
}
}
Ответ 6
openURL устарел в iOS10.0: используйте OpenURL: options: completeHandler вместо
let url = URL(string: UIApplicationOpenSettingsURLString)!
UIApplication.shared.open(url, options: [:]) { success in }