WLInboxItemViewController.m
17.9 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
/*
Copyright 2010-2016 Warply Inc. All rights reserved.
Redistribution and use in source and binary forms, without modification,
are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binaryform must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE WARPLY LTD ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
EVENT SHALL WARPLY LTD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#import "WLInboxItemViewController.h"
#import "WLGlobals.h"
#import "WLAnalyticsManager.h"
#import "Warply.h"
#import "WLInboxItem.h"
#import "WLAPPActionHandler.h"
///////////////////////////////////////////////////////////////////////////////
@interface WLInboxItemViewController ()
{
@private
//UI
WKWebView *_webView;
UIActivityIndicatorView *_activity;
UIButton *_btnBack;
UIButton *_btnForward;
NSTimer *_timer;
}
@property (nonatomic, strong) WLInboxItem *item;
@property (nonatomic, strong) NSTimer *timer;
@property (nonatomic, assign) WLInboxItemViewController *inboxVC;
- (void)updateButtons;
- (void)backPressed;
- (void)forwardPressed;
- (void)closePressed;
- (void)onTimer:(NSTimer*)sender;
@end
@implementation WLInboxItemViewController
//Private
@synthesize item = _item;
@synthesize timer = _timer;
#pragma mark - Initialization
///////////////////////////////////////////////////////////////////////////////
- (id)initWithItem:(WLInboxItem *)anItem
{
self = [super init];
if (self) {
self.item = anItem;
}
return self;
}
#pragma mark - View Lifecycle
///////////////////////////////////////////////////////////////////////////////
- (void)loadView
{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 480.0)];
view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
view.backgroundColor = [UIColor blackColor];
UIView *statusView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 20.0)];
statusView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
statusView.backgroundColor = [UIColor whiteColor];
[view addSubview:statusView];
WKPreferences *preferences = [[WKPreferences alloc] init];
preferences.javaScriptEnabled = YES; // Here its set
// Other things you might want to disable
preferences.javaScriptCanOpenWindowsAutomatically = YES;
//Init WebView
_webView = [[WKWebView alloc] initWithFrame:CGRectMake(0.0, 20.0, 320.0, 480.0)];
//_webView.dataDetectorTypes = UIDataDetectorTypeNone;
WKWebViewConfiguration *theConfiguration = [[WKWebViewConfiguration alloc] init];
theConfiguration.preferences = preferences;
theConfiguration.dataDetectorTypes = UIDataDetectorTypeNone;
_webView = [[WKWebView alloc] initWithFrame:CGRectMake(0.0, 20.0, 320.0, 480.0) configuration:theConfiguration];
_webView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
_webView.navigationDelegate = self;
[view addSubview:_webView];
//Init ActivityView
_activity = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
_activity.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
_activity.center = _webView.center;
_activity.alpha = 0.0;
[view addSubview:_activity];
self.view = view;
}
-(NSString *)session_uuid
{
return _item.session_uuid;
}
///////////////////////////////////////////////////////////////////////////////
- (void)viewDidLoad
{
[super viewDidLoad];
//Back Button
_btnBack = [UIButton buttonWithType:UIButtonTypeCustom];
[_btnBack setImage:[UIImage imageNamed:@"warp_white_back_button.png"] forState:UIControlStateNormal];
[_btnBack sizeToFit];
[_btnBack addTarget:self action:@selector(backPressed) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *backBarButton = [[UIBarButtonItem alloc] initWithCustomView:_btnBack];
//Forward Button
_btnForward = [UIButton buttonWithType:UIButtonTypeCustom];
[_btnForward setImage:[UIImage imageNamed:@"warp_white_forward_button.png"] forState:UIControlStateNormal];
[_btnForward sizeToFit];
[_btnForward addTarget:self action:@selector(forwardPressed) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *forwardBarButton = [[UIBarButtonItem alloc] initWithCustomView:_btnForward];
_btnForward.hidden = _hideNavigationArrows;
_btnBack.hidden = _hideNavigationArrows;
//Close Button
UIButton *btnClose = [[UIButton alloc] initWithFrame:CGRectMake(0.0, 0.0, 44.0, 44.0)];
btnClose.backgroundColor = [UIColor clearColor];
[btnClose setImage:[UIImage imageNamed:@"warp_white_close_button.png"] forState:UIControlStateNormal];
[btnClose addTarget:self action:@selector(closePressed) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *closeBarButton = [[UIBarButtonItem alloc] initWithCustomView:btnClose];
//temp close button
UIButton *tmpClose = [[UIButton alloc] initWithFrame:CGRectMake(260.0, 30.0, 44.0, 44.0)];
if ([UIScreen mainScreen].bounds.size.height < 568) {
tmpClose.frame = CGRectMake(260.0, 30.0, 44.0, 44.0);
} else if ([UIScreen mainScreen].bounds.size.height < 667) {
tmpClose.frame = CGRectMake(260.0, 30.0, 44.0, 44.0);
} else if ([UIScreen mainScreen].bounds.size.height < 736) {
tmpClose.frame = CGRectMake(315.0, 30.0, 44.0, 44.0);
} else {
tmpClose.frame = CGRectMake(354.0, 30.0, 44.0, 44.0);
}
tmpClose.backgroundColor = [UIColor colorWithRed:0.24 green:0.29 blue:0.32 alpha:0.7];
tmpClose.layer.cornerRadius = 22.0;
tmpClose.clipsToBounds = YES;
[tmpClose setImage:[UIImage imageNamed:@"warp_white_close_button.png"] forState:UIControlStateNormal];
[tmpClose addTarget:self action:@selector(closePressed) forControlEvents:UIControlEventTouchUpInside];
if (_fromFullPage || _fromNativeAd) {
[self.view addSubview:tmpClose];
[UIApplication sharedApplication].statusBarHidden = YES;
}
// [self.view addSubview:tmpClose];
//Toolbar
self.navigationController.toolbar.tintColor = [UIColor blackColor];
if ([self.navigationController.toolbar respondsToSelector:@selector(setBarTintColor:)]) {
self.navigationController.toolbar.barTintColor = [UIColor blackColor];
}
self.navigationController.toolbar.backgroundColor = [UIColor blackColor];;
self.navigationController.toolbar.translucent = NO;
UIBarButtonItem *flex = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:NULL];
UIBarButtonItem *space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:NULL];
space.width = 40.0f;
NSArray *items = [NSArray arrayWithObjects: backBarButton, space, forwardBarButton, flex, closeBarButton, nil];
[self setToolbarItems:items];
[self.navigationController setNavigationBarHidden:YES animated:YES];
[self.navigationController.tabBarController setHidesBottomBarWhenPushed:YES];
[_activity startAnimating];
[UIView animateWithDuration:0.5 animations:^{
_activity.alpha = 1.0;
}];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[_item getPageURL]]];
request.cachePolicy = NSURLRequestReloadIgnoringLocalCacheData;
[request setValue:[Warply sharedService].webId forHTTPHeaderField:@"loyalty-web-id"];
[request setValue:@"gzip" forHTTPHeaderField:@"Accept-Encoding"];
[request setValue:@"gzip" forHTTPHeaderField:@"User-Agent"];
[_webView loadRequest:request];
NSString *jScript = @"var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta);";
WKUserScript *wkUScript = [[WKUserScript alloc] initWithSource:jScript injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
WKUserContentController *wkUController = [[WKUserContentController alloc] init];
[wkUController addUserScript:wkUScript];
WKWebViewConfiguration *wkWebConfig = [[WKWebViewConfiguration alloc] init];
wkWebConfig.userContentController = wkUController;
_webView = [[WKWebView alloc] initWithFrame:self.view.frame configuration:wkWebConfig];
self.timer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(onTimer:) userInfo:nil repeats:YES];
[Warply sharedService].allOffers = nil;
[[Warply sharedService] getInboxWithSuccessBlock: nil failureBlock:nil];
}
///////////////////////////////////////////////////////////////////////////////
- (void)viewDidUnload
{
[super viewDidUnload];
[_webView setUIDelegate:nil];
}
///////////////////////////////////////////////////////////////////////////////
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
return (toInterfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
///////////////////////////////////////////////////////////////////////////////
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskAllButUpsideDown;
}
- (WKWebView *)webView:(WKWebView *)webView createWebViewWithConfiguration:(WKWebViewConfiguration *)configuration forNavigationAction:(WKNavigationAction *)navigationAction windowFeatures:(WKWindowFeatures *)windowFeatures
{
NSLog(@"createWebViewWithConfiguration %@ %@", navigationAction, windowFeatures);
if (!navigationAction.targetFrame.isMainFrame) {
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
[(WKWebView *)_webView loadRequest:navigationAction.request];
}
return nil;
}
#pragma mark - UIWebViewDelegater
///////////////////////////////////////////////////////////////////////////////
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
//NSURL *url = [navigationAction.request.URL query];
if ([navigationAction.request.URL.scheme isEqualToString:@"tel"]) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:[navigationAction.request.URL.resourceSpecifier stringByReplacingOccurrencesOfString:@"/" withString:@""] message:nil delegate:self cancelButtonTitle:NSLocalizedString(@"Cancel", @"Warply") otherButtonTitles:NSLocalizedString(@"Call", @"Warply"), nil];
alert.tag = 1000;
[alert show];
}
decisionHandler(WKNavigationActionPolicyAllow);
NSString *url = navigationAction.request.URL.absoluteString;
if ([navigationAction.request.URL.absoluteString containsString:@"mobile_store"]) {
if ([[UIApplication sharedApplication] canOpenURL:navigationAction.request.URL]) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url] options:@{} completionHandler:nil];
[UIApplication sharedApplication].statusBarHidden = NO;
[self dismissViewControllerAnimated:YES completion:nil];
}
}
if([[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleURLTypes"]) {
NSArray *urlTypes = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleURLTypes"];
for(NSDictionary *urlType in urlTypes)
{
if(urlType[@"CFBundleURLSchemes"])
{
NSArray *urlSchemes = urlType[@"CFBundleURLSchemes"];
for(NSString *urlScheme in urlSchemes)
if ([navigationAction.request.URL.absoluteString containsString:urlScheme]) {
float seconds = 2.5;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(seconds * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
if ([[UIApplication sharedApplication] canOpenURL:navigationAction.request.URL]) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:navigationAction.request.URL.absoluteString] options:@{} completionHandler:nil];
[UIApplication sharedApplication].statusBarHidden = NO;
[self dismissViewControllerAnimated:YES completion:nil];
}
});
}
}
}
}
if (navigationAction.navigationType == UIWebViewNavigationTypeLinkClicked) {
if ([[UIApplication sharedApplication] canOpenURL:navigationAction.request.URL]) {
[[UIApplication sharedApplication] openURL:navigationAction.request.URL options:@{} completionHandler:nil];
}
[UIApplication sharedApplication].statusBarHidden = NO;
[self dismissViewControllerAnimated:YES completion:nil];
}
}
- (BOOL)webView:(WKWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
_requestedUrl = request.URL;
if ([request.URL.scheme isEqualToString:@"tel"]) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:[request.URL.resourceSpecifier stringByReplacingOccurrencesOfString:@"/" withString:@""] message:nil delegate:self cancelButtonTitle:NSLocalizedString(@"Cancel", @"Warply") otherButtonTitles:NSLocalizedString(@"Call", @"Warply"), nil];
alert.tag = 1000;
[alert show];
return NO;
}
[self updateButtons];
WLLOG(@"WARPLY OFFER WEBVIEW REQUEST URL:%@",request.URL);
if ([[Warply sharedService] handleActionUrl:_requestedUrl]) {
return NO;
}
return YES;
}
///////////////////////////////////////////////////////////////////////////////
//- (void)webViewDidStartLoad:(WKWebView *)webView {
//
//}
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation {
}
///////////////////////////////////////////////////////////////////////////////
//- (void)webViewDidFinishLoad:(WKWebView *)webView
//{
// WLLOG(@"Finished Loading Campaign.");
// [self updateButtons];
// [UIView animateWithDuration:0.5
// animations:^{
// _activity.alpha = 0.0;
// }
// completion:^(BOOL finished) {
// [_activity stopAnimating];
// }];
//}
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
WLLOG(@"Finished Loading Campaign.");
[self updateButtons];
[UIView animateWithDuration:0.5
animations:^{
_activity.alpha = 0.0;
}
completion:^(BOOL finished) {
[_activity stopAnimating];
}];
}
///////////////////////////////////////////////////////////////////////////////
//- (void)webView:(WKWebView *)webView didFailLoadWithError:(NSError *)error
//{
// WLLOG(@"Error while loading page = %@", [error description]);
// [self updateButtons];
//}
- (void)webView:(WKWebView *)webView didFailNavigation:(WKNavigation *)navigation withError:(NSError *)error {
WLLOG(@"Error while loading page = %@", [error description]);
[self updateButtons];
}
#pragma mark - Private Methods
///////////////////////////////////////////////////////////////////////////////
- (void)updateButtons
{
_btnBack.enabled = _webView.canGoBack;
_btnForward.enabled = _webView.canGoForward;
}
///////////////////////////////////////////////////////////////////////////////
- (void)backPressed
{
[_webView goBack];
}
///////////////////////////////////////////////////////////////////////////////
- (void)forwardPressed
{
[_webView goForward];
}
///////////////////////////////////////////////////////////////////////////////
- (void)closePressed
{
[UIApplication sharedApplication].statusBarHidden = NO;
[self dismissViewControllerAnimated:YES completion:nil];
}
///////////////////////////////////////////////////////////////////////////////
- (void)onTimer:(NSTimer*)sender
{
[self updateButtons];
}
///////////////////////////////////////////////////////////////////////////////
#pragma mark - Alert View Delegate
// If the alert view is a call confirmation prompt send the appropriate action analytic event
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
if (alertView.tag != 1000) {
return;
}
NSArray *callActions;
switch (buttonIndex) {
case 0:
callActions = @[@"call_cancelled"];
[WLAnalyticsManager logUrgentEventWithEventId:@"NB_CUSTOM_ACTION" pageId:@"TEL" actionMetadata:@{@"session_uuid":self.item.session_uuid, @"result": callActions}];
break;
default:
callActions = @[@"call_clicked"];
[WLAnalyticsManager logUrgentEventWithEventId:@"NB_CUSTOM_ACTION" pageId:@"TEL" actionMetadata:@{@"session_uuid":self.item.session_uuid, @"result": callActions}];
[[UIApplication sharedApplication] openURL:_requestedUrl];
}
}
#pragma mark - Memory Management
///////////////////////////////////////////////////////////////////////////////
- (void)dealloc
{
[_timer invalidate];
[_webView setUIDelegate:nil];
}
@end