iOS开发实战:获取手机各种权限状态

获取权限状态前,需要先在plist文件添加权限语句,有的还需要在Capabilities设置里打开对应开关。

以下是可能用到的库


下面是具体实现代码

//充电状态

+ (BOOL)NativePhoneBatteryState

{

    //打开电池的监听

    [UIDevice currentDevice].batteryMonitoringEnabled = YES;

    /*

     //battery的状态


     [UIDevice currentDevice].batteryState分为:




     UIDeviceBatteryStateUnknown,    未知


     UIDeviceBatteryStateUnplugged,  // 未充电


     UIDeviceBatteryStateCharging,    // 正在充电


     UIDeviceBatteryStateFull,            // 满电

     */

    //获取电池的状态

    UIDeviceBatteryState BatteryState = [UIDevice currentDevice].batteryState;

    switch(BatteryState) {

        case UIDeviceBatteryStateUnknown:

            returnNO;

            break;


        case UIDeviceBatteryStateCharging:

            returnYES;

            break;

        case UIDeviceBatteryStateFull:

            returnNO;

            break;


        default:

            returnNO;

            break;

    }


}

//网络状态

+ (BOOL)NativeGetNetworkState

{


    Reachability *reach = [Reachability reachabilityWithHostName:@"www.baidu.com"];

    switch ([reach currentReachabilityStatus]) {

        case NotReachable:

            returnNO;

            break;


        case ReachableViaWiFi:

            returnYES;

            break;

        case ReachableViaWWAN:


            returnYES;

            break;


        default:

            returnNO;

            break;

    }

}

//相册状态

+ (BOOL)NativeGetPhonePhoto{

    PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];

    if (status == PHAuthorizationStatusRestricted || status == PHAuthorizationStatusDenied || status == PHAuthorizationStatusNotDetermined)

    {

        returnNO;

    }else{

        returnYES;

    }

}

//相机状态

+ (BOOL)NativeGetPhoneCamera{

    AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];

    if (status == AVAuthorizationStatusRestricted || status == AVAuthorizationStatusDenied || status == AVAuthorizationStatusNotDetermined)

    {

        returnNO;

    }else{

        returnYES;

    }

}

//定位状态

+ (BOOL)NativeGetPhoneLocation{

    if([CLLocationManager authorizationStatus] ==kCLAuthorizationStatusDenied || [CLLocationManager authorizationStatus] ==kCLAuthorizationStatusNotDetermined || [CLLocationManager authorizationStatus] ==kCLAuthorizationStatusRestricted) {


        returnNO;

    }else{

        returnYES;

    }

}

//麦克风状态

+ (BOOL)NativeGetPhoneMicrophone{

    AVAuthorizationStatus statusAudio = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio];

    if(statusAudio == AVAuthorizationStatusDenied || statusAudio == AVAuthorizationStatusRestricted || statusAudio == AVAuthorizationStatusNotDetermined)

    {

        returnNO;

    }else{

        returnYES;

    }

}

//通讯录状态

+ (BOOL)NativeGetPhoneContacts{


    CNAuthorizationStatus status = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts];

    switch(status) {

        case CNAuthorizationStatusNotDetermined:

           returnNO;

            break;

        case CNAuthorizationStatusRestricted:

            returnNO;

            break;


        case CNAuthorizationStatusDenied:

            returnNO;

            break;

        case CNAuthorizationStatusAuthorized:

            returnYES;

            break;


        default:

            returnNO;

            break;

    }


}

//蓝牙状态

+ (BOOL)NativeGetPhoneBlue{

    CBPeripheralManagerAuthorizationStatus authStatus = [CBPeripheralManager authorizationStatus];

    switch(authStatus) {

        case CBPeripheralManagerAuthorizationStatusNotDetermined:

            returnNO;

            break;

        case CBPeripheralManagerAuthorizationStatusRestricted:

            returnNO;

            break;


        case CBPeripheralManagerAuthorizationStatusDenied:

            returnNO;

            break;

        case CBPeripheralManagerAuthorizationStatusAuthorized:

            returnYES;

            break;


        default:

            returnNO;

            break;

    }

}

//日历状态

+ (BOOL)NativeGetPhoneCalendars{

    EKAuthorizationStatus status = [EKEventStore authorizationStatusForEntityType:EKEntityTypeEvent];

    switch(status) {

        case EKAuthorizationStatusNotDetermined:

            returnNO;

            break;

        case EKAuthorizationStatusRestricted:

            returnNO;

            break;


        case EKAuthorizationStatusDenied:

            returnNO;

            break;

        case EKAuthorizationStatusAuthorized:

            returnYES;

            break;


        default:

            returnNO;

            break;

    }

}

