1败许、iOS開發(fā)檢測(cè)是否開啟定位:
需要導(dǎo)入:
#import <CoreLocation/CoreLocation.h>
代碼如下:
if ([CLLocationManager locationServicesEnabled] && [CLLocationManager authorizationStatus] != kCLAuthorizationStatusDenied) {// 開啟狀態(tài)
}
2、iOS開發(fā)檢測(cè)是否允許消息推送:
需要導(dǎo)入:
#import <UserNotifications/UserNotifications.h>
代碼如下:
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_8_0
UIUserNotificationSettings *setting = [[UIApplication sharedApplication] currentUserNotificationSettings];
if (setting.types != UIUserNotificationTypeNone) {// 開啟狀態(tài)
}
#else
UIRemoteNotificationType type = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
if (type != UIRemoteNotificationTypeNone) {// 開啟狀態(tài)
}
#endif
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
[[UNUserNotificationCenter currentNotificationCenter] getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings *settings) {
if (settings.authorizationStatus == UNAuthorizationStatusAuthorized) {// 開啟狀態(tài)
}
}];
#elif __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_8_0
if ([[UIApplication sharedApplication] isRegisteredForRemoteNotifications]) {// 開啟狀態(tài)
}
#else
UIRemoteNotificationType type = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
if (type != UIRemoteNotificationTypeNone) {// 開啟狀態(tài)
}
#endif
3淑蔚、iOS開發(fā)檢測(cè)是否開啟攝像頭:
需要導(dǎo)入:
#import <AVFoundation/AVFoundation.h>
代碼如下:
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_7_0
AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
if (authStatus == AVAuthorizationStatusNotDetermined) {
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
if (granted) {// 開啟狀態(tài)
} else {// 關(guān)閉狀態(tài)
}
}];
} else if (authStatus == AVAuthorizationStatusRestricted || authStatus == AVAuthorizationStatusDenied) {// 關(guān)閉狀態(tài)
} else {// 開啟狀態(tài)
}
#endif
4市殷、iOS開發(fā)檢測(cè)是否開啟相冊(cè):
需要導(dǎo)入:
#import <Photos/Photos.h>
#import <AssetsLibrary/AssetsLibrary.h>
代碼如下:
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_8_0
PHAuthorizationStatus authStatus = [PHPhotoLibrary authorizationStatus];
if (authStatus == PHAuthorizationStatusRestricted || authStatus == PHAuthorizationStatusDenied) {// 關(guān)閉狀態(tài)
}
#else
ALAuthorizationStatus author = [ALAssetsLibrary authorizationStatus];
if (author == ALAuthorizationStatusRestricted || author == ALAuthorizationStatusDenied) {// 關(guān)閉狀態(tài)
}
#endif
5、iOS開發(fā)檢測(cè)是否開啟麥克風(fēng):
需要導(dǎo)入:
#import <AVFoundation/AVFoundation.h>
代碼如下:
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_8_0
AVAudioSessionRecordPermission permissionStatus = [[AVAudioSession sharedInstance] recordPermission];
if (permissionStatus == AVAudioSessionRecordPermissionUndetermined) {
[[AVAudioSession sharedInstance] requestRecordPermission:^(BOOL granted) {
if (granted) {// 開啟狀態(tài)
}
}];
} else if (permissionStatus == AVAudioSessionRecordPermissionDenied) {// 關(guān)閉狀態(tài)
} else {// 開啟狀態(tài)
}
#endif
6刹衫、iOS開發(fā)檢測(cè)是否開啟通訊錄:
需要導(dǎo)入:
#import <AddressBook/AddressBook.h>
#import <Contacts/Contacts.h>
代碼如下:
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_9_0
CNAuthorizationStatus cnAuthStatus = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts];
if (cnAuthStatus == CNAuthorizationStatusNotDetermined) {
CNContactStore *store = [[CNContactStore alloc] init];
[store requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError *error) {
if (granted) {// 開啟狀態(tài)
}
}];
} else if (cnAuthStatus == CNAuthorizationStatusRestricted || cnAuthStatus == CNAuthorizationStatusDenied) {// 關(guān)閉狀態(tài)
} else {// 開啟狀態(tài)
}
#else
//ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL);
ABAddressBookRef addressBook = ABAddressBookCreate();
ABAuthorizationStatus authStatus = ABAddressBookGetAuthorizationStatus();
if (authStatus != kABAuthorizationStatusAuthorized) {// 關(guān)閉狀態(tài)
ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
dispatch_async(dispatch_get_main_queue(), ^{
if (error) {// 關(guān)閉狀態(tài)
NSLog(@"Error: %@", (__bridge NSError *)error);
} else {// 開啟狀態(tài)
}
});
});
} else {// 開啟狀態(tài)
}
#endif
7醋寝、iOS開發(fā)檢測(cè)是否開啟藍(lán)牙:
需要導(dǎo)入:
#import <CoreBluetooth/CoreBluetooth.h>
代碼如下:
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_7_0
CBPeripheralManagerAuthorizationStatus cbAuthStatus = [CBPeripheralManager authorizationStatus];
if (cbAuthStatus == CBPeripheralManagerAuthorizationStatusNotDetermined) {// 關(guān)閉狀態(tài)
} else if (cbAuthStatus == CBPeripheralManagerAuthorizationStatusRestricted || cbAuthStatus == CBPeripheralManagerAuthorizationStatusDenied) {// 關(guān)閉狀態(tài)
} else {// 開啟狀態(tài)
}
#endif
8、iOS開發(fā)檢測(cè)是否開啟日歷/備忘錄:
需要導(dǎo)入:
#import <EventKit/EventKit.h>
代碼如下:
// EKEntityTypeEvent 代表日歷
// EKEntityTypeReminder 代表備忘
EKAuthorizationStatus ekAuthStatus = [EKEventStore authorizationStatusForEntityType:entityType];
if (ekAuthStatus == EKAuthorizationStatusNotDetermined) {
EKEventStore *store = [[EKEventStore alloc] init];
[store requestAccessToEntityType:entityType completion:^(BOOL granted, NSError *error) {
if (granted) {// 開啟狀態(tài)
} else {// 關(guān)閉狀態(tài)
}
}];
} else if (ekAuthStatus == EKAuthorizationStatusRestricted || ekAuthStatus == EKAuthorizationStatusDenied) {// 關(guān)閉狀態(tài)
} else {// 開啟狀態(tài)
}
9带迟、iOS開發(fā)檢測(cè)是否開啟聯(lián)網(wǎng):
需要導(dǎo)入:
#import <CoreTelephony/CTCellularData.h>
代碼如下:
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_9_0
CTCellularData *cellularData = [[CTCellularData alloc] init];
cellularData.cellularDataRestrictionDidUpdateNotifier = ^(CTCellularDataRestrictedState state){
if (state == kCTCellularDataRestrictedStateUnknown || state == kCTCellularDataNotRestricted) {// 關(guān)閉狀態(tài)
} else {// 開啟狀態(tài)
}
};
CTCellularDataRestrictedState state = cellularData.restrictedState;
if (state == kCTCellularDataRestrictedStateUnknown || state == kCTCellularDataNotRestricted) {// 關(guān)閉狀態(tài)
} else {// 開啟狀態(tài)
}
#endif
10音羞、iOS開發(fā)檢測(cè)是否開啟健康:
需要導(dǎo)入:
#import <HealthKit/HealthKit.h>
代碼如下:
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_8_0
if (![HKHealthStore isHealthDataAvailable]) {// 關(guān)閉狀態(tài)
} else {
HKHealthStore *healthStore = [[HKHealthStore alloc] init];
HKObjectType *hkObjectType = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierHeight];
HKAuthorizationStatus hkAuthStatus = [healthStore authorizationStatusForType:hkObjectType];
if (hkAuthStatus == HKAuthorizationStatusNotDetermined) {
// 1. 你創(chuàng)建了一個(gè)NSSet對(duì)象,里面存有本篇教程中你將需要用到的從Health Stroe中讀取的所有的類型:個(gè)人特征(血液類型邮旷、性別黄选、出生日期)蝇摸、數(shù)據(jù)采樣信息(身體質(zhì)量婶肩、身高)以及鍛煉與健身的信息。
NSSet <HKObjectType *> * healthKitTypesToRead = [[NSSet alloc] initWithArray:@[[HKObjectType characteristicTypeForIdentifier:HKCharacteristicTypeIdentifierDateOfBirth],[HKObjectType characteristicTypeForIdentifier:HKCharacteristicTypeIdentifierBloodType],[HKObjectType characteristicTypeForIdentifier:HKCharacteristicTypeIdentifierBiologicalSex],[HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyMass],[HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierHeight],[HKObjectType workoutType]]];
// 2. 你創(chuàng)建了另一個(gè)NSSet對(duì)象貌夕,里面有你需要向Store寫入的信息的所有類型(鍛煉與健身的信息、BMI险毁、能量消耗畔况、運(yùn)動(dòng)距離)
NSSet <HKSampleType *> * healthKitTypesToWrite = [[NSSet alloc] initWithArray:@[[HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyMassIndex],[HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierActiveEnergyBurned],[HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDistanceWalkingRunning],[HKObjectType workoutType]]];
[healthStore requestAuthorizationToShareTypes:healthKitTypesToWrite readTypes:healthKitTypesToRead completion:^(BOOL success, NSError *error) {
if (success) {// 開啟狀態(tài)
}
}];
} else if (hkAuthStatus == HKAuthorizationStatusSharingDenied) {// 關(guān)閉狀態(tài)
} else {// 開啟狀態(tài)
}
}
#endif
11、iOS開發(fā)檢測(cè)是否開啟Touch ID:
需要導(dǎo)入:
#import <LocalAuthentication/LocalAuthentication.h>
代碼如下:
#pragma mark - 開啟Touch ID服務(wù)
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_8_0
LAContext *laContext = [[LAContext alloc] init];
laContext.localizedFallbackTitle = @"輸入密碼";
NSError *error;
if ([laContext canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]) {
NSLog(@"恭喜,Touch ID可以使用!");
[laContext evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:@"需要驗(yàn)證您的指紋來確認(rèn)您的身份信息" reply:^(BOOL success, NSError *error) {
if (success) {// 開啟狀態(tài)
} else if (error) {// 關(guān)閉狀態(tài)
if (error.code == LAErrorAuthenticationFailed) {
// 驗(yàn)證失敗
}
if (error.code == LAErrorUserCancel) {
// 用戶取消
}
if (error.code == LAErrorUserFallback) {
// 用戶選擇輸入密碼
}
if (error.code == LAErrorSystemCancel) {
// 系統(tǒng)取消
}
if (error.code == LAErrorPasscodeNotSet) {
// 密碼沒有設(shè)置
}
}
}];
} else {
NSLog(@"設(shè)備不支持Touch ID功能,原因:%@",error);
}
#endif
12吵瞻、iOS開發(fā)檢測(cè)是否開啟Apple Pay:
需要導(dǎo)入:
#import <PassKit/PassKit.h>
代碼如下:
#pragma mark - 開啟Apple Pay服務(wù)
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_9_0
NSArray<PKPaymentNetwork> *supportedNetworks = @[PKPaymentNetworkAmex, PKPaymentNetworkMasterCard, PKPaymentNetworkVisa, PKPaymentNetworkDiscover];
if ([PKPaymentAuthorizationViewController canMakePayments] && [PKPaymentAuthorizationViewController canMakePaymentsUsingNetworks:supportedNetworks]) {// 開啟狀態(tài)
} else {// 關(guān)閉狀態(tài)
}
#endif
13眯停、iOS開發(fā)檢測(cè)是否開啟語音識(shí)別:
需要導(dǎo)入:
#import <Speech/Speech.h>
代碼如下:
#pragma mark - 開啟語音識(shí)別服務(wù)
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
SFSpeechRecognizerAuthorizationStatus speechAuthStatus = [SFSpeechRecognizer authorizationStatus];
if (speechAuthStatus == SFSpeechRecognizerAuthorizationStatusNotDetermined) {
[SFSpeechRecognizer requestAuthorization:^(SFSpeechRecognizerAuthorizationStatus status) {
if (status == SFSpeechRecognizerAuthorizationStatusAuthorized) {// 開啟狀態(tài)
} else {// 關(guān)閉狀態(tài)
}
}];
} else if (speechAuthStatus == SFSpeechRecognizerAuthorizationStatusAuthorized) {// 開啟狀態(tài)
} else {// 關(guān)閉狀態(tài)
}
#endif
14莺债、iOS開發(fā)檢測(cè)是否開啟媒體資料庫/Apple Music:
需要導(dǎo)入:
#import <MediaPlayer/MediaPlayer.h>
代碼如下:
#pragma mark - 開啟媒體資料庫/Apple Music 服務(wù)
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_9_3
MPMediaLibraryAuthorizationStatus authStatus = [MPMediaLibrary authorizationStatus];
if (authStatus == MPMediaLibraryAuthorizationStatusNotDetermined) {
[MPMediaLibrary requestAuthorization:^(MPMediaLibraryAuthorizationStatus status) {
if (status == MPMediaLibraryAuthorizationStatusAuthorized) {// 開啟狀態(tài)
}else{// 關(guān)閉狀態(tài)
}
}];
}else if (authStatus == MPMediaLibraryAuthorizationStatusAuthorized){// 開啟狀態(tài)
}else{// 關(guān)閉狀態(tài)
}
#endif
15、iOS開發(fā)檢測(cè)是否開啟Siri:
需要導(dǎo)入:
#import <Intents/Intents.h>
代碼如下:
#pragma mark - 開啟Siri服務(wù)
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
INSiriAuthorizationStatus siriAutoStatus = [INPreferences siriAuthorizationStatus];
if (siriAutoStatus == INSiriAuthorizationStatusNotDetermined) {
[INPreferences requestSiriAuthorization:^(INSiriAuthorizationStatus status) {
if (status == INSiriAuthorizationStatusAuthorized) {// 開啟狀態(tài)
} else {// 關(guān)閉狀態(tài)
}
}];
} else if (siriAutoStatus == INSiriAuthorizationStatusAuthorized) {// 開啟狀態(tài)
} else{// 關(guān)閉狀態(tài)
}
#endif
由于iOS 10的權(quán)限原因侄旬,需要在工程的info.plist(右擊選擇Open as - Source Code)中添加
<!-- 相冊(cè) -->
<key>NSPhotoLibraryUsageDescription</key>
<string>App需要您的同意,才能訪問相冊(cè)</string>
<!-- 相機(jī) -->
<key>NSCameraUsageDescription</key>
<string>App需要您的同意,才能訪問相機(jī)</string>
<!-- 麥克風(fēng) -->
<key>NSMicrophoneUsageDescription</key>
<string>App需要您的同意,才能訪問麥克風(fēng)</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>
<!-- 提醒事項(xiàng) -->
<key>NSRemindersUsageDescription</key>
<string>App需要您的同意,才能訪問提醒事項(xiàng)</string>
<!-- 運(yùn)動(dòng)與健身 -->
<key>NSMotionUsageDescription</key>
<string>App需要您的同意,才能訪問運(yùn)動(dòng)與健身</string>
<!-- 健康更新 -->
<key>NSHealthUpdateUsageDescription</key>
<string>App需要您的同意,才能訪問健康更新 </string>
<!-- 健康分享 -->
<key>NSHealthShareUsageDescription</key>
<string>App需要您的同意,才能訪問健康分享</string>
<!-- 藍(lán)牙 -->
<key>NSBluetoothPeripheralUsageDescription</key>
<string>App需要您的同意,才能訪問藍(lán)牙</string>
<!-- 媒體資料庫 -->
<key>NSAppleMusicUsageDescription</key>
<string>App需要您的同意,才能訪問媒體資料庫</string>
<!-- 語音識(shí)別 -->
<key>NSSpeechRecognitionUsageDescription</key>
<string>App需要您的同意,才能使用語音識(shí)別</string>