├── README ├── TCInstapaper.h └── TCInstapaper.m /README: -------------------------------------------------------------------------------- 1 | TCInstapaper is a very small, light and super easy to use Instapaper minimalist Objective-C wrapper for iOS and Mac OS (last one is not tested tho). 2 | 3 | For usage and detailed description please visit: http://totocaster.com/?p=43 4 | 5 | For information about license visit: http://totocaster.com/source-code-license/ -------------------------------------------------------------------------------- /TCInstapaper.h: -------------------------------------------------------------------------------- 1 | #import 2 | 3 | @class TCInstapaper; 4 | 5 | @protocol TCInstapaperDelegate 6 | - (void)instapaper:(TCInstapaper *)instapaper didDidFinishRequestWithCode:(NSUInteger)code; 7 | @end 8 | 9 | @interface TCInstapaper : NSObject { 10 | NSString *_username; 11 | NSString *_password; 12 | 13 | BOOL isAuthenticated; 14 | BOOL secure; 15 | 16 | id delegate; 17 | 18 | @private 19 | 20 | NSURLConnection *_connection; 21 | 22 | } 23 | 24 | @property (readonly) BOOL isAuthenticated; 25 | @property (readwrite) BOOL secure; 26 | @property (retain,readwrite) id delegate; 27 | 28 | - (id)initWithUsername:(NSString *)username; 29 | - (id)initWithUsername:(NSString *)username andPassword:(NSString *)password; 30 | 31 | - (void)authenticate; 32 | 33 | - (void)addURLString:(NSString *)urlString title:(NSString *)title; 34 | - (void)addURLString:(NSString *)urlString; 35 | 36 | 37 | @end 38 | -------------------------------------------------------------------------------- /TCInstapaper.m: -------------------------------------------------------------------------------- 1 | #import "TCInstapaper.h" 2 | 3 | @implementation TCInstapaper 4 | 5 | @synthesize delegate, isAuthenticated, secure; 6 | 7 | - (id)init 8 | { 9 | self = [super init]; 10 | if (self != nil) { 11 | isAuthenticated = NO; 12 | secure = YES; 13 | } 14 | return self; 15 | } 16 | 17 | - (id)initWithUsername:(NSString *)username 18 | { 19 | return [self initWithUsername:username andPassword:@""]; 20 | } 21 | 22 | - (id)initWithUsername:(NSString *)username andPassword:(NSString *)password 23 | { 24 | _username = username; 25 | _password = password; 26 | 27 | //NSLog(@"*** TCInstapaper: user = %@, password = %@",_username,_password); 28 | 29 | return [self init]; 30 | } 31 | 32 | 33 | - (void)authenticate 34 | { 35 | NSString *requestString = [NSString stringWithFormat:@"username=%@&password=%@",_username,_password]; 36 | //NSLog(@"*** TCInstapaper: %@",[requestString stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]); 37 | 38 | NSData *requestData = [NSData dataWithBytes:[requestString UTF8String] length:[requestString length]]; 39 | NSString *secureHTTP = (self.secure)?(@"https"):(@"http"); 40 | NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@://www.instapaper.com/api/authenticate",secureHTTP]]]; 41 | [request setHTTPBody:requestData]; 42 | [request setHTTPMethod:@"POST"]; 43 | [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"]; 44 | 45 | _connection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; 46 | 47 | } 48 | 49 | - (void)addURLString:(NSString *)urlString title:(NSString *)title 50 | { 51 | NSString *requestString = [NSString stringWithFormat:@"username=%@&password=%@&url=%@",_username,_password,urlString]; 52 | if (title) 53 | { 54 | requestString = [requestString stringByAppendingFormat:@"&title=%@",title]; 55 | } 56 | 57 | NSData *requestData = [NSData dataWithBytes:[requestString UTF8String] length:[requestString length]]; 58 | NSString *secureHTTP = (self.secure)?(@"https"):(@"http"); 59 | NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@://www.instapaper.com/api/add",secureHTTP]]]; 60 | [request setHTTPBody:requestData]; 61 | [request setHTTPMethod:@"POST"]; 62 | [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"]; 63 | 64 | _connection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; 65 | } 66 | 67 | - (void)addURLString:(NSString *)urlString 68 | { 69 | [self addURLString:urlString title:nil]; 70 | } 71 | 72 | #pragma mark - 73 | #pragma mark NSURLConnectionDelegate Methods 74 | 75 | - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 76 | { 77 | // TODO: error handling 78 | } 79 | 80 | - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { 81 | [_connection cancel]; 82 | _connection = nil; 83 | 84 | NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response; 85 | [self.delegate instapaper:self didDidFinishRequestWithCode:[httpResponse statusCode]]; 86 | 87 | } 88 | 89 | - (void)dealloc 90 | { 91 | [_connection release]; 92 | [super dealloc]; 93 | } 94 | 95 | 96 | 97 | @end 98 | --------------------------------------------------------------------------------