-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathWBAuthorizeWebView.m
332 lines (273 loc) · 9.41 KB
/
WBAuthorizeWebView.m
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
//
// WBAuthorizeWebView.m
// SinaWeiBoSDK
// Based on OAuth 2.0
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//
// Copyright 2011 Sina. All rights reserved.
//
#import "WBAuthorizeWebView.h"
#import <QuartzCore/QuartzCore.h>
@interface WBAuthorizeWebView (Private)
- (void)bounceOutAnimationStopped;
- (void)bounceInAnimationStopped;
- (void)bounceNormalAnimationStopped;
- (void)allAnimationsStopped;
- (UIInterfaceOrientation)currentOrientation;
- (void)sizeToFitOrientation:(UIInterfaceOrientation)orientation;
- (CGAffineTransform)transformForOrientation:(UIInterfaceOrientation)orientation;
- (BOOL)shouldRotateToOrientation:(UIInterfaceOrientation)orientation;
- (void)addObservers;
- (void)removeObservers;
@end
@implementation WBAuthorizeWebView
@synthesize delegate;
#pragma mark - WBAuthorizeWebView Life Circle
- (id)init
{
if (self = [super initWithFrame:CGRectMake(0, 0, 320, 480)])
{
// background settings
[self setBackgroundColor:[UIColor clearColor]];
[self setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
// add the panel view
panelView = [[UIView alloc] initWithFrame:CGRectMake(10, 30, 300, 440)];
[panelView setBackgroundColor:[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.55]];
[[panelView layer] setMasksToBounds:NO]; // very important
[[panelView layer] setCornerRadius:10.0];
[self addSubview:panelView];
// add the conainer view
containerView = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 280, 420)];
[[containerView layer] setBorderColor:[UIColor colorWithRed:0. green:0. blue:0. alpha:0.7].CGColor];
[[containerView layer] setBorderWidth:1.0];
// add the web view
webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 280, 390)];
[webView setDelegate:self];
[containerView addSubview:webView];
[panelView addSubview:containerView];
indicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
[indicatorView setCenter:CGPointMake(160, 240)];
[self addSubview:indicatorView];
}
return self;
}
- (void)dealloc
{
[panelView release], panelView = nil;
[containerView release], containerView = nil;
[webView release], webView = nil;
[indicatorView release], indicatorView = nil;
[super dealloc];
}
#pragma mark Actions
- (void)onCloseButtonTouched:(id)sender
{
[self hide:YES];
}
#pragma mark Orientations
- (UIInterfaceOrientation)currentOrientation
{
return [UIApplication sharedApplication].statusBarOrientation;
}
- (void)sizeToFitOrientation:(UIInterfaceOrientation)orientation
{
[self setTransform:CGAffineTransformIdentity];
if (UIInterfaceOrientationIsLandscape(orientation))
{
[self setFrame:CGRectMake(0, 0, 480, 320)];
[panelView setFrame:CGRectMake(10, 30, 460, 280)];
[containerView setFrame:CGRectMake(10, 10, 440, 260)];
[webView setFrame:CGRectMake(0, 0, 440, 260)];
[indicatorView setCenter:CGPointMake(240, 160)];
}
else
{
[self setFrame:CGRectMake(0, 0, 320, 480)];
[panelView setFrame:CGRectMake(10, 30, 300, 440)];
[containerView setFrame:CGRectMake(10, 10, 280, 420)];
[webView setFrame:CGRectMake(0, 0, 280, 420)];
[indicatorView setCenter:CGPointMake(160, 240)];
}
[self setCenter:CGPointMake(160, 240)];
[self setTransform:[self transformForOrientation:orientation]];
previousOrientation = orientation;
}
- (CGAffineTransform)transformForOrientation:(UIInterfaceOrientation)orientation
{
if (orientation == UIInterfaceOrientationLandscapeLeft)
{
return CGAffineTransformMakeRotation(-M_PI / 2);
}
else if (orientation == UIInterfaceOrientationLandscapeRight)
{
return CGAffineTransformMakeRotation(M_PI / 2);
}
else if (orientation == UIInterfaceOrientationPortraitUpsideDown)
{
return CGAffineTransformMakeRotation(-M_PI);
}
else
{
return CGAffineTransformIdentity;
}
}
- (BOOL)shouldRotateToOrientation:(UIInterfaceOrientation)orientation
{
if (orientation == previousOrientation)
{
return NO;
}
else
{
return orientation == UIInterfaceOrientationLandscapeLeft
|| orientation == UIInterfaceOrientationLandscapeRight
|| orientation == UIInterfaceOrientationPortrait
|| orientation == UIInterfaceOrientationPortraitUpsideDown;
}
return YES;
}
#pragma mark Obeservers
- (void)addObservers
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(deviceOrientationDidChange:)
name:@"UIDeviceOrientationDidChangeNotification" object:nil];
}
- (void)removeObservers
{
[[NSNotificationCenter defaultCenter] removeObserver:self
name:@"UIDeviceOrientationDidChangeNotification" object:nil];
}
#pragma mark Animations
- (void)bounceOutAnimationStopped
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.13];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(bounceInAnimationStopped)];
[panelView setAlpha:0.8];
[panelView setTransform:CGAffineTransformScale(CGAffineTransformIdentity, 0.9, 0.9)];
[UIView commitAnimations];
}
- (void)bounceInAnimationStopped
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.13];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(bounceNormalAnimationStopped)];
[panelView setAlpha:1.0];
[panelView setTransform:CGAffineTransformScale(CGAffineTransformIdentity, 1.0, 1.0)];
[UIView commitAnimations];
}
- (void)bounceNormalAnimationStopped
{
[self allAnimationsStopped];
}
- (void)allAnimationsStopped
{
// nothing shall be done here
}
#pragma mark Dismiss
- (void)hideAndCleanUp
{
[self removeObservers];
[self removeFromSuperview];
}
#pragma mark - WBAuthorizeWebView Public Methods
- (void)loadRequestWithURL:(NSURL *)url
{
NSURLRequest *request =[NSURLRequest requestWithURL:url
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval:60.0];
[webView loadRequest:request];
}
- (void)show:(BOOL)animated
{
[self sizeToFitOrientation:[self currentOrientation]];
UIWindow *window = [UIApplication sharedApplication].keyWindow;
if (!window)
{
window = [[UIApplication sharedApplication].windows objectAtIndex:0];
}
[window addSubview:self];
if (animated)
{
[panelView setAlpha:0];
CGAffineTransform transform = CGAffineTransformIdentity;
[panelView setTransform:CGAffineTransformScale(transform, 0.3, 0.3)];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.2];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(bounceOutAnimationStopped)];
[panelView setAlpha:0.5];
[panelView setTransform:CGAffineTransformScale(transform, 1.1, 1.1)];
[UIView commitAnimations];
}
else
{
[self allAnimationsStopped];
}
[self addObservers];
}
- (void)hide:(BOOL)animated
{
if (animated)
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.3];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(hideAndCleanUp)];
[self setAlpha:0];
[UIView commitAnimations];
}
[self hideAndCleanUp];
}
#pragma mark - UIDeviceOrientationDidChangeNotification Methods
- (void)deviceOrientationDidChange:(id)object
{
UIInterfaceOrientation orientation = [self currentOrientation];
if ([self shouldRotateToOrientation:orientation])
{
NSTimeInterval duration = [UIApplication sharedApplication].statusBarOrientationAnimationDuration;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:duration];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[self sizeToFitOrientation:orientation];
[UIView commitAnimations];
}
}
#pragma mark - UIWebViewDelegate Methods
- (void)webViewDidStartLoad:(UIWebView *)aWebView
{
[indicatorView startAnimating];
}
- (void)webViewDidFinishLoad:(UIWebView *)aWebView
{
[indicatorView stopAnimating];
}
- (void)webView:(UIWebView *)aWebView didFailLoadWithError:(NSError *)error
{
[indicatorView stopAnimating];
}
- (BOOL)webView:(UIWebView *)aWebView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSRange range = [request.URL.absoluteString rangeOfString:@"code="];
if (range.location != NSNotFound)
{
NSString *code = [request.URL.absoluteString substringFromIndex:range.location + range.length];
if ([delegate respondsToSelector:@selector(authorizeWebView:didReceiveAuthorizeCode:)])
{
[delegate authorizeWebView:self didReceiveAuthorizeCode:code];
}
}
return YES;
}
@end