iOS开发——ios10以后应用内跳转系统设置页面无效的解决办法
2017-07-28 本文已影响62人
YY程序猿
需求
应用需要定位服务,如果系统定位服务未开启,就要跳到系统设置页面。
问题
关于如何跳转,网上有很多好的文章,这里就不赘述了,贴出几个链接:
1、http://www.jianshu.com/p/767c409c50e6
2、http://www.2cto.com/kf/201506/405779.html
3、http://blog.csdn.net/wuyanyanstrong/article/details/51149786
4、http://blog.csdn.net/wlm0813/article/details/54896499
等。。。
但是,ios10以后,只能跳到系统设置里自己应用的配置界面,如下
而无法跳转到系统总体的设置页面,如下
系统设置
解决方法
利用一个私有API,需要用到的类:LSApplicationWorkspace,属于MobileCoreServices.framework静态库,关于这一私有API和runtime的知识可以查看:http://www.jianshu.com/p/6167b9ce7af8
代码
NSString *defaultWork = @"defaultWorkspace";
NSString *bluetoothMethod = @"openSensitiveURL:withOptions:";
Class LSApplicationWorkspace = NSClassFromString(@"LSApplicationWorkspace");
[[LSApplicationWorkspace performSelector:NSSelectorFromString(defaultWork)] performSelector:NSSelectorFromString(bluetoothMethod) withObject:url withObject:nil];
这样就可以跳转到系统设置页面了。
备注
1、这样跳转,不是跳转到系统设置主页面,系统设置页面本身在哪个页面,跳转过去,页面会保持在那个页面,例如你之前操作手机打开了wifi设置,跳转过去就是在wifi设置;
2、根据我所掌握的知识,ios10之后,没有办法指定跳转的具体位置,例如你想指定跳转到系统设置的蓝牙设置,ios10以后做不到,如果有会的大神朋友请给我留言!
3、上面的代码中,@"defaultWorkspace",@"openSensitiveURL:withOptions:"这些字比较敏感,可能会影响审核,可以采取NSData转NSString的方式,屏蔽掉那些字段
//@"defaultWorkspace"
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 *defaultWork = [[NSString alloc] initWithData:dataOne encoding:NSASCIIStringEncoding];
//@"openSensitiveURL:withOptions:"
NSData *dataTwo = [NSData dataWithBytes:(unsigned char []){0x6f, 0x70, 0x65, 0x6e, 0x53, 0x65, 0x6e, 0x73, 0x69,0x74, 0x69,0x76,0x65,0x55,0x52,0x4c} length:16];
NSString *keyTwo = [[NSString alloc] initWithData:dataTwo encoding:NSASCIIStringEncoding];
NSData *dataThree = [NSData dataWithBytes:(unsigned char []){0x77,0x69,0x74,0x68,0x4f,0x70,0x74,0x69,0x6f,0x6e,0x73} length:11];
NSString *keyThree = [[NSString alloc] initWithData:dataThree encoding:NSASCIIStringEncoding];
NSString *bluetoothMethod = [NSString stringWithFormat:@"%@%@%@%@",keyTwo,@":",keyThree,@":"];
//私有API跳转
Class LSApplicationWorkspace = NSClassFromString(@"LSApplicationWorkspace");
[[LSApplicationWorkspace performSelector:NSSelectorFromString(defaultWork)] performSelector:NSSelectorFromString(bluetoothMethod) withObject:url withObject:nil];