需求
應用需要定位服務,如果系統定位服務未開啟,就要跳到系統設置頁面。
問題
關于如何跳轉,網上有很多好的文章,這里就不贅述了,貼出幾個鏈接:
1、http://www.lxweimin.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.lxweimin.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];