2018-07-02 10:29:28 a18339063397 阅读数 2062
  • 管理员登录

    熟练掌握前后端交互的安全控制 熟练掌握JWT结合微服务的使用 熟练掌握JWT与微服务zuul组件的整合

    27599课时 0分钟 2人学习 崔海焘
    免费试看

1.判断用户是否有权限访问相册

#import <AssetsLibrary/AssetsLibrary.h>
ALAuthorizationStatus author =[ALAssetsLibrary authorizationStatus];
if (author == kCLAuthorizationStatusRestricted || author ==kCLAuthorizationStatusDenied)
{
       //无权限
}

可以注意一下下面这个

typedef enum {
      kCLAuthorizationStatusNotDetermined = 0, // 用户尚未做出选择这个应用程序的问候
      kCLAuthorizationStatusRestricted,        // 此应用程序没有被授权访问的照片数据。可能是家长控制权限
      kCLAuthorizationStatusDenied,            // 用户已经明确否认了这一照片数据的应用程序访问
      kCLAuthorizationStatusAuthorized         // 用户已经授权应用访问照片数据
} CLAuthorizationStatus;

iOS 8 之后推荐用 #import <Photos/Photos.h> 中的判断

PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
if (status == PHAuthorizationStatusRestricted ||
    status == PHAuthorizationStatusDenied) {
    return NO;
}
return YES;
typedef NS_ENUM(NSInteger, PHAuthorizationStatus) {
    PHAuthorizationStatusNotDetermined = 0, // 默认还没做出选择
    PHAuthorizationStatusRestricted,        // 此应用程序没有被授权访问的照片数据
    PHAuthorizationStatusDenied,            // 用户已经明确否认了这一照片数据的应用程序访问
    PHAuthorizationStatusAuthorized         //  用户已经授权应用访问照片数据
} NS_AVAILABLE_IOS(8_0);
推荐使用
- (BOOL)isCanUsePhotos {

    if ([[[UIDevice currentDevice] systemVersion] floatValue] < 8.0) {
        ALAuthorizationStatus author =[ALAssetsLibrary authorizationStatus];
        if (author == kCLAuthorizationStatusRestricted || author == kCLAuthorizationStatusDenied) {
            //无权限
            return NO;
        }
    }
    else {
        PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
        if (status == PHAuthorizationStatusRestricted ||
            status == PHAuthorizationStatusDenied) {
            //无权限
            return NO;
        }
    }
    return YES;
}
2015-06-10 17:14:36 Mideveloper 阅读数 17895
  • 管理员登录

    熟练掌握前后端交互的安全控制 熟练掌握JWT结合微服务的使用 熟练掌握JWT与微服务zuul组件的整合

    27599课时 0分钟 2人学习 崔海焘
    免费试看

1.判断用户是否有权限访问相册


#import <AssetsLibrary/AssetsLibrary.h>

ALAuthorizationStatus author = [ALAssetsLibraryauthorizationStatus];
    if (author == kCLAuthorizationStatusRestricted || author ==kCLAuthorizationStatusDenied){
        //无权限
    }
    typedef enum {
        kCLAuthorizationStatusNotDetermined = 0, // 用户尚未做出选择这个应用程序的问候
        kCLAuthorizationStatusRestricted,        // 此应用程序没有被授权访问的照片数据。可能是家长控制权限
        kCLAuthorizationStatusDenied,            // 用户已经明确否认了这一照片数据的应用程序访问
        kCLAuthorizationStatusAuthorized         // 用户已经授权应用访问照片数据} CLAuthorizationStatus;
    }




2.判断用户是否有权限访问相机


iOS7之前都可以访问相机,iOS7之后访问相机有权限设置


#import <AVFoundation/AVCaptureDevice.h>

#import <AVFoundation/AVMediaFormat.h>

    AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
    if (authStatus == AVAuthorizationStatusRestricted || authStatus ==AVAuthorizationStatusDenied)
    {
        //无权限
    }



3.判断是否开启定位服务

    [CLLocationManager locationServicesEnabled] //检测的是整个的iOS系统的定位服务是否开启检测当前应用的定位服务是否开启需要通过一下方法来检测
    - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error

http://www.2cto.com/kf/201405/304909.html


2018-07-31 16:49:37 FlyingFireFish 阅读数 2026
  • 管理员登录

    熟练掌握前后端交互的安全控制 熟练掌握JWT结合微服务的使用 熟练掌握JWT与微服务zuul组件的整合

    27599课时 0分钟 2人学习 崔海焘
    免费试看

AppDelegate中的方法

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    
        //MARK: APP启动时候,判断用户是否授权使用相机
        if (AVCaptureDevice.authorizationStatus(for: AVMediaType.video) == .notDetermined) {
            AVCaptureDevice.requestAccess(for: .video, completionHandler: { (statusFirst) in
                if statusFirst {
                    //用户首次允许
                    print("允许APP访问相机")
                } else {
                    //用户首次拒接
                    print("拒绝APP访问相机")
                }
            })
        }
       
        //MARK: APP启动时候,判断用户是否授权使用相册
        if (PHPhotoLibrary.authorizationStatus() == .notDetermined) {
            PHPhotoLibrary.requestAuthorization({ (firstStatus) in
                let result = (firstStatus == .authorized)
                if result {
                    print("允许APP访问相册")
                } else {
                    print("拒绝APP访问相册")
                }
            })
        }
        
        return true
    }

相机、相册权限判断

/****************************APP权限**************************************/
/* .restricted     ---> 受限制,系统原因,无法访问
 * .notDetermined  ---> 系统还未知是否访问,第一次开启时
 * .authorized     ---> 允许、已授权
 * .denied         ---> 受限制,系统原因,无法访问
 */
//MARK: 判断是否可访问相册
//UIDevice.current.systemVersion >= 8.0
///相册权限---> 直接使用 <Photo方法 @available(iOS 8.0, *)>
func photoEnable() -> Bool {
    func photoResult() {
        let status = PHPhotoLibrary.authorizationStatus()
        if (status == .authorized) {
            savePhoto(value: "1")
        }
        else if (status == .restricted || status == .denied) {
            let alertV = UIAlertView.init(title: "提示", message: "请去-> [设置 - 隐私 - 相册] 打开访问开关", delegate: nil, cancelButtonTitle: nil, otherButtonTitles: "确定")
            alertV.show()
            savePhoto(value: "0")
        }
        else if (status == .notDetermined) {//首次使用
            PHPhotoLibrary.requestAuthorization({ (firstStatus) in
                let isTrue = (firstStatus == .authorized)
                if isTrue {
                    print("首次允许")
                    savePhoto(value: "1")
                } else {
                    print("首次不允许")
                    savePhoto(value: "0")
                }
            })
        }
    }
    func savePhoto(value: String) {
        UserDefaults.standard.setValue(value, forKey: "photoEnablebs")
    }
    let result = (UserDefaults.standard.object(forKey: "photoEnablebs") as! String) == "1"
    return result
}

///相册权限 --> 需要AppleDelegate中让用户先选择授权
func photoEnableDelegate() -> Bool {
    let status = PHPhotoLibrary.authorizationStatus()
    if status == .authorized {
        return true
    }
    return false
}

//UIDevice.current.systemVersion < 8.0
//depressed
///相册权限---> 需要AppleDelegate中让用户先选择授权, 此方法将被废弃 <AssetsLibrary方法 @available(iOS 7.0, *)>
func photoEnableDelegate2() -> Bool {
    let author = ALAssetsLibrary.authorizationStatus()
    print("--\(author.rawValue)--")
    if (author == .restricted || author == .denied) {
        let alertV = UIAlertView.init(title: "提示", message: "请去-> [设置 - 隐私 - 相册] 打开访问开关", delegate: nil, cancelButtonTitle: nil, otherButtonTitles: "确定")
        alertV.show()
        return false
    }
    return true
}

//MARK: 判断是否可访问相机
///相机权限 ---> 直接调用
func cameraEnable() -> Bool {
    func cameraResult() {
        let authStatus = AVCaptureDevice.authorizationStatus(for: AVMediaType.video)
        
        if (authStatus == .authorized) { /****已授权,可以打开相机****/
            saveCamera(value: "1")
        }
            
        else if (authStatus == .denied) {
            saveCamera(value: "0")
            let alertV = UIAlertView.init(title: "提示", message: "请去-> [设置 - 隐私 - 相机] 打开访问开关", delegate: nil, cancelButtonTitle: nil, otherButtonTitles: "确定")
            alertV.show()
        }
            
        else if (authStatus == .restricted) {//相机权限受限
            saveCamera(value: "0")
            let alertV = UIAlertView.init(title: "提示", message: "相机权限受限", delegate: nil, cancelButtonTitle: nil, otherButtonTitles: "确定")
            alertV.show()
        }
            
        else if (authStatus == .notDetermined) {//首次 使用
            AVCaptureDevice.requestAccess(for: .video, completionHandler: { (statusFirst) in
                if statusFirst {
                    //用户首次允许
                    saveCamera(value: "1")
                } else {
                    //用户首次拒接
                    saveCamera(value: "0")
                }
            })
        }
    }
    func saveCamera(value: String) {
        UserDefaults.standard.setValue(value, forKey: "cameraEnablebs")
    }
    cameraResult()
    let result = (UserDefaults.standard.value(forKey: "cameraEnablebs") as! String) == "1"
    return result
}

///相机权限2 --> 需要AppleDelegate中让用户先选择授权
func cameraEnableDelegate() -> Bool {
    let status = AVCaptureDevice.authorizationStatus(for: AVMediaType.video)
    if status == .authorized {
        return true
    }
    return false
}

/******************************************************************/

 

2015-09-06 11:29:01 jueyi1127 阅读数 7365
  • 管理员登录

    熟练掌握前后端交互的安全控制 熟练掌握JWT结合微服务的使用 熟练掌握JWT与微服务zuul组件的整合

    27599课时 0分钟 2人学习 崔海焘
    免费试看

在iOS应用中,我们经常要做各种权限的判断,以下是我整理的(相机,相册,定位,录音),希望对大家有所帮助。


1 相机权限

            NSString *mediaType = AVMediaTypeVideo;
            AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:mediaType];
            if(authStatus == ALAuthorizationStatusRestricted || authStatus == ALAuthorizationStatusDenied){
                //无权限
                NSString *tips = [NSString stringWithFormat:@"请在iPhone的”设置-隐私-相机“选项中,允许%@访问你的手机相机",NSLocalizedString(@"AppName",@"GMChatDemo")];
                [UIAlertView showWithTitle:@" " message:tips cancelButtonTitle:@"好" otherButtonTitles:nil tapBlock:nil];
                return;
            }



2 相册权限

            ALAuthorizationStatus author = [ALAssetsLibrary authorizationStatus];
            if (author == kCLAuthorizationStatusRestricted || author ==kCLAuthorizationStatusDenied){
                //无权限
                NSString *tips = [NSString stringWithFormat:@"请在iPhone的”设置-隐私-照片“选项中,允许%@访问你的照片",NSLocalizedString(@"AppName",@"GMChatDemo")];
                [UIAlertView showWithTitle:@" " message:tips cancelButtonTitle:@"好" otherButtonTitles:nil tapBlock:nil];
                return;
            }



3 麦克风权限(录音等)

+ (BOOL)canRecord
{
    __block BOOL bCanRecord = YES;
    if ([[[UIDevice currentDevice] systemVersion] compare:@"7.0"] != NSOrderedAscending)
    {
        AVAudioSession *audioSession = [AVAudioSession sharedInstance];
        if ([audioSession respondsToSelector:@selector(requestRecordPermission:)]) {
            [audioSession performSelector:@selector(requestRecordPermission:) withObject:^(BOOL granted) {
                bCanRecord = granted;
            }];
        }
    }
    return bCanRecord;
}



4 定位权限

    CLAuthorizationStatus status = [CLLocationManager authorizationStatus];
    if (kCLAuthorizationStatusDenied == status || kCLAuthorizationStatusRestricted == status) {
        [self showHint:NSLocalizedString(@"location.open","please open location")];
        return;
    }




2016-08-09 11:05:08 u013127850 阅读数 0
  • 管理员登录

    熟练掌握前后端交互的安全控制 熟练掌握JWT结合微服务的使用 熟练掌握JWT与微服务zuul组件的整合

    27599课时 0分钟 2人学习 崔海焘
    免费试看

1.判断用户是否有权限访问相册

#import <AssetsLibrary/AssetsLibrary.h>

ALAuthorizationStatus author = [ALAssetsLibraryauthorizationStatus];

if (author == kCLAuthorizationStatusRestricted || author ==kCLAuthorizationStatusDenied)

        {

           //无权限

        }


typedef enum {

    kCLAuthorizationStatusNotDetermined = 0// 用户尚未做出选择这个应用程序的问候

    kCLAuthorizationStatusRestricted,        // 此应用程序没有被授权访问的照片数据。可能是家长控制权限

    kCLAuthorizationStatusDenied,            // 用户已经明确否认了这一照片数据的应用程序访问

    kCLAuthorizationStatusAuthorized         // 用户已经授权应用访问照片数据

} CLAuthorizationStatus;


2.判断用户是否有权限访问相机

iOS7之前都可以访问相机,iOS7之后访问相机有权限设置

#import <AVFoundation/AVCaptureDevice.h>

#import <AVFoundation/AVMediaFormat.h>

AVAuthorizationStatus authStatus = [AVCaptureDeviceauthorizationStatusForMediaType:AVMediaTypeVideo];

if (authStatus == AVAuthorizationStatusRestricted || authStatus ==AVAuthorizationStatusDenied)

        {

            //无权限

        }


3.判断是否开启定位服务

[CLLocationManagerlocationServicesEnabled] //检测的是整个的iOS系统的定位服务是否开启

检测当前应用的定位服务是否开启需要通过一下方法来检测

- (void)locationManager:(CLLocationManager *)manager

       didFailWithError:(NSError *)error