IOS 檢測使用者是否開啟定位 和 利用URL Scheme跳轉到系統設定Settings
開發中經常會碰到檢測 使用者是否開啟定位許可權 和 獲取圖片許可權 開啟系統通知 等
有時檢測到使用者系統級關閉,需要去設定裡去開啟。
通過URL Scheme的方式開啟內建的Settings,程式碼如下:
以下是內建的Settings的URL Scheme:
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
在iOS8版本使用以下方法:
NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
if
([[UIApplication sharedApplication] canOpenURL:url]) {
[[UIApplication sharedApplication] openURL:url];
經過驗證iOS8 iOS9 都可以使用url Scheme;
檢測是否允許獲取定位:
CLAuthorizationStatus type = [CLLocationManager authorizationStatus];
if (![CLLocationManager locationServicesEnabled] || type == kCLAuthorizationStatusDenied){
nslog(@“做處理”);
}
[CLLocationManager locationServicesEnabled]是檢測系統定位是否開啟。
[CLLocationManager authorizationStatus];是檢測使用者是否對本應用開啟定位許可權。
警告:
關於iOS地圖定位中點選設定->隱私->定位服務 有時會閃退
iOS8之後,如果應用中用到了地圖定位,那麼點選設定->隱私->定位服務 再點選該應用有時候會出現閃退問題,其原因是iOS8之後定位中添加了NSLocationWhenInUseDescription和NSLocationAlwaysUsageDescription,需要在plist檔案中新增這兩個或其中一個,出現閃退是因為在plist中把這兩個值設成了Boolean型別的,將其改成NSString就不會閃退了。