// Face ID状态

+ (BOOL)NativeGetPhoneFaceID{

    if ([UIDevice currentDevice].systemVersion.floatValue < 11.0f) {

        returnNO;

    }

    LAContext*authenticationContext = [[LAContextalloc]init];

    NSError*error =nil;

   staticBOOLfaceBool =NO;

    BOOL canEvaluatePolicy = [authenticationContext canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error];

    if(canEvaluatePolicy) {

        if(authenticationContext.biometryType==LABiometryTypeFaceID) {

            [authenticationContextevaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:@"" reply:^(BOOL success, NSError * _Nullable error) {

                if(error) {}else{

                    if(success) {

                        faceBool =YES;

                    }else{

                        faceBool =NO;

                    }

                }

            }];


        }else{

            faceBool =NO;

        }

    }

    returnfaceBool;


}

//健康分享状态

+ (BOOL)NativeGetPhoneHealthShare{

    if ([HKHealthStore isHealthDataAvailable]) {

        HKHealthStore *healthStore = [[HKHealthStore alloc] init];

        HKQuantityType *heartRateType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierHeartRate];

        HKAuthorizationStatusstatus = [healthStoreauthorizationStatusForType:heartRateType];


        switch(status) {

            case HKAuthorizationStatusNotDetermined:

                returnNO;

                break;

            case HKAuthorizationStatusSharingDenied:

                returnNO;

                break;

            case HKAuthorizationStatusSharingAuthorized:

                returnYES;

                break;


            default:

                returnNO;

                break;

        }

    }else{

        returnNO;

    }


}

//媒体资料库状态

+ (BOOL)NativeGetPhoneAppleMusic{

    if ([UIDevice currentDevice].systemVersion.floatValue < 9.3f) {

       returnNO;

    }


    SKCloudServiceAuthorizationStatus status = [SKCloudServiceController authorizationStatus];

    switch(status) {

        case SKCloudServiceAuthorizationStatusNotDetermined:

            returnNO;

            break;

        case SKCloudServiceAuthorizationStatusRestricted:

            returnNO;

            break;


        case SKCloudServiceAuthorizationStatusDenied:

            returnNO;

            break;

        case SKCloudServiceAuthorizationStatusAuthorized:

            returnYES;

            break;


        default:

            returnNO;

            break;

    }

}

//提醒事项状态

+ (BOOL)NativeGetPhoneReminders{

    EKAuthorizationStatus status = [EKEventStore authorizationStatusForEntityType:EKEntityTypeReminder];


    switch(status) {

        case EKAuthorizationStatusNotDetermined:

            returnNO;

            break;

        case EKAuthorizationStatusRestricted:

            returnNO;

            break;


        case EKAuthorizationStatusDenied:

            returnNO;

            break;

        case EKAuthorizationStatusAuthorized:

            returnYES;

            break;


        default:

            returnNO;

            break;

    }

}

//Siri状态

+ (BOOL)NativeGetPhoneSiri{

    if ([UIDevice currentDevice].systemVersion.floatValue < 10.0f) {

       returnNO;

    }

    INSiriAuthorizationStatus status = [INPreferences siriAuthorizationStatus];

    switch(status) {

        case INSiriAuthorizationStatusNotDetermined:

            returnNO;

            break;

        case EKAuthorizationStatusRestricted:

            returnNO;

            break;


        case INSiriAuthorizationStatusDenied:

            returnNO;

            break;

        case INSiriAuthorizationStatusAuthorized:

            returnYES;

            break;


        default:

            returnNO;

            break;

    }


}

//语音识别状态

+ (BOOL)NativeGetPhoneSpeechRecognition{

    if ([UIDevice currentDevice].systemVersion.floatValue < 10.0f) {

       returnNO;

    }

    SFSpeechRecognizerAuthorizationStatus status = [SFSpeechRecognizer authorizationStatus];

    switch(status) {

        case SFSpeechRecognizerAuthorizationStatusNotDetermined:

            returnNO;

            break;

        case SFSpeechRecognizerAuthorizationStatusRestricted:

            returnNO;

            break;


        case SFSpeechRecognizerAuthorizationStatusDenied:

            returnNO;

            break;

        case SFSpeechRecognizerAuthorizationStatusAuthorized:

            returnYES;

            break;


        default:

            returnNO;

            break;

    }

}

//推送状态

+ (BOOL)NativeGetPhonepush{

    return [UIApplication sharedApplication].isRegisteredForRemoteNotifications;;

}

你可能感兴趣的:(iOS开发实战:获取手机各种权限状态)