须要倒入一个框架 AVFoundationweb
1.须要有输入设备, 数组
2.输出设备 (解析输入设备的内容)浏览器
3.有一个会话类(输入输出完事了须要哪个是输入那一个是输出)session
AVCaptureSession 框架
4。一个展现扫描的内容 layeride
二 : 而后建立学习
三:设置代理 --获取数据优化
会话类不知道哪个是时输入设备 哪个是 输出设备的 须要衔接她们atom
去绑定输入和输出设备 (作一个是否的判断)、spa
四:设置扫描类型
五:开始扫描
[self.session startRunning]
六:完成代理方法去处理二维码
扫描到的是一个数据数组
还须要去找到一个类(是一个类型的) (细节头文件是没有提示的) 跳到方法里去
获取到了数据 里面有两个属性 obj.stringvalue
七:在iOS9里面又一个特殊服务 能够不用加载webview 能够方便展现网页
细节:(自带一相似导航栏的东西 完成 刷新 还带有分享 也就是系统真正的浏览器)
好处:网页过长的时候 在滚动的过程当中是没有那个tabbar和导航栏的
只有在最上面的时候才会显示那个的tabbar和导航栏
若是你的SFSafariViewController
//
// GASScanViewController.m
// GasManger
//
// Created by Ninesday on 16/6/1.
// Copyright © 2016年 Ninesday. All rights reserved.
//
#import "GASScanViewController.h"
#import <AVFoundation/AVFoundation.h>
#import <SafariServices/SafariServices.h>
@interface GASScanViewController ()<AVCaptureMetadataOutputObjectsDelegate>
//1.输入设备
@property (nonatomic,strong)AVCaptureDeviceInput *input;
//2.输出设备
@property (nonatomic,strong)AVCaptureMetadataOutput *output;
//3.会话类
@property (nonatomic,strong) AVCaptureSession *session;
//4.扫描到内容的layer
@property (nonatomic,strong) AVCaptureVideoPreviewLayer *previewlayer;
@end
@implementation GASScanViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
//输入设备
//得先建立设备
AVCaptureDevice * device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
self.input = [[AVCaptureDeviceInput alloc]initWithDevice:device error:nil];
//输出设备
self.output = [AVCaptureMetadataOutput new];
//会话类
self.session = [AVCaptureSession new];
//绑定设备
if ([self.session canAddInput:self.input]) {
[self.session addInput:self.input];
}
if ([self.session canAddOutput:self.output]) {
[self.session addOutput:self.output];
}
//设置代理获取数据
[self.output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
//设置扫描的类型
[self.output setMetadataObjectTypes:@[AVMetadataObjectTypeQRCode]];
//扫描到内容layer--须要设置会话
self.previewlayer = [[AVCaptureVideoPreviewLayer alloc]initWithSession:self.session];
self.previewlayer.frame = self.view.bounds;
[self.view.layer addSublayer:self.previewlayer];
//开始扫描
[self.session startRunning];
}
//- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
//}
-(void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection {
if (metadataObjects.count >0) {
//1.中止扫描
[self.session stopRunning];
//删除layer
[self.previewlayer removeFromSuperlayer];
//获取数据 AVMetadataMachineReadableCodeObject 头文件没提示
AVMetadataMachineReadableCodeObject *obj = metadataObjects[0];
NSLog(@"%@", obj.stringValue);