iOS二维码扫描

iOS二维码扫描_第1张图片
iOS二维码扫描.png

前面我们已经学会了iOS二维码生成相关的知识,这篇文章主要是讲解一下iOS二维码扫描相关的知识.

二维码扫描步骤

  1. 导入#import 框架
  2. 遵守AVCaptureMetadataOutputObjectsDelegate协议
  3. info.plist 添加 访问相机
    NSCameraUsageDescription
    访问相机

添加振动,和音效功能.

  • 引用#import 框架
#pragma mark - AVCaptureMetadataOutputObjectsDelegate
-(void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection
{
    if (metadataObjects.count > 0)
    {
        [self.session stopRunning];
        
        AVMetadataMachineReadableCodeObject *metadataObject = [metadataObjects firstObject];
        
        [self showAlertWithTitle:@"扫描结果" message:metadataObject.stringValue handler:^(UIAlertAction *action) {
            [self.session startRunning];
            //播放扫描二维码的声音
            NSString *path = [[NSBundle mainBundle] pathForResource:@"noticeMusic" ofType:@"wav"];
            static SystemSoundID shake_sound_male_id = 0;
            if (path) {
                //注册声音到系统
                AudioServicesCreateSystemSoundID((__bridge CFURLRef _Nonnull)[NSURL fileURLWithPath:path],&shake_sound_male_id);
            }
            AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);//让手机震动
        }];
    }
}

开启闪光灯

你可能感兴趣的:(iOS二维码扫描)