This repository was archived by the owner on Aug 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 128
Expand file tree
/
Copy pathSimpleAuthWebViewController.m
More file actions
138 lines (100 loc) · 3.7 KB
/
SimpleAuthWebViewController.m
File metadata and controls
138 lines (100 loc) · 3.7 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
//
// SimpleAuthWebViewController.m
// SimpleAuth
//
// Created by Caleb Davenport on 11/7/13.
// Copyright (c) 2013-2014 Byliner, Inc. All rights reserved.
//
#import "SimpleAuthWebViewController.h"
@interface SimpleAuthWebViewController ()
@property (nonatomic, copy) NSDictionary *options;
@property (nonatomic, copy) NSDictionary *requestToken;
@end
@implementation SimpleAuthWebViewController {
BOOL _hasInitialLoad;
}
@synthesize webView = _webView;
#pragma mark - UIViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
[self setupWebView];
}
- (void)setupWebView {
[self.view addSubview:self.webView];
self.webView.translatesAutoresizingMaskIntoConstraints = NO;
[self.webView.topAnchor constraintEqualToAnchor:self.view.layoutMarginsGuide.topAnchor].active = YES;
[self.webView.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor].active = YES;
[self.webView.bottomAnchor constraintEqualToAnchor:self.view.layoutMarginsGuide.bottomAnchor].active = YES;
[self.webView.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor].active = YES;
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
if (!_hasInitialLoad) {
_hasInitialLoad = YES;
NSURLRequest *request = [self initialRequest];
request = [[self class] canonicalRequestForRequest:request];
[self.webView loadRequest:request];
}
}
#pragma mark - Public
- (instancetype)initWithOptions:(NSDictionary *)options requestToken:(NSDictionary *)requestToken {
if ((self = [super init])) {
self.options = options;
self.requestToken = requestToken;
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
target:self
action:@selector(cancel)];
}
return self;
}
- (instancetype)initWithOptions:(NSDictionary *)options {
self = [self initWithOptions:options requestToken:nil];
return self;
}
- (BOOL)isTargetRedirectURL:(NSURL *)URL {
NSString *targetURLString = [self.options[SimpleAuthRedirectURIKey] lowercaseString];
NSString *actualURLString = [[URL absoluteString] lowercaseString];
return [actualURLString hasPrefix:targetURLString];
}
- (id)responseObjectFromRedirectURL:(NSURL *)URL {
return nil;
}
- (void)cancel {
NSError *error = [NSError errorWithDomain:SimpleAuthErrorDomain code:SimpleAuthErrorUserCancelled userInfo:nil];
self.completion(self, nil, error);
}
- (NSURLRequest *)initialRequest {
[self doesNotRecognizeSelector:_cmd];
return nil;
}
#pragma mark - Private
+ (NSURLRequest *)canonicalRequestForRequest:(NSURLRequest *)request {
NSMutableURLRequest *mutableRequest = [request mutableCopy];
[mutableRequest setCachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData];
return mutableRequest;
}
#pragma mark - Accessors
- (UIWebView *)webView {
if (!_webView) {
_webView = [UIWebView new];
_webView.delegate = self;
}
return _webView;
}
#pragma mark - UIWebViewDelegate
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
webView.delegate = nil;
self.completion(self, nil, error);
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)type {
NSURL *URL = [request URL];
if ([self isTargetRedirectURL:URL]) {
webView.delegate = nil;
self.completion(self, URL, nil);
return NO;
}
return YES;
}
@end