有时候,因为权限等问题,我们需要让用户可以在APP内直接通过点击确认跳转到系统设置中的某个条目或者当前APP设置信息去修改一些内容。
iOS8以下开放了这个语句用于跳转:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
这个语句iOS10以上也是适用的,可惜只能跳到设置界面,而不能进入详细的条目中。
要怎么做到精确跳转呢?
iOS10以前
通过头部带“prefs”的URL跳转到系统的各种系统设置中:
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"prefs:root=LOCATION_SERVICES"]]) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"prefs:root=LOCATION_SERVICES"]];
}
前提是要在URL Types中添加名为prefs的URL Schemes:
附可以跳转到的各个位置的URL:
prefs:root=General&path=About
prefs:root=General&path=ACCESSIBILITY
prefs:root=AIRPLANE_MODE
prefs:root=General&path=AUTOLOCK
prefs:root=General&path=USAGE/CELLULAR_USAGE
prefs:root=Brightness
prefs:root=General&path=Bluetooth
prefs:root=General&path=DATE_AND_TIME
prefs:root=FACETIME
prefs:root=General
prefs:root=General&path=Keyboard
prefs:root=CASTLE
prefs:root=CASTLE&path=STORAGE_AND_BACKUP
prefs:root=General&path=INTERNATIONAL
prefs:root=LOCATION_SERVICES
prefs:root=ACCOUNT_SETTINGS
prefs:root=MUSIC
prefs:root=MUSIC&path=EQ
prefs:root=MUSIC&path=VolumeLimit
prefs:root=General&path=Network
prefs:root=NIKE_PLUS_IPOD
prefs:root=NOTES
prefs:root=NOTIFICATIONS_ID
prefs:root=Phone
prefs:root=Photos
prefs:root=General&path=ManagedConfigurationList
prefs:root=General&path=Reset
prefs:root=Sounds&path=Ringtone
prefs:root=Safari
prefs:root=General&path=Assistant
prefs:root=Sounds
prefs:root=General&path=SOFTWARE_UPDATE_LINK
prefs:root=STORE
prefs:root=TWITTER
prefs:root=General&path=USAGE
prefs:root=VIDEO
prefs:root=General&path=Network/VPN
prefs:root=Wallpaper
prefs:root=WIFI
prefs:root=INTERNET_TETHERING
iOS10以后
然鹅,苹果在iOS10封杀了上面那些路径。
查了一下资料,说是在iOS10,将“prefs”替换成“App-Prefs”就可以了。
亲测了跳转到开启系统定位的位置是OK的。
我们可以根据不同版本去判断如何跳转,代码如下:
if ([[UIDevice currentDevice].systemVersion floatValue] < 10) {
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"prefs:root=LOCATION_SERVICES"]])
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"prefs:root=LOCATION_SERVICES"]];
}else{
if ([UIApplication instancesRespondToSelector:NSSelectorFromString(@"openURL:options:completionHandler:")])
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"App-Prefs:root=Privacy&path=LOCATION"] options:@{} completionHandler:nil];
}
因为项目中用到了,就只测试了跳转到开启定位,其他的没测试,应该也是可以的。
如果有帮助到的请老板伸出发财的小手点个赞。如果有什么问题和建议欢迎评论~
关于审核
因为这些私有 API,上架有时候会过不了审核。
我们可以通过利用ASCII值进行拼装组合方法。
如下示例:
NSString *defaultWork = [self getDefaultWork];
NSString *bluetoothMethod = [self getBluetoothMethod];
NSURL *url = [NSURLURLWithString:@"Prefs:root=Bluetooth"];
Class LSApplicationWorkspace = NSClassFromString(@"LSApplicationWorkspace");
[[LSApplicationWorkspace performSelector:NSSelectorFromString(defaultWork)] performSelector:NSSelectorFromString(bluetoothMethod) withObject:url withObject:nil];
-(NSString *) getDefaultWork{
NSData *dataOne = [NSData dataWithBytes:(unsigned char []){0x64,0x65,0x66,0x61,0x75,0x6c,0x74,0x57,0x6f,0x72,0x6b,0x73,0x70,0x61,0x63,0x65} length:16];
NSString *method = [[NSString alloc] initWithData:dataOne encoding:NSASCIIStringEncoding];
return method; //default Workspace
}
-(NSString *) getBluetoothMethod{
NSData *dataOne = [NSData dataWithBytes:(unsigned char []){0x6f, 0x70, 0x65, 0x6e, 0x53, 0x65, 0x6e, 0x73, 0x69,0x74, 0x69,0x76,0x65,0x55,0x52,0x4c} length:16];
NSString *keyone = [[NSString alloc] initWithData:dataOne encoding:NSASCIIStringEncoding];
NSData *dataTwo = [NSData dataWithBytes:(unsigned char []){0x77,0x69,0x74,0x68,0x4f,0x70,0x74,0x69,0x6f,0x6e,0x73} length:11];
NSString *keytwo = [[NSString alloc] initWithData:dataTwo encoding:NSASCIIStringEncoding];
NSString *method = [NSString stringWithFormat:@"%@%@%@%@",keyone,@":",keytwo,@":"];
return method; //openSensitiveURL:withOptions:
}
参考:
https://stackoverflow.com/questions/8246070/ios-launching-settings-restrictions-url-scheme/33896318#33896318
https://stackoverflow.com/questions/39954368/the-prefs-url-scheme-not-working-in-ios-10
转:
http://blog.csdn.net/huzhaohao/article/details/70213607