iOS需要一些手机的隐私权限,下面介绍一下常用的隐私权限是否开启的获取方法
<!-- 相册 -->
<key>NSPhotoLibraryUsageDescription</key>
<string>App需要您的同意,才能访问相册</string>
<!-- 相机 -->
<key>NSCameraUsageDescription</key>
<string>App需要您的同意,才能访问相机</string>
<!-- 麦克风 -->
<key>NSMicrophoneUsageDescription</key>
<string>App需要您的同意,才能访问麦克风</string>
<!-- 位置 -->
<key>NSLocationUsageDescription</key>
<string>App需要您的同意,才能访问位置</string>
<!-- 在使用期间访问位置 -->
<key>NSLocationWhenInUseUsageDescription</key>
<string>App需要您的同意,才能在使用期间访问位置</string>
<!-- 始终访问位置 -->
<key>NSLocationAlwaysUsageDescription</key>
<string>App需要您的同意,才能始终访问位置</string>
<!-- 日历 -->
<key>NSCalendarsUsageDescription</key>
<string>App需要您的同意,才能访问日历</string>
<!-- 提醒事项 -->
<key>NSRemindersUsageDescription</key>
<string>App需要您的同意,才能访问提醒事项</string>
<!-- 运动与健身 -->
<key>NSMotionUsageDescription</key>
<string>App需要您的同意,才能访问运动与健身</string>
<!-- 健康更新 -->
<key>NSHealthUpdateUsageDescription</key>
<string>App需要您的同意,才能访问健康更新 </string>
<!-- 健康分享 -->
<key>NSHealthShareUsageDescription</key>
<string>App需要您的同意,才能访问健康分享</string>
<!-- 蓝牙 -->
<key>NSBluetoothPeripheralUsageDescription</key>
<string>App需要您的同意,才能访问蓝牙</string>
<!-- 媒体资料库 -->
<key>NSAppleMusicUsageDescription</key>
<string>App需要您的同意,才能访问媒体资料库</string>
<!-- 语音识别 -->
<key>NSSpeechRecognitionUsageDescription</key>
<string>App需要您的同意,才能使用语音识别</string>
- 打开设置中心
class func openSetCenter() {
let url = URL.init(string: UIApplication.openSettingsURLString)
if UIApplication.shared.canOpenURL(url!) {
UIApplication.shared.open(url!, options: [:], completionHandler: nil)
}
}
- 定位权限
class func isLocationAuthority() -> Bool {
if CLLocationManager.locationServicesEnabled() && CLLocationManager.authorizationStatus() != .denied {
return true
}
return false
}
- 相机权限
class func isCameraAuthority() -> Bool {
let status = AVCaptureDevice.authorizationStatus(for: .video)
if status == .restricted || status == .denied {
return false
}
return true
}
- 相册权限
class func isLibraryAuthority() -> Bool {
let status = PHPhotoLibrary.authorizationStatus()
if status == .restricted || status == .denied {
return false
}
return true
}
- 通知权限
class func isNotificationAuthority() -> Bool {
var isOpen = false
UNUserNotificationCenter.current().getNotificationSettings { settings in
DispatchQueue.main.async {
if settings.alertStyle == .none {
isOpen = false
} else {
isOpen = true
}
}
}
return isOpen
}
- 运动权限
class func isHealthAuthority() -> Bool {
if !HKHealthStore.isHealthDataAvailable() {
return false
}
let healthStore = HKHealthStore()
let type = HKObjectType.quantityType(forIdentifier: .height)!
let status = healthStore.authorizationStatus(for: type)
if status == .notDetermined {
var isOpen = false
let read = Set([HKObjectType]())
let write = Set([HKSampleType]())
healthStore.requestAuthorization(toShare: write, read: read) { success, error in
isOpen = success
}
return isOpen
} else if status == .sharingDenied {
return false
} else {
return true
}
}
- 网络权限
class func isNetAuthority() -> Bool {
let cellular = CTCellularData()
var isOpen = false
cellular.cellularDataRestrictionDidUpdateNotifier = { status in
if status == .restrictedStateUnknown || status == .notRestricted {
isOpen = false
} else {
isOpen = true
}
}
return isOpen
}