- (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; }