扫描二维码,并跳转到所扫描的界面

#import "ViewController.h"
#import [AVFoundation/AVFoundation.h]
#import // 生成二维码

@interface ViewController ()

@property (nonatomic, strong) AVCaptureSession *session;
@property (nonatomic, strong) AVCaptureVideoPreviewLayer *previewLayer;

@end

@implementation ViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
    // Do any additional setup after loading the view, typically from a nib.
    
    // 1. 实例化拍摄设备
    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    NSError *error = nil;
    
    // 2. 设置输入设备
    AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];
    
    // 此处判断,否则在模拟器上会导致崩溃
    if (error) {
        NSLog(@"没有摄像头");
        return;
    }
    
    // 3. 设置原数据输出
    // 3.1 实例化拍摄原数据输出
    AVCaptureMetadataOutput *output = [[AVCaptureMetadataOutput alloc] init];
    // 3.2 设置输出数据代理
    [output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
    
    // 4. 添加拍摄会话
    // 4.1 实例化拍摄会话
    AVCaptureSession *session = [[AVCaptureSession alloc] init];
    // 4.2 添加会话输入
    [session addInput:input];
    // 4.3 添加会话输出
    [session addOutput:output];
    // 4.3 设置输出数据类型, 需要将原数据输出添加到会话后, 才能指定原数据类型, 否则会报错
    [output setMetadataObjectTypes:@[AVMetadataObjectTypeQRCode]];
    
    self.session = session;
    
    // 5. 视频预览图层
    // 5.1 实例化预览图层,传递_session 是为了告诉图层将来显示什么内容
    AVCaptureVideoPreviewLayer *preview = [AVCaptureVideoPreviewLayer layerWithSession:_session];
    
    preview.videoGravity = AVLayerVideoGravityResizeAspectFill;
    preview.frame = self.view.bounds;
    
    // 5.2 将图层插入当前视图
    [self.view.layer insertSublayer:preview atIndex:100];
    self.previewLayer = preview;
    
    // 6. 启动会话
    [_session startRunning];
    
//    [self turnWebViewstr:@"http://www.baidu.com"];
}


// 扫到二维码时调用的方法
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection
{
    // 会频繁的扫描,调用代理方法
    // 1. 如果扫描完成,停止会话
    [self.session stopRunning];
    
    // 2. 删除预览图层
    [self.previewLayer removeFromSuperlayer];
    NSLog(@"%@", metadataObjects);
    
    // 3. 设置界面显示扫描结果
    if (metadataObjects.count > 0 ) {
        AVMetadataMachineReadableCodeObject *obj = metadataObjects[0];
        NSLog(@"%@",obj.stringValue);
        
        [self turnWebViewstr:obj.stringValue];
//        [self showAlert:obj.stringValue];
    }
}

- (void)turnWebViewstr:(NSString *)str
{
    UIViewController *webC = [[UIViewController alloc]init];
    UIWebView *webView = [[UIWebView alloc]init];
    //    webView.delegate = self;
    
    NSURL *url = [NSURL URLWithString:str];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    
    webView.frame = CGRectMake(0, 0, 375, 667 );
    [webView loadRequest:request];
    webView.autoresizingMask = UIViewAutoresizingFlexibleWidth| UIViewAutoresizingFlexibleHeight;
    webView.scalesPageToFit = YES;
    webC.view = webView;
    [self.navigationController pushViewController:webC animated:YES];// 注意此处是 navigationcontroller 控制器 push 过去而不是 viewcontroller
    self.navigationController.navigationBarHidden = NO;
}


//根据NSTimer来给AlertView定时, 让显示完后消失
+ (void)timerFireMethod:(NSTimer*)theTimer//弹出框
{
    UIAlertView *promptAlert = (UIAlertView*)[theTimer userInfo];
    [promptAlert dismissWithClickeonIndex:0 animated:NO];
    promptAlert =NULL;
    
}

//弹出AlertView
- (void)showAlert:(NSString *) _message
{//时间
    UIAlertView *promptAlert = [[UIAlertView alloc] initWithTitle:@"提示:" message:_message delegate:nil cancelButtonTitle:nil otherButtonTitles:nil];
    
    [NSTimer scheduledTimerWithTimeInterval:1.0f
                                     target:self
                                   selector:@selector(timerFireMethod:)
                                   userInfo:promptAlert
                                    repeats:YES];
    [promptAlert show];
}

你可能感兴趣的:(扫描二维码,扫描二维码后跳转到相应界面,iOS开发二维码)