- (void)setupCamera { dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ // 耗时的操做 // Device _device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; // Input _input = [AVCaptureDeviceInput deviceInputWithDevice:self.device error:nil]; // Output _output = [[AVCaptureMetadataOutput alloc]init]; // [_output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()]; [_output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()]; // Session _session = [[AVCaptureSession alloc]init]; [_session setSessionPreset:AVCaptureSessionPresetHigh]; if ([_session canAddInput:self.input]) { [_session addInput:self.input]; } if ([_session canAddOutput:self.output]) { [_session addOutput:self.output]; } // 条码类型 AVMetadataObjectTypeQRCode _output.metadataObjectTypes =@[AVMetadataObjectTypeQRCode]; dispatch_async(dispatch_get_main_queue(), ^{ // 更新界面 // Preview _preview =[AVCaptureVideoPreviewLayer layerWithSession:self.session]; _preview.videoGravity = AVLayerVideoGravityResizeAspectFill; // _preview.frame =CGRectMake(20,110,280,280); _preview.frame = self.view.bounds; [self.view.layer insertSublayer:self.preview atIndex:0]; // Start [_session startRunning]; }); }); }
-(void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; if (_session && ![_session isRunning]) { [_session startRunning]; } timer = [NSTimer scheduledTimerWithTimeInterval:.02 target:self selector:@selector(animation1) userInfo:nil repeats:YES]; } - (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; [timer invalidate]; }
以上timer是个扫描动画的计时器,能够略过不看。javascript
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection { NSString *stringValue; if ([metadataObjects count] >0) { AVMetadataMachineReadableCodeObject * metadataObject = [metadataObjects objectAtIndex:0]; stringValue = metadataObject.stringValue; } [_session stopRunning]; [timer invalidate]; NSLog(@"%@",stringValue); }
NSString *url = [NSURL URLWithString:@"html/judgement.html" relativeToURL:[ZXApiClient sharedClient].baseURL].absoluteString; if ([stringValue hasPrefix:url]) { //若是扫出来的url是本身的域名开头的,那么作以下的处理。 }
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:stringValue]];
直接使用openUrl系统自带的浏览器打开url就行,或者本身写个内置的浏览器打开。html
首先将本身的二维码定义成http://www.xxx.com/xxxxx这样的本身域名的url。java
那么第三方的二维码扫出来后,会跳向这个网址。android
其次在服务器上部署这个页面,加入以下的代码浏览器
<script language="javascript"> if (navigator.userAgent.match(/(iPhone|iPod|iPad);?/i)) { var loadDateTime = new Date(); window.setTimeout(function() { var timeOutDateTime = new Date(); if (timeOutDateTime - loadDateTime < 5000) { window.location = "要跳转的页面URL"; } else { window.close(); } }, 25); window.location = " test:// "; } else if (navigator.userAgent.match(/android/i)) { var state = null; try { state = window.open("apps custom url schemes ", '_blank'); } catch(e) {} if (state) { window.close(); } else { window.location = "要跳转的页面URL"; } } </script>
这段代码是基于url schemes的原理,若是你的app里存在这个url schemes(例子里是test://),那么会马上打开这个url,若是不存在,就会超过25毫秒,那么就指向另外一个页面,通常是下载页。服务器
接着,在app的url schemes里设置,好比test微信
Paste_Image.pngsession
这个时候,浏览器发出test://的请求的时候,就能马上打开这个app了。app
最后,若是不知足于扫描二维码只能打开app,想对二维码里的内容作一些操做的话,能够:async
将二维码的内容定义成http://www.xxx.com/xxxxx?uid=xxx这样,固然后面的参数须要加密。
在js代码里获取这个参数,并原封不动的附加在url schemes后面,如test://uid=xxx。
在appDelegate里加上以下代码。
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url { if ([url.absoluteString hasPrefix:@"test://uid="]) { NSString *uid = [url.absoluteString substringFromIndex:11]; NSLog(@"uid=%@",uid); //对uid进行操做 } else { //其余的地方抛过来的url,好比微信 return [WXApi handleOpenURL:url delegate:self]; } return YES; }