iOS开发技巧

来源:互联网 发布:张海山锐线体简mac 编辑:程序博客网 时间:2024/05/17 04:07

有时候,因为权限等问题,我们需要让用户可以在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:

1

附可以跳转到的各个位置的URL:

prefs:root=General&path=Aboutprefs:root=General&path=ACCESSIBILITYprefs:root=AIRPLANE_MODEprefs:root=General&path=AUTOLOCKprefs:root=General&path=USAGE/CELLULAR_USAGEprefs:root=Brightnessprefs:root=General&path=Bluetoothprefs:root=General&path=DATE_AND_TIMEprefs:root=FACETIMEprefs:root=Generalprefs:root=General&path=Keyboardprefs:root=CASTLEprefs:root=CASTLE&path=STORAGE_AND_BACKUPprefs:root=General&path=INTERNATIONALprefs:root=LOCATION_SERVICESprefs:root=ACCOUNT_SETTINGSprefs:root=MUSICprefs:root=MUSIC&path=EQprefs:root=MUSIC&path=VolumeLimitprefs:root=General&path=Networkprefs:root=NIKE_PLUS_IPODprefs:root=NOTESprefs:root=NOTIFICATIONS_IDprefs:root=Phoneprefs:root=Photosprefs:root=General&path=ManagedConfigurationListprefs:root=General&path=Resetprefs:root=Sounds&path=Ringtoneprefs:root=Safariprefs:root=General&path=Assistantprefs:root=Soundsprefs:root=General&path=SOFTWARE_UPDATE_LINKprefs:root=STOREprefs:root=TWITTERprefs:root=General&path=USAGEprefs:root=VIDEOprefs:root=General&path=Network/VPNprefs:root=Wallpaperprefs:root=WIFIprefs: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