-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScanHashViewController.m
More file actions
178 lines (123 loc) · 5.5 KB
/
Copy pathScanHashViewController.m
File metadata and controls
178 lines (123 loc) · 5.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
//
// SettingsViewController.m
// DemoApp
//
// Created by Shimi Sheetrit on 2/3/16.
// Copyright © 2016 Matomy Media Group Ltd. All rights reserved.
//
#import "ScanHashViewController.h"
#import "AppDelegate.h"
#define MOBFOX_HASH_INTER @"267d72ac3f77a3f447b32cf7ebf20673"
@interface ScanHashViewController ()
@property (weak, nonatomic) IBOutlet UITextField *hashTextField;
@property (weak, nonatomic) IBOutlet UINavigationBar *navigationBar;
@property (weak, nonatomic) IBOutlet UIView *viewPreview;
@property (weak, nonatomic) IBOutlet UILabel *statusLabel;
@property (weak, nonatomic) IBOutlet UIButton *startButton;
@property (weak, nonatomic) IBOutlet UIBarButtonItem *doneButtonItem;
@property (nonatomic) BOOL isScannerReading;
@property (strong, nonatomic) AVCaptureSession *captureSession;
@property (strong, nonatomic) AVCaptureVideoPreviewLayer *videoPreviewLayer;
@property (strong, nonatomic) MobFoxInterstitialAd *mobfoxInterAd;
@property (nonatomic, assign) BOOL sendData;
@end
@implementation ScanHashViewController
@synthesize delegate;
- (void) hashUpdater : (NSString *) hash {
[self.delegate updateHashAfterScan:hash];
}
- (void)viewDidLoad {
[super viewDidLoad];
_sendData = NO;
self.isScannerReading = NO;
self.captureSession = nil;
self.startButton.layer.cornerRadius = 4.0;
[self.statusLabel setText:@"QR Code Reader is not yet running..."];
[self.view addGestureRecognizer:[[UITapGestureRecognizer alloc]
initWithTarget:self
action:@selector(dismissKeyboard)]];
}
-(void)dismissKeyboard {
[_hashTextField resignFirstResponder];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Navigation
- (IBAction)startStopReading:(id)sender {
NSLog(@"startStopReading");
if (!self.isScannerReading) {
if ([self startReading]) {
[self.startButton setTitle:@"Stop" forState:UIControlStateNormal];
[self.statusLabel setText:@"Scanning for QR Code..."];
}
}
else{
[self stopReading];
[self.startButton setTitle:@"Start!" forState:UIControlStateNormal];
[self.statusLabel setText:@"QR Code Reader is not running..."];
}
self.isScannerReading = !self.isScannerReading;
}
- (BOOL)startReading {
NSError *error;
AVCaptureDevice *captureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:captureDevice error:&error];
if (!input) {
NSLog(@"%@", [error localizedDescription]);
return NO;
}
self.captureSession = [[AVCaptureSession alloc] init];
[self.captureSession addInput:input];
AVCaptureMetadataOutput *captureMetadataOutput = [[AVCaptureMetadataOutput alloc] init];
[self.captureSession addOutput:captureMetadataOutput];
dispatch_queue_t dispatchQueue;
dispatchQueue = dispatch_queue_create("myQueue", NULL);
[captureMetadataOutput setMetadataObjectsDelegate:self queue:dispatchQueue];
[captureMetadataOutput setMetadataObjectTypes:[NSArray arrayWithObject:AVMetadataObjectTypeQRCode]];
self.videoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:self.captureSession];
[self.videoPreviewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
[self.videoPreviewLayer setFrame:self.viewPreview.layer.bounds];
[self.viewPreview.layer addSublayer:self.videoPreviewLayer];
[self.captureSession startRunning];
return YES;
}
-(void)stopReading{
[self.captureSession stopRunning];
self.captureSession = nil;
[self.videoPreviewLayer removeFromSuperlayer];
}
#pragma mark AVCaptureMetadataOutputObjectsDelegate
-(void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection{
if(_sendData == NO){
_sendData = YES;
if (metadataObjects != nil && [metadataObjects count] > 0) {
AVMetadataMachineReadableCodeObject *metadataObj = [metadataObjects objectAtIndex:0];
if ([[metadataObj type] isEqualToString:AVMetadataObjectTypeQRCode]) {
[_statusLabel performSelectorOnMainThread:@selector(setText:) withObject:@"QR Code Reader is not running..." waitUntilDone:NO];
[self performSelectorOnMainThread:@selector(stopReading) withObject:nil waitUntilDone:NO];
dispatch_async(dispatch_get_main_queue(), ^{
[self.startButton setTitle:@"Start!" forState:UIControlStateNormal];
self.hashTextField.text = [metadataObj stringValue];
//[self performSegueWithIdentifier:_segueIdentify sender:nil];
[self hashUpdater:_hashTextField.text];
// [self dismissViewControllerAnimated:YES completion:nil];
[self.navigationController popViewControllerAnimated:YES];
});
self.isScannerReading = NO;
}
}
}
else
return;
}
#pragma mark MobFox Interstitial Ad Delegate
//best to show after delegate informs an ad was loaded
- (void)MobFoxInterstitialAdDidLoad:(MobFoxInterstitialAd *)interstitial {
NSLog(@"MobFoxInterstitialAdDidLoad:");
if(self.mobfoxInterAd.ready){
[self.mobfoxInterAd show];
}
}
@end