應用裡常需要引導使用者去開啟某個權限:「請到設定打開藍牙」、「請允許定位」。做法是透過 prefs: 這個 URL Scheme 搭配 openURL: 直接跳到對應的設定頁。
在專案的 info.plist 中新增 URL Types,並加入一項 URL Schemes 設為 prefs。

NSURL *url = [NSURL URLWithString:@"prefs:root=WIFI"];
if ([[UIApplication sharedApplication] canOpenURL:url]) {
[[UIApplication sharedApplication] openURL:url];
}
很多 App 都依賴定位,一旦使用者關閉,就可以用這段引導他回去開啟:
NSURL *url = [NSURL URLWithString:@"prefs:root=LOCATION_SERVICES"];
if ([[UIApplication sharedApplication] canOpenURL:url]) {
[[UIApplication sharedApplication] openURL:url];
}
NSURL *url = [NSURL URLWithString:@"prefs:root=FACETIME"];
if ([[UIApplication sharedApplication] canOpenURL:url]) {
[[UIApplication sharedApplication] openURL:url];
}
NSURL *url = [NSURL URLWithString:@"prefs:root=MUSIC"];
if ([[UIApplication sharedApplication] canOpenURL:url]) {
[[UIApplication sharedApplication] openURL:url];
}
NSURL *url = [NSURL URLWithString:@"prefs:root=Wallpaper"];
if ([[UIApplication sharedApplication] canOpenURL:url]) {
[[UIApplication sharedApplication] openURL:url];
}
NSURL *url = [NSURL URLWithString:@"prefs:root=Bluetooth"];
if ([[UIApplication sharedApplication] canOpenURL:url]) {
[[UIApplication sharedApplication] openURL:url];
}
NSURL *url = [NSURL URLWithString:@"prefs:root=CASTLE"];
if ([[UIApplication sharedApplication] canOpenURL:url]) {
[[UIApplication sharedApplication] openURL:url];
}
看完上面幾個例子應該會發現規律:只要換掉 prefs:root= 後面的值即可。常見對照如下:
About — prefs:root=General&path=About Accessibility — prefs:root=General&path=ACCESSIBILITY Airplane Mode On — prefs:root=AIRPLANE_MODE Auto-Lock — prefs:root=General&path=AUTOLOCK Brightness — prefs:root=Brightness Bluetooth — prefs:root=General&path=Bluetooth Date & Time — prefs:root=General&path=DATE_AND_TIME FaceTime — prefs:root=FACETIME General — prefs:root=General Keyboard — prefs:root=General&path=Keyboard iCloud — prefs:root=CASTLE iCloud Storage & Backup — prefs:root=CASTLE&path=STORAGE_AND_BACKUP International — prefs:root=General&path=INTERNATIONAL Location Services — prefs:root=LOCATION_SERVICES Music — prefs:root=MUSIC Music Equalizer — prefs:root=MUSIC&path=EQ Music Volume Limit — prefs:root=MUSIC&path=VolumeLimit Network — prefs:root=General&path=Network Nike + iPod — prefs:root=NIKE_PLUS_IPOD Notes — prefs:root=NOTES Notification — prefs:root=NOTIFICATIONS_ID Phone — prefs:root=Phone Photos — prefs:root=Photos Profile — prefs:root=General&path=ManagedConfigurationList Reset — prefs:root=General&path=Reset Safari — prefs:root=Safari Siri — prefs:root=General&path=Assistant Sounds — prefs:root=Sounds Software Update — prefs:root=General&path=SOFTWARE_UPDATE_LINK Store — prefs:root=STORE Twitter — prefs:root=TWITTER Usage — prefs:root=General&path=USAGE VPN — prefs:root=General&path=Network/VPN Wallpaper — prefs:root=Wallpaper Wi-Fi — prefs:root=WIFI
最後提醒一個重要現實:從 iOS 5.1 起,蘋果已不允許第三方 App 使用 prefs: 之外的私有 scheme 直接跳設定;目前的正規做法是呼叫 UIApplicationOpenSettingsURLString,把使用者帶到「本 App 的權限設定頁」。上表可作為理解與相容舊版本的參考。