forked from bengottlieb/Twitter-OAuth-iPhone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SA_OAuthTwitterController.m
231 lines (180 loc) · 9.47 KB
/
SA_OAuthTwitterController.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
//
// SA_OAuthTwitterController.m
//
// Created by Ben Gottlieb on 24 July 2009.
// Copyright 2009 Stand Alone, Inc.
//
// Some code and concepts taken from examples provided by
// Matt Gemmell, Chris Kimpton, and Isaiah Carew
// See ReadMe for further attributions, copyrights and license info.
//
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
#import "SA_OAuthTwitterEngine.h"
#import "SA_OAuthTwitterController.h"
// Constants
static NSString* const kGGTwitterLoadingBackgroundImage = @"twitter_load.png";
@interface DummyClassForProvidingSetDataDetectorTypesMethod
- (void) setDataDetectorTypes: (int) types;
- (void) setDetectsPhoneNumbers: (BOOL) detects;
@end
@implementation SA_OAuthTwitterController
@synthesize engine = _engine, delegate = _delegate, navigationBar = _navBar, orientation = _orientation;
- (void) dealloc {
[_backgroundView release];
_webView.delegate = nil;
[_webView loadRequest: [NSURLRequest requestWithURL: [NSURL URLWithString: @""]]];
[_webView release];
self.view = nil;
self.engine = nil;
[super dealloc];
}
+ (SA_OAuthTwitterController *) controllerToEnterCredentialsWithTwitterEngine: (SA_OAuthTwitterEngine *) engine delegate: (id <SA_OAuthTwitterControllerDelegate>) delegate forOrientation:(UIInterfaceOrientation)theOrientation {
if (![self credentialEntryRequiredWithTwitterEngine: engine]) return nil; //not needed
SA_OAuthTwitterController *controller = [[[SA_OAuthTwitterController alloc] initWithEngine:engine andOrientation:theOrientation] autorelease];
controller.delegate = delegate;
return controller;
}
+ (SA_OAuthTwitterController *) controllerToEnterCredentialsWithTwitterEngine: (SA_OAuthTwitterEngine *) engine delegate: (id <SA_OAuthTwitterControllerDelegate>) delegate {
return [SA_OAuthTwitterController controllerToEnterCredentialsWithTwitterEngine:engine delegate:delegate forOrientation:UIInterfaceOrientationPortrait];
}
+ (BOOL) credentialEntryRequiredWithTwitterEngine: (SA_OAuthTwitterEngine *) engine {
return ![engine isAuthorized];
}
- (id) initWithEngine: (SA_OAuthTwitterEngine *) engine andOrientation:(UIInterfaceOrientation)theOrientation {
if (self = [super init]) {
self.engine = engine;
if (!engine.OAuthSetup) [_engine requestRequestToken];
self.orientation = theOrientation;
_firstLoad = YES;
if (UIInterfaceOrientationIsLandscape( self.orientation ) )
_webView = [[UIWebView alloc] initWithFrame: CGRectMake(0, 32, 480, 288)];
else
_webView = [[UIWebView alloc] initWithFrame: CGRectMake(0, 44, 320, 416)];
_webView.alpha = 0.0;
_webView.delegate = self;
_webView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
if ([_webView respondsToSelector: @selector(setDetectsPhoneNumbers:)]) [(id) _webView setDetectsPhoneNumbers: NO];
if ([_webView respondsToSelector: @selector(setDataDetectorTypes:)]) [(id) _webView setDataDetectorTypes: 0];
NSURLRequest *request = _engine.authorizeURLRequest;
[_webView loadRequest: request];
}
return self;
}
//=============================================================================================================================
#pragma mark Actions
- (void) denied {
if ([_delegate respondsToSelector: @selector(OAuthTwitterControllerFailed:)]) [_delegate OAuthTwitterControllerFailed: self];
[self performSelector: @selector(dismissModalViewControllerAnimated:) withObject: (id) kCFBooleanTrue afterDelay: 1.0];
}
- (void) gotPin: (NSString *) pin {
_engine.pin = pin;
[_engine requestAccessToken];
if ([_delegate respondsToSelector: @selector(OAuthTwitterController:authenticatedWithUsername:)]) [_delegate OAuthTwitterController: self authenticatedWithUsername: _engine.username];
[self performSelector: @selector(dismissModalViewControllerAnimated:) withObject: (id) kCFBooleanTrue afterDelay: 1.0];
}
- (void) cancel: (id) sender {
if ([_delegate respondsToSelector: @selector(OAuthTwitterControllerCanceled:)]) [_delegate OAuthTwitterControllerCanceled: self];
[self performSelector: @selector(dismissModalViewControllerAnimated:) withObject: (id) kCFBooleanTrue afterDelay: 0.0];
}
//=============================================================================================================================
#pragma mark View Controller Stuff
- (void) loadView {
[super loadView];
_backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:kGGTwitterLoadingBackgroundImage]];
if ( UIInterfaceOrientationIsLandscape( self.orientation ) ) {
self.view = [[[UIView alloc] initWithFrame: CGRectMake(0, 0, 480, 288)] autorelease];
_backgroundView.frame = CGRectMake(0, 44, 480, 288);
_navBar = [[[UINavigationBar alloc] initWithFrame: CGRectMake(0, 0, 480, 32)] autorelease];
} else {
self.view = [[[UIView alloc] initWithFrame: CGRectMake(0, 0, 320, 416)] autorelease];
_backgroundView.frame = CGRectMake(0, 44, 320, 416);
_navBar = [[[UINavigationBar alloc] initWithFrame: CGRectMake(0, 0, 320, 44)] autorelease];
}
_navBar.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin;
_backgroundView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
if (!UIInterfaceOrientationIsLandscape( self.orientation)) [self.view addSubview:_backgroundView];
[self.view addSubview: _webView];
[self.view addSubview: _navBar];
_blockerView = [[[UIView alloc] initWithFrame: CGRectMake(0, 0, 200, 60)] autorelease];
_blockerView.backgroundColor = [UIColor colorWithWhite: 0.0 alpha: 0.8];
_blockerView.center = CGPointMake(self.view.bounds.size.width / 2, self.view.bounds.size.height / 2);
_blockerView.alpha = 0.0;
_blockerView.clipsToBounds = YES;
if ([_blockerView.layer respondsToSelector: @selector(setCornerRadius:)]) [(id) _blockerView.layer setCornerRadius: 10];
UILabel *label = [[[UILabel alloc] initWithFrame: CGRectMake(0, 5, _blockerView.bounds.size.width, 15)] autorelease];
label.text = NSLocalizedString(@"Please Wait…", nil);
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor whiteColor];
label.textAlignment = UITextAlignmentCenter;
label.font = [UIFont boldSystemFontOfSize: 15];
[_blockerView addSubview: label];
UIActivityIndicatorView *spinner = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle: UIActivityIndicatorViewStyleWhite] autorelease];
spinner.center = CGPointMake(_blockerView.bounds.size.width / 2, _blockerView.bounds.size.height / 2 + 10);
[_blockerView addSubview: spinner];
[self.view addSubview: _blockerView];
[spinner startAnimating];
UINavigationItem *navItem = [[[UINavigationItem alloc] initWithTitle: NSLocalizedString(@"Twitter Info", nil)] autorelease];
navItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemCancel target: self action: @selector(cancel:)] autorelease];
[_navBar pushNavigationItem: navItem animated: NO];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
- (void) didRotateFromInterfaceOrientation: (UIInterfaceOrientation) fromInterfaceOrientation {
self.orientation = self.interfaceOrientation;
_blockerView.center = CGPointMake(self.view.bounds.size.width / 2, self.view.bounds.size.height / 2);
// [self performInjection]; //removed due to twitter update
}
//=============================================================================================================================
#pragma mark Webview Delegate stuff
- (void) webViewDidFinishLoad: (UIWebView *) webView {
_loading = NO;
//[self performInjection];
if (_firstLoad) {
[_webView performSelector: @selector(stringByEvaluatingJavaScriptFromString:) withObject: @"window.scrollBy(0,200)" afterDelay: 0];
_firstLoad = NO;
}
NSString *authPin = [[_webView stringByEvaluatingJavaScriptFromString: @"document.getElementById('oauth-pin').innerHTML"] stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
[UIView beginAnimations: nil context: nil];
_blockerView.alpha = 0.0;
[UIView commitAnimations];
if (authPin.length) {
[self gotPin: authPin];
}
if ([_webView isLoading] || authPin.length) {
_webView.alpha = 0.0;
} else {
_webView.alpha = 1.0;
}
}
- (void) performInjection {
if (_loading) return;
NSError *error;
NSString *filename = UIInterfaceOrientationIsLandscape(self.orientation ) ? @"jQueryInjectLandscape" : @"jQueryInject";
NSString *path = [[NSBundle mainBundle] pathForResource: filename ofType: @"txt"];
NSString *dataSource = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];
if (dataSource == nil) {
NSLog(@"An error occured while processing the jQueryInject file");
}
[_webView stringByEvaluatingJavaScriptFromString:dataSource]; //This line injects the jQuery to make it look better
}
- (void) webViewDidStartLoad: (UIWebView *) webView {
//[_activityIndicator startAnimating];
_loading = YES;
[UIView beginAnimations: nil context: nil];
_blockerView.alpha = 1.0;
[UIView commitAnimations];
}
- (BOOL) webView: (UIWebView *) webView shouldStartLoadWithRequest: (NSURLRequest *) request navigationType: (UIWebViewNavigationType) navigationType {
NSData *data = [request HTTPBody];
char *raw = data ? (char *) [data bytes] : "";
if (raw && strstr(raw, "cancel=")) {
[self denied];
return NO;
}
if (navigationType != UIWebViewNavigationTypeOther) _webView.alpha = 0.1;
return YES;
}
@end