App 第一次安裝執行時,系統會跳出提示詢問使用者是否允許取得所在位置。若使用者當下選擇拒絕,之後系統不會再主動提示——他必須自己走進設定應用才能開啟。這對定位相關的 App 是個不小的流失點。
變通的辦法是在 App 內提供一個「前往設定」的按鈕,直接幫使用者跳到定位服務那一頁,讓他手動開啟。做法是透過 prefs: 這個 URL Scheme:
- (IBAction)btnSetting:(id)sender {
NSURL *url = [NSURL URLWithString:@"prefs:root=LOCATION_SERVICES"];
[[UIApplication sharedApplication] openURL:url];
}
同一套寫法搭配不同的 query 字串,就能跳到各個設定分頁:
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
呼叫方式與上面的範例完全相同,替換字串即可。
prefs: 屬於未公開的 URL Scheme,蘋果並不保證其可用性。這個 scheme 沒有任何官方文件,蘋果有權在任何版本中移除或變更它。實務上的具體影響是:
更關鍵的是,在鎖定應用目錄與沙箱越來越嚴格的 iOS 版本中,直接跳轉到任意設定頁的行為已被視為不鼓勵的用法。目前的正確做法是跳到你自己的 App 專屬設定頁,這也是唯一有官方背書的方式:
// iOS 8 之後的官方做法 NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString]; [[UIApplication sharedApplication] openURL:url];
這個常數會開啟設定應用並直接定位到你的 App 那一頁,使用者一眼就能看到未允許的權限項目並逐項開啟,體驗反而比丟他去一個總覽頁更順。若在 iOS 10 之前,則使用 UIApplicationOpenNotificationSettingsURLString 等更早的專用常數。
與其等使用者自己發現,更好的做法是在 App 內主動偵測狀態並給出引導。CLLocationManager 提供了 authorizationStatus(iOS 14 之後為 authorizationStatus 類別方法),回傳 kCLAuthorizationStatusDenied 就代表使用者已拒絕,此時在介面上顯示提示與「前往設定」按鈕,是最貼心的處理方式。切記不要在未經使用者操作下反覆跳出系統提示——那只會讓使用者更想刪掉你的 App。