Repository navigation
Expand file tree
/
Copy pathDBConnectController.m
More file actions
executable file
·321 lines (248 loc) · 10.6 KB
/
Copy pathDBConnectController.m
File metadata and controls
executable file
·321 lines (248 loc) · 10.6 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
//
// DBConnectController.m
// DropboxSDK
//
// Created by Brian Smith on 5/4/12.
// Copyright (c) 2012 Dropbox, Inc. All rights reserved.
//
#import "DBConnectController.h"
#import <QuartzCore/QuartzCore.h>
#import "DBLog.h"
#import "DBRequest.h"
#import "DBSession+iOS.h"
#include "TargetConditionals.h"
extern id<DBNetworkRequestDelegate> dbNetworkRequestDelegate;
@interface DBConnectController () <UIWebViewDelegate, UIAlertViewDelegate>
- (void)loadRequest;
- (BOOL)openUrl:(NSURL *)url;
- (void)dismiss;
- (void)dismissAnimated:(BOOL)animated;
- (void)cancelAnimated:(BOOL)animated;
@property (nonatomic, assign) UIViewController *rootController;
@property (nonatomic, retain) DBSession *session;
@property (nonatomic, retain) UIAlertView *alertView;
@property (nonatomic, assign) BOOL hasLoaded;
@property (nonatomic, retain) NSURL *url;
@property (nonatomic, retain) UIWebView *webView;
@end
@interface NSURL (DBConnectController)
- (NSArray *)dbPathComponents;
@end
@implementation DBConnectController
@synthesize alertView;
- (void)setAlertView:(UIAlertView *)pAlertView {
if (pAlertView == alertView) return;
alertView.delegate = nil;
[alertView release];
alertView = [pAlertView retain];
}
@synthesize rootController;
@synthesize session;
@synthesize hasLoaded;
@synthesize url;
@synthesize webView;
- (id)initWithUrl:(NSURL *)connectUrl fromController:(UIViewController *)pRootController {
return [self initWithUrl:connectUrl fromController:pRootController session:[DBSession sharedSession]];
}
- (id)initWithUrl:(NSURL *)connectUrl fromController:(UIViewController *)pRootController session:(DBSession *)pSession {
if ((self = [super init])) {
self.url = connectUrl;
self.rootController = pRootController;
self.session = pSession;
self.title = @"Dropbox";
self.navigationItem.rightBarButtonItem =
[[[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancel)]
autorelease];
}
return self;
}
- (void)dealloc {
[session release];
alertView.delegate = nil;
[alertView release];
[url release];
if (webView.isLoading) {
[webView stopLoading];
}
webView.delegate = nil;
[webView release];
[super dealloc];
}
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor colorWithRed:241.0/255 green:249.0/255 blue:255.0/255 alpha:1.0];
UIActivityIndicatorView *activityIndicator =
[[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray] autorelease];
activityIndicator.autoresizingMask =
UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
CGRect frame = activityIndicator.frame;
frame.origin.x = floorf(self.view.bounds.size.width/2 - frame.size.width/2);
frame.origin.y = floorf(self.view.bounds.size.height/2 - frame.size.height/2) - 20;
activityIndicator.frame = frame;
[activityIndicator startAnimating];
[self.view addSubview:activityIndicator];
self.webView = [[[UIWebView alloc] initWithFrame:self.view.frame] autorelease];
self.webView.delegate = self;
self.webView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.webView.scalesPageToFit = YES;
self.webView.hidden = YES;
self.webView.dataDetectorTypes = UIDataDetectorTypeNone;
[self.view addSubview:self.webView];
[self loadRequest];
}
- (void)viewDidUnload {
[super viewDidUnload];
if ([webView isLoading]) {
[webView stopLoading];
}
webView.delegate = nil;
[webView release];
webView = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad ||
[self.rootController shouldAutorotateToInterfaceOrientation:interfaceOrientation]; // Delegate to presenting view.
}
#pragma mark UIWebViewDelegate methods
- (void)webViewDidStartLoad:(UIWebView *)webView {
[dbNetworkRequestDelegate networkRequestStarted];
}
- (void)webViewDidFinishLoad:(UIWebView *)aWebView {
[aWebView stringByEvaluatingJavaScriptFromString:@"document.documentElement.style.webkitTouchCallout = \"none\";"]; // Disable touch-and-hold action sheet
[aWebView stringByEvaluatingJavaScriptFromString:@"document.documentElement.style.webkitUserSelect = \"none\";"]; // Disable text selection
webView.frame = self.view.bounds;
CATransition* transition = [CATransition animation];
transition.duration = 0.25;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
transition.type = kCATransitionFade;
[self.view.layer addAnimation:transition forKey:nil];
webView.hidden = NO;
hasLoaded = YES;
[dbNetworkRequestDelegate networkRequestStopped];
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
[dbNetworkRequestDelegate networkRequestStopped];
// ignore "Fame Load Interrupted" errors and cancels
if (error.code == 102 && [error.domain isEqual:@"WebKitErrorDomain"]) return;
if (error.code == NSURLErrorCancelled && [error.domain isEqual:NSURLErrorDomain]) return;
DBLogWarning(@"DropboxSDK: error loading DBConnectController - %@", error);
NSString *title = @"";
NSString *message = @"";
if ([error.domain isEqual:NSURLErrorDomain] && error.code == NSURLErrorNotConnectedToInternet) {
title = NSLocalizedString(@"No internet connection", @"");
message = NSLocalizedString(@"Try again once you have an internet connection.", @"");
} else if ([error.domain isEqual:NSURLErrorDomain] &&
(error.code == NSURLErrorTimedOut || error.code == NSURLErrorCannotConnectToHost)) {
title = NSLocalizedString(@"Internet connection lost", @"");
message = NSLocalizedString(@"Please try again.", @"");
} else {
title = NSLocalizedString(@"Unknown Error Occurred", @"");
message = NSLocalizedString(@"There was an error loading Dropbox. Please try again.", @"");
}
if (self.hasLoaded) {
// If it has loaded, it means it's a form submit, so users can cancel/retry on their own
NSString *okStr = NSLocalizedString(@"OK", nil);
self.alertView =
[[[UIAlertView alloc]
initWithTitle:title message:message delegate:nil cancelButtonTitle:okStr otherButtonTitles:nil]
autorelease];
} else {
// if the page hasn't loaded, this alert gives the user a way to retry
NSString *retryStr = NSLocalizedString(@"Retry", @"Retry loading a page that has failed to load");
self.alertView =
[[[UIAlertView alloc]
initWithTitle:title message:message delegate:self cancelButtonTitle:NSLocalizedString(@"Cancel", @"")
otherButtonTitles:retryStr, nil]
autorelease];
}
[self.alertView show];
}
- (BOOL)webView:(UIWebView *)aWebView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
NSString *appScheme = [self.session appScheme];
if ([[[request URL] scheme] isEqual:appScheme]) {
BOOL success = [self openUrl:[request URL]];
if (success && ![self.session isLinked]) {
DBLogError(@"DropboxSDK: credentials not saved. Make sure you call -[DBSession handleOpenUrl:] in your app delegate's application:openURL:sourceApplication:annotation: method");
}
[self dismiss];
return NO;
} else if ([[[request URL] scheme] isEqual:@"itms-apps"]) {
#if TARGET_IPHONE_SIMULATOR
DBLogError(@"DropboxSDK - Can't open on simulator. Run on an iOS device to test this functionality");
#else
[[UIApplication sharedApplication] openURL:[request URL]];
[self cancelAnimated:NO];
#endif
return NO;
} else if (![[[request URL] dbPathComponents] isEqual:[self.url dbPathComponents]]) {
DBConnectController *childController = [[[DBConnectController alloc] initWithUrl:[request URL] fromController:self.rootController] autorelease];
NSDictionary *queryParams = [DBSession parseURLParams:[[request URL] query]];
NSString *title = [queryParams objectForKey:@"embed_title"];
if (title) {
childController.title = title;
} else {
childController.title = self.title;
}
childController.navigationItem.rightBarButtonItem = nil;
[self.navigationController pushViewController:childController animated:YES];
return NO;
}
return YES;
}
#pragma mark UIAlertView methods
- (void)alertView:(UIAlertView *)pAlertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex != pAlertView.cancelButtonIndex) {
[self loadRequest];
} else {
if ([self.navigationController.viewControllers count] > 1) {
[self.navigationController popViewControllerAnimated:YES];
} else {
[self cancel];
}
}
self.alertView = nil;
}
#pragma mark private methods
- (void)loadRequest {
NSURLRequest *urlRequest =
[[[NSURLRequest alloc] initWithURL:self.url cachePolicy:NSURLCacheStorageNotAllowed timeoutInterval:20] autorelease];
[self.webView loadRequest:urlRequest];
}
- (BOOL)openUrl:(NSURL *)openUrl {
UIApplication *app = [UIApplication sharedApplication];
id<UIApplicationDelegate> delegate = app.delegate;
if ([delegate respondsToSelector:@selector(application:openURL:sourceApplication:annotation:)]) {
[delegate application:app openURL:openUrl sourceApplication:@"com.getdropbox.Dropbox" annotation:nil];
} else if ([delegate respondsToSelector:@selector(application:handleOpenURL:)]) {
[delegate application:app handleOpenURL:openUrl];
} else {
DBLogError(@"DropboxSDK: app delegate does not implement application:openURL:sourceApplication:annotation:");
return NO;
}
return YES;
}
- (void)cancelAnimated:(BOOL)animated {
[self dismissAnimated:animated];
NSString *cancelUrl = [NSString stringWithFormat:@"%@://%@/cancel", [self.session appScheme], kDBDropboxAPIVersion];
[self openUrl:[NSURL URLWithString:cancelUrl]];
}
- (void)cancel {
[self cancelAnimated:YES];
}
- (void)dismissAnimated:(BOOL)animated {
if ([webView isLoading]) {
[webView stopLoading];
}
[self.navigationController dismissModalViewControllerAnimated:animated];
}
- (void)dismiss {
[self dismissAnimated:YES];
}
@end
@implementation NSURL (DBConnectController)
- (NSArray *)dbPathComponents {
return [[self path] pathComponents];
}
@end