-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathPSCloudipspWKWebView.m
More file actions
116 lines (94 loc) · 4.02 KB
/
PSCloudipspWKWebView.m
File metadata and controls
116 lines (94 loc) · 4.02 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
//
// PSCloudipspWKWebView.m
// Pods
//
// Created by Nadiia Dovbysh on 5/16/16.
//
//
#import "PSCloudipspWKWebView.h"
NSString * const URL_START_PATTERN = @"http://secure-redirect.cloudipsp.com/submit/#";
@interface PSPayConfirmation (private)
@property (nonatomic, strong, readonly) NSString *htmlPageContent;
@property (nonatomic, strong, readonly) NSString *url;
@property (nonatomic, strong, readonly) NSString *callbackUrl;
@property (nonatomic, strong, readonly) NSString *host;
@property (nonatomic, copy, readonly) OnConfirmed onConfirmed;
@end
@interface PSCloudipspWKWebView () <WKNavigationDelegate>
@property (nonatomic, strong) PSPayConfirmation *confirmation;
@end
@implementation PSCloudipspWKWebView
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.hidden = YES;
}
return self;
}
- (instancetype)initWithFrame:(CGRect)frame configuration:(nonnull WKWebViewConfiguration *)configuration
{
self = [super initWithFrame:frame configuration:configuration];
if (self) {
self.hidden = YES;
}
return self;
}
#pragma mark - PSCloudipspView
- (void)confirm:(PSPayConfirmation *)confirmation {
dispatch_async(dispatch_get_main_queue(), ^{
self.hidden = NO;
});
if (confirmation == nil) {
@throw [NSException exceptionWithName:@"NullPointerException" reason:@"confirmation should be not null" userInfo:nil];
}
self.confirmation = confirmation;
dispatch_async(dispatch_get_main_queue(), ^{
self.navigationDelegate = self;
[self loadHTMLString:confirmation.htmlPageContent baseURL:[NSURL URLWithString:confirmation.url]];
});
}
#pragma mark - WKNavigationDelegate
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
NSString *url = [navigationAction.request.URL absoluteString];
BOOL detectsStartPattern = [url hasPrefix:URL_START_PATTERN];
BOOL detectsCallbackUrl = NO;
BOOL detectsApiToken = NO;
if (!detectsStartPattern) {
detectsCallbackUrl = [url hasPrefix:self.confirmation.callbackUrl];
if (!detectsCallbackUrl) {
detectsApiToken = [url hasPrefix:[NSString stringWithFormat:@"%@/api/checkout?token=", self.confirmation.host]];
}
}
if (detectsStartPattern || detectsCallbackUrl || detectsApiToken) {
NSString *jsonContent = nil;
if (detectsStartPattern) {
jsonContent = [url substringFromIndex:[URL_START_PATTERN length]];
jsonContent = [jsonContent stringByRemovingPercentEncoding];
}
self.confirmation.onConfirmed(jsonContent);
self.hidden = YES;
self.confirmation = nil;
self.navigationDelegate = nil;
decisionHandler(WKNavigationActionPolicyCancel);
} else {
decisionHandler(WKNavigationActionPolicyAllow);
}
}
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction preferences:(WKWebpagePreferences *)preferences decisionHandler:(void (^)(WKNavigationActionPolicy, WKWebpagePreferences * _Nonnull))decisionHandler API_AVAILABLE(ios(13.0)){
[self webView:webView decidePolicyForNavigationAction:navigationAction decisionHandler:^void(WKNavigationActionPolicy policy) {
decisionHandler(policy, preferences);
}];
}
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(null_unspecified WKNavigation *)navigation {
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
}
- (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation {
if(!webView.isLoading) {
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}
}
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(null_unspecified WKNavigation *)navigation withError:(NSError *)error {
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}
@end