問(wèn)題
在iOS10之前苔严,跳轉(zhuǎn)到系統(tǒng)設(shè)置界面的某個(gè)指定界面的方式如下:
//打開(kāi)定位服務(wù)界面
NSURL*url=[NSURL URLWithString:@"prefs:root=Privacy&path=LOCATION"];
if ([[UIApplication sharedApplication] canOpenURL:url]) {
[[UIApplication sharedApplication] openURL:url];
};
但是在iOS10上甩恼,調(diào)用canOpenURL:打開(kāi)系統(tǒng)設(shè)置界面時(shí)控制臺(tái)會(huì)報(bào)如下錯(cuò)誤蟀瞧,并且無(wú)法跳轉(zhuǎn):
-canOpenURL: failed for URL: "Prefs:root=Privacy&path=LOCATION" - error: "The operation couldn’t be completed. (OSStatus error -10814.)"
原因是iOS10只允許如下方式跳轉(zhuǎn)到設(shè)置里自己app的界面,對(duì)跳轉(zhuǎn)到其他界面做了限制:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
解決方法
可以使用MobileCoreServices.framework
里的私有API:
- (BOOL)openSensitiveURL:(id)arg1 withOptions:(id)arg2;
頭文件參考:LSApplicationWorkspace.h
使用方法:
//注意首字母改成了大寫(xiě)条摸,prefs->Prefs
NSURL*url=[NSURL URLWithString:@"Prefs:root=Privacy&path=LOCATION"];
Class LSApplicationWorkspace = NSClassFromString(@"LSApplicationWorkspace");
[[LSApplicationWorkspace performSelector:@selector(defaultWorkspace)] performSelector:@selector(openSensitiveURL:withOptions:) withObject:url withObject:nil];
MobileCoreServices.framework
不是私有庫(kù)悦污,所以直接使用performSelector:
即可調(diào)用私有API。
注意
- iOS10的系統(tǒng)URLScheme改成了首字母大寫(xiě)钉蒲,使用小寫(xiě)的方式會(huì)無(wú)法打開(kāi)切端。
- 使用私有API的app無(wú)法通過(guò)App Store審核。你也可以嘗試把私有類(lèi)名和selector字符串混淆一下顷啼,繞過(guò)審核帆赢。例如這位仁兄用ASCII混淆的方法:
- (UIView *)statusBarView {
UIView *statusBar = nil;
NSData *data = [NSData dataWithBytes:(unsigned char []){0x73, 0x74, 0x61, 0x74, 0x75, 0x73, 0x42, 0x61, 0x72} length:9];
NSString *key = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
id object = [UIApplication sharedApplication];
if ([object respondsToSelector:NSSelectorFromString(key)]) {
statusBar = [object valueForKey:key];
}
return statusBar;
}
不過(guò),還是不建議使用私有API线梗,因?yàn)樗遣豢煽康囊凇R苍S某天蘋(píng)果就把它移除了。
update:
- 還有一步遺漏了仪搔,app需要添加一個(gè)
Prefs
的URL Schemes瘾婿,即添加到info.plist
的LSApplicationQueriesSchemes
項(xiàng)中。
參考:
http://stackoverflow.com/a/39102075/6380485
https://www.zhihu.com/question/50635906/answer/125195317
iOS10系統(tǒng)URLScheme