IT虾米网

iOS判断手机是否开启麦克风

luoye 2018年06月23日 手机开发 1275 0
//判断是否允许使用麦克风7.0新增的方法requestRecordPermission 
-(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] show]; 
                    }); 
                } 
            }]; 
        } 
    } 
     
    return bCanRecord; 
}


评论关闭
IT虾米网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!