前面我们已经学会了iOS二维码生成相关的知识,这篇文章主要是讲解一下iOS二维码扫描相关的知识.
二维码扫描步骤
- 导入
#import <AVFoundation/AVFoundation.h>
框架 - 遵守
AVCaptureMetadataOutputObjectsDelegate
协议 - info.plist 添加 访问相机
<key>NSCameraUsageDescription</key>
<string>访问相机</string>
添加振动,和音效功能.
- 引用
#import <AudioToolbox/AudioToolbox.h>
框架
#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);//让手机震动
}];
}
}
网友评论