ios7-录音权限访问-AVAudioSession

ios7新增api requestRecordPermission

api 说明:

- (void)requestRecordPermission:(PermissionBlock)response
Description    
Request the user’s permission for audio recording.
Recording audio requires explicit permission from the user. The system automatically prompts the user for permission if you use any session categories that enable recording, or you can call this method to prompt for permission at a time of your choosing. After the user grants or denies permission, the system remembers the choice for future use in the same app. If permission is not granted, or if the user has not yet responded to the permission prompt, any audio recording sessions record only silence.
The response parameter is a block of the PermissionBlock type, whose sole parameter indicates whether the user granted or denied permission to record. This method always returns immediately: if the user has previously granted or denied recording permission, it executes the block when called; otherwise, it displays an alert and executes the block only after the user has responded to the alert.
Parameters    
response    
A block to be called when recording permission has been granted or denied.
Availability    iOS (7.0 and later)



新增api,获取录音权限.

返回值,YES为无拒绝,NO为拒绝录音.

-(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) {
                if (granted) {
                    bCanRecord = YES;
                }
                else {
                    bCanRecord = NO;
                    dispatch_async(dispatch_get_main_queue(), ^{
                        [[[[UIAlertView alloc] initWithTitle:nil
                                                     message:@"app需要访问您的麦克风。\n请启用麦克风-设置/隐私/麦克风"
                                                    delegate:nil
                                           cancelButtonTitle:@"关闭"
                                           otherButtonTitles:nil] autorelease] show];
                    });
                }
            }];
        }
    }
    
    return bCanRecord;
}

你可能感兴趣的:(ios开发)