-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOLBTwitpicEngine.m
275 lines (224 loc) · 11.2 KB
/
OLBTwitpicEngine.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
//
// OLBTwitpicEngine.m
// ----------------------------------------------------------------------
// Controller class for uploading a UIImage to TwitPic.com and post to Twitter.com.
// This procedure automatically posts to the Twitter account specified.
//
// License
// ----------------------------------------------------------------------
// This code is offered under the MIT License.
//
// Copyright (c) 2008-2012 Oskar Boethius Lissheim (@avocade on Twitter)
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// 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.
//
// ----------------------------------------------------------------------
#import "OLBTwitpicEngine.h"
#import "RegexKitLite.h"
#import "SynthesizeSingleton.h"
enum SendToTwitterAlertButtons {
SendToTwitterAlertButton_Cancel = 0,
SendToTwitterAlertButton_Send,
};
#define kTwitpicUploadURL @"https://twitpic.com/api/uploadAndPost" // Note: This URL automatically posts to Twitter on upload
#define kTwitpicImageJPEGCompression 0.7 // Between 0.1 and 1.0, where 1.0 is the highest quality JPEG compression setting
@implementation OLBTwitpicEngine
@synthesize imageToSend = _imageToSend;
@synthesize twitterMsgTextField = _twitterMsgTextField;
SYNTHESIZE_SINGLETON_FOR_CLASS(OLBTwitpicEngine); // Get this singleton-creation class from cocoawithlove.com
#pragma mark -
#pragma mark Show UIAlertView to input twitter message
- (void)presentTwitterMsgAlertWithImage:(UIImage *)theImage delegate:(id)theDelegate
{
UIAlertView *alert = nil;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
UITextField *emailTextField = nil;
CGRect aRect;
NSLog(@"presentAddTwitterMsgAlert");
// Add delegate
delegate = theDelegate;
// Save the image to send
self.imageToSend = theImage;
// Standard error presentation (only OK button)
alert = [[[UIAlertView alloc] initWithTitle:@"Send to Twitter"
message:@"Add a Tweet message:\n\n\nNote: The image will be posted to Twitter via TwitPic."
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"Send", nil ] autorelease];
// Add text field
aRect = CGRectMake(20, 75, 242, 22);
emailTextField = [[[UITextField alloc] initWithFrame:aRect] autorelease];
emailTextField.font = [UIFont boldSystemFontOfSize:16.0];
emailTextField.backgroundColor = [UIColor whiteColor];
emailTextField.autocorrectionType = UITextAutocorrectionTypeNo;
emailTextField.autocapitalizationType = UITextAutocapitalizationTypeNone;
emailTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
emailTextField.keyboardAppearance = UIKeyboardAppearanceAlert;
emailTextField.delegate = self;
// Add as ivar
self.twitterMsgTextField = emailTextField;
// Set text from defaults
emailTextField.text = [defaults valueForKey:kTwitpicMessage];
// Add subview
[alert addSubview:emailTextField];
// Present alert
[alert show];
}
#pragma mark TextField delegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
// From showUserEmailOnServerAlert
}
#pragma mark UIAlertview delegate methods
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSLog(@"alertView:clickedButtonAtIndex: %i", buttonIndex);
NSString *buttonTitle = [alertView buttonTitleAtIndex:buttonIndex];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *message = nil;
NSString *username = nil;
NSString *password = nil;
if ([alertView.title isEqualToString:@"Send to Twitter"] )
{
if ([buttonTitle isEqualToString:@"Send"] )
{
// Set message, username and password from defaults
// otherwise throw up error
username = [defaults stringForKey:@"twitpicUsername"];
password = [defaults stringForKey:@"twitpicPassword"];
if (username && password)
{
// Add in the loading indicator in place of the button
[delegate twitpicEngine:self showTwitpicUploadActivityIndicator:YES];
// Get the message from the textfield
message = self.twitterMsgTextField.text;
// Create OLBTwitpicEngine object with userCredentials dictionary and delegate to self
[[OLBTwitpicEngine sharedOLBTwitpicEngine] uploadImageToTwitpic:self.imageToSend withMessage:message
username:username password:password];
}
else
{
// Show UIAlertView, need to set username and password in Settings
alertView = [[[UIAlertView alloc] initWithTitle:@"Couldn't Send to Twitter" message:@"Please enter a valid Twitter username and password in the Light Table Settings."
delegate:self cancelButtonTitle:@"I'll check"
otherButtonTitles:nil] autorelease];
[alertView show];
}
}
}
}
#pragma mark -
#pragma mark Send image to twitpic
- (void)uploadingDataWithURLRequest:(NSURLRequest *)urlRequest
{
// Called on a separate thread; upload and handle server response
NSHTTPURLResponse *urlResponse = nil;
NSError *error = nil;
NSString *responseString = nil;
NSData *responseData = nil;
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; // Each thread must have its own NSAutoreleasePool
[urlRequest retain]; // Retain since we autoreleased it before
// Send the request
urlResponse = nil;
responseData = [NSURLConnection sendSynchronousRequest:urlRequest
returningResponse:&urlResponse
error:&error];
responseString = [[NSString alloc] initWithData:responseData
encoding:NSUTF8StringEncoding];
// Handle the error or success
// If error, create error message and throw up UIAlertView
NSLog(@"Response Code: %d", [urlResponse statusCode]);
if ([urlResponse statusCode] >= 200 && [urlResponse statusCode] < 300)
{
NSLog(@"urlResultString: %@", responseString);
NSString *match = [responseString stringByMatching:@"http[a-zA-Z0-9.:/]*"]; // Match the URL for the twitpic.com post
NSLog(@"match: %@", match);
// Send back notice to delegate
[delegate twitpicEngine:self didUploadImageWithResponse:match];
}
else
{
NSLog(@"Error while uploading, got 400 error back or no response at all: %@", [urlResponse statusCode]);
[delegate twitpicEngine:self didUploadImageWithResponse:nil]; // Nil should mean "upload failed" to the delegate
}
[pool release];
[responseString release];
[urlRequest release];
}
- (BOOL)uploadImageToTwitpic:(UIImage *)image withMessage:(NSString *)theMessage
username:(NSString *)username password:(NSString *)password
{
NSString *stringBoundary, *contentType, *message, *baseURLString, *urlString;
NSData *imageData = nil;
NSURL *url = nil;
NSMutableURLRequest *urlRequest = nil;
NSMutableData *postBody = nil;
// Create POST request from message, imageData, username and password
baseURLString = kTwitpicUploadURL;
urlString = [NSString stringWithFormat:@"%@", baseURLString];
url = [NSURL URLWithString:urlString];
urlRequest = [[[NSMutableURLRequest alloc] initWithURL:url] autorelease];
[urlRequest setHTTPMethod:@"POST"];
// Set the params
message = ([theMessage length] > 1) ? theMessage : kTwitpicMessageDefault;
imageData = UIImageJPEGRepresentation(image, kTwitpicImageJPEGCompression);
// Setup POST body
stringBoundary = [NSString stringWithString:@"0xKhTmLbOuNdArY"];
contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", stringBoundary];
[urlRequest addValue:contentType forHTTPHeaderField:@"Content-Type"];
// Setting up the POST request's multipart/form-data body
postBody = [NSMutableData data];
[postBody appendData:[[NSString stringWithFormat:@"\r\n\r\n--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"source\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"lighttable"] dataUsingEncoding:NSUTF8StringEncoding]]; // So Light Table show up as source in Twitter post
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"username\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:username] dataUsingEncoding:NSUTF8StringEncoding]]; // username
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"password\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:password] dataUsingEncoding:NSUTF8StringEncoding]]; // password
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"message\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:message] dataUsingEncoding:NSUTF8StringEncoding]]; // message
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"media\"; filename=\"%@\"\r\n", @"lighttable_twitpic_image.jpg" ] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"Content-Type: image/jpg\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; // jpeg as data
[postBody appendData:[[NSString stringWithString:@"Content-Transfer-Encoding: binary\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:imageData]; // Tack on the imageData to the end
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[urlRequest setHTTPBody:postBody];
// Spawn a new thread so the UI isn't blocked while we're uploading the image
[NSThread detachNewThreadSelector:@selector(uploadingDataWithURLRequest:) toTarget:self withObject:urlRequest];
return YES; // TODO: Should raise exception on error
}
#pragma mark -
#pragma mark Misc
- (void)dealloc
{
self.twitterMsgTextField = nil;
self.imageToSend = nil;
[super dealloc];
}
@end