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:
附可以跳转到的各个位置的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
- iOS开发技巧积累
- iOS 开发技巧1
- iOS 开发技巧2
- iOS开发技巧3
- iOS开发技巧4
- iOS开发技巧7
- iOS开发技巧8
- iOS开发技巧9
- iOS开发技巧10
- iOS开发技巧
- iOS开发技巧
- iOS开发技巧12
- iOS开发技巧14
- iOS开发技巧17
- iOS开发 小技巧
- ios开发技巧
- IOS开发技巧
- iOS 开发小技巧
- IoT: FIWARE+Docker 城市天气信息收集与可视化( 一)
- poj2337 欧拉道路/欧拉回路 字符串字典序排列(qsort实现)
- Android开发四年以来的工作难点总结
- shell学习八-----变量数值计算(())
- 工作 1 个月
- iOS开发技巧
- WaterGEMS第四讲:CAD文件导入WaterGMES方法
- JAVA 语言实现简单的聊天室,附源代码
- js面向对象小结(工厂模式,构造函数,原型方法,继承)
- kafka核心原理总结
- CF632D:Longest Subsequence(思维 & 数论)
- Poj 1860 Currency Exchange ( SPFA
- Fabric 1.0 读写集
- 23种设计模式