From 40c8e375c7e5cadc92f0ca796b32387d40a574f2 Mon Sep 17 00:00:00 2001 From: Damien Glancy Date: Sun, 11 Dec 2011 12:56:37 +0000 Subject: [PATCH] Changes made to TKTumblrKit: 1. Allow compiling and usage on iOS and OS X (previously was OS X only) 2. Fixed 15 memory leaks 3. Fixed [TKTumblr tumblelogs] to return an array of the users Tumblelogs. Introduced optional protocol delegate "(void)tumblrDidReturnTumblelogs:(NSArray *)tumblelogs" 4. Introduced an authenticate function and delegate callback to check if a users login/password is correct. --- NSDictionary+TumblrKit.h | 5 ++++ NSDictionary+TumblrKit.m | 1 + TKPost.h | 11 ++++++++ TKPost.m | 35 +++++++++++++----------- TKTumblelog.h | 5 ++++ TKTumblr.h | 14 ++++++++-- TKTumblr.m | 58 +++++++++++++++++++++++++++++++++++++--- 7 files changed, 109 insertions(+), 20 deletions(-) diff --git a/NSDictionary+TumblrKit.h b/NSDictionary+TumblrKit.h index 8705eda..c86d07e 100644 --- a/NSDictionary+TumblrKit.h +++ b/NSDictionary+TumblrKit.h @@ -20,7 +20,12 @@ // THE SOFTWARE. // +#import +#if (TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR) +#import +#else #import +#endif /** TumblrKit's additions to NSDictionary. diff --git a/NSDictionary+TumblrKit.m b/NSDictionary+TumblrKit.m index 30381a6..51bac3e 100644 --- a/NSDictionary+TumblrKit.m +++ b/NSDictionary+TumblrKit.m @@ -56,6 +56,7 @@ - (NSData *)multipartMIMEData } [result appendData:[[NSString stringWithFormat:@"--%@--\r\n", [NSString MIMEBoundary]] dataUsingEncoding:NSUTF8StringEncoding]]; + [dict_ release]; return result; } diff --git a/TKPost.h b/TKPost.h index bae658a..a05bb31 100644 --- a/TKPost.h +++ b/TKPost.h @@ -195,7 +195,13 @@ typedef enum { NSMutableString *caption; NSString *source; + + #if (TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR) + UIImage *image; + #else NSImage *image; + #endif + NSUInteger width; NSUInteger height; } @@ -203,7 +209,12 @@ typedef enum @property (assign) NSUInteger width; @property (assign) NSUInteger height; @property (copy) NSString *source; + +#if (TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR) +@property (retain) UIImage *image; +#else @property (retain) NSImage *image; +#endif - (NSString *)caption; - (void)setCaption:(NSString *)aCaption; diff --git a/TKPost.m b/TKPost.m index 881d8f7..7f95c85 100644 --- a/TKPost.m +++ b/TKPost.m @@ -138,7 +138,7 @@ + (id)postWithAttributes:(NSDictionary *)attributeDict } } - return [(TKPost *)[postClass alloc] initWithAttributes:attributeDict]; + return [[(TKPost *)[postClass alloc] initWithAttributes:attributeDict] autorelease]; } - (NSString *)typeAsString @@ -179,7 +179,7 @@ - (void)dealloc - (NSString *)title { - return [title copy]; + return [[title copy] autorelease]; } - (void)setTitle:(NSString *)aTitle @@ -192,7 +192,7 @@ - (void)setTitle:(NSString *)aTitle - (NSString *)body { - return [body copy]; + return [[body copy] autorelease]; } - (void)setBody:(NSString *)aBody @@ -252,7 +252,7 @@ - (void)dealloc - (NSString *)text { - return [text copy]; + return [[text copy] autorelease]; } - (void)setText:(NSString *)aText @@ -311,7 +311,7 @@ - (void)dealloc - (NSString *)text { - return [text copy]; + return [[text copy] autorelease]; } - (void)setText:(NSString *)aText @@ -324,7 +324,7 @@ - (void)setText:(NSString *)aText - (NSString *)source { - return [source copy]; + return [[source copy] autorelease]; } - (void)setSource:(NSString *)aSource @@ -380,7 +380,7 @@ - (void)dealloc - (NSString *)text { - return [text copy]; + return [[text copy] autorelease]; } - (void)setText:(NSString *)aText @@ -449,7 +449,7 @@ - (void)dealloc - (NSString *)caption { - return [caption copy]; + return [[caption copy] autorelease]; } - (void)setCaption:(NSString *)aCaption @@ -476,8 +476,13 @@ - (NSDictionary *)attributesAsDictionary if (source != nil) [dict setObject:source forKey:@"source"]; if (image != nil && source == nil) { - NSBitmapImageRep *bitmap = [[image representations] objectAtIndex:0]; - [dict setObject:[bitmap representationUsingType:NSJPEGFileType properties:nil] forKey:@"data"]; + #if (TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR) + NSData *data = UIImageJPEGRepresentation(image, 1.0); + [dict setObject:data forKey:@"data"]; + #else + NSBitmapImageRep *bitmap = [[image representations] objectAtIndex:0]; + [dict setObject:[bitmap representationUsingType:NSJPEGFileType properties:nil] forKey:@"data"]; + #endif } [dict setObject:caption forKey:@"caption"]; @@ -510,7 +515,7 @@ - (void)dealloc - (NSString *)caption { - return [caption copy]; + return [[caption copy] autorelease]; } - (void)setCaption:(NSString *)aCaption @@ -523,7 +528,7 @@ - (void)setCaption:(NSString *)aCaption - (NSString *)source { - return [source copy]; + return [[source copy] autorelease]; } - (void)setSource:(NSString *)aSource @@ -536,7 +541,7 @@ - (void)setSource:(NSString *)aSource - (NSString *)player { - return [player copy]; + return [[player copy] autorelease]; } - (void)setPlayer:(NSString *)aPlayer @@ -591,7 +596,7 @@ - (void)dealloc - (NSString *)caption { - return [caption copy]; + return [[caption copy] autorelease]; } - (void)setCaption:(NSString *)aCaption @@ -604,7 +609,7 @@ - (void)setCaption:(NSString *)aCaption - (NSString *)player { - return [player copy]; + return [[player copy] autorelease]; } - (void)setPlayer:(NSString *)aPlayer diff --git a/TKTumblelog.h b/TKTumblelog.h index 1e55e0e..d99140d 100644 --- a/TKTumblelog.h +++ b/TKTumblelog.h @@ -20,7 +20,12 @@ // THE SOFTWARE. // +#import +#if (TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR) +#import +#else #import +#endif typedef enum { diff --git a/TKTumblr.h b/TKTumblr.h index fb1097b..e05abbe 100644 --- a/TKTumblr.h +++ b/TKTumblr.h @@ -41,7 +41,9 @@ typedef enum - (void)tumblrDidUploadPost:(TKPost *)thePost withDomain:(NSString *)theDomain postID:(NSNumber *)thePostID; - (void)tumblrDidFailToUploadPost:(TKPost *)thePost withDomain:(NSString *)theDomain returnCode:(TKTumblrResponseReturnCode)theReturnCode; - (void)tumblrDidReceiveTumblelog:(TKTumblelog *)theTumblelog; - +- (void)tumblrDidReturnTumblelogs:(NSArray *)tumblelogs; +- (void)tumblrUserAuthenticated; +- (void)tumblrUserNotAuthenticated; @end @interface TKTumblr : NSObject @@ -51,19 +53,26 @@ typedef enum NSString *email; NSString *password; + NSMutableArray *usersTumblelogs; TKTumblelog *currentTumblelog; TKPost *currentPost; TKPost *requestedPost; NSString *currentElementName; + + BOOL authenticationRequestInProgress; + BOOL userAuthenticated; } @property (assign) id delegate; @property (nonatomic,copy) NSString *email; @property (nonatomic,copy) NSString *password; +@property (nonatomic, retain) NSMutableArray *usersTumblelogs; @property (nonatomic,retain) TKTumblelog *currentTumblelog; @property (nonatomic,retain) TKPost *currentPost; @property (nonatomic,retain) TKPost *requestedPost; @property (nonatomic,copy) NSString *currentElementName; +@property (assign) BOOL userAuthenticated; +@property (assign) BOOL authenticationRequestInProgress; - (id)initWithEmail:(NSString *)theEmail andPassword:(NSString *)thePassword; - (BOOL)uploadPost:(TKPost *)thePost; @@ -71,6 +80,7 @@ typedef enum - (void)postsWithReadRequest:(TKTumblrReadRequest *)theReadRequest; - (TKPost *)postWithID:(NSNumber *)thePostID andDomain:(NSString *)theDomain; - (NSDictionary *)attributesAsDictionary; -- (NSArray *)tumblelogs; +- (void)tumblelogs; +- (void)authenticate; @end diff --git a/TKTumblr.m b/TKTumblr.m index 2f534ba..317ed97 100644 --- a/TKTumblr.m +++ b/TKTumblr.m @@ -27,7 +27,7 @@ @implementation TKTumblr -@synthesize delegate, email, password, currentPost, currentElementName, requestedPost, currentTumblelog; +@synthesize delegate, email, password, currentPost, currentElementName, requestedPost, currentTumblelog, usersTumblelogs, authenticationRequestInProgress, userAuthenticated; - (id)initWithEmail:(NSString *)theEmail andPassword:(NSString *)thePassword { @@ -38,6 +38,7 @@ - (id)initWithEmail:(NSString *)theEmail andPassword:(NSString *)thePassword self.currentPost = nil; self.currentElementName = nil; self.requestedPost = nil; + self.usersTumblelogs = [[[NSMutableArray alloc] init] autorelease]; } return self; } @@ -50,6 +51,7 @@ - (void)dealloc self.currentPost = nil; self.currentElementName = nil; self.requestedPost = nil; + self.usersTumblelogs = nil; [super dealloc]; } @@ -154,7 +156,7 @@ - (NSDictionary *)attributesAsDictionary return dict; } -- (NSArray *)tumblelogs +- (void)tumblelogs { NSString *theURLString = [NSString stringWithFormat:@"https://www.tumblr.com/api/authenticate?email=%@&password=%@", [email stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding], @@ -164,8 +166,18 @@ - (NSArray *)tumblelogs [parser setDelegate:self]; [parser parse]; [parser release]; +} - return nil; +- (void)authenticate { + NSString *theURLString = [NSString stringWithFormat:@"https://www.tumblr.com/api/authenticate?email=%@&password=%@", + [email stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding], + [password stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; + NSURL *theURL = [NSURL URLWithString:theURLString]; + NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:theURL]; + self.authenticationRequestInProgress = YES; + [parser setDelegate:self]; + [parser parse]; + [parser release]; } #pragma mark NSXMLParserDelegate @@ -173,6 +185,12 @@ - (NSArray *)tumblelogs - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict { self.currentElementName = elementName; + + if ([elementName isEqualToString:@"user"] && authenticationRequestInProgress) { + if (authenticationRequestInProgress) { + self.userAuthenticated = YES; + } + } if ([elementName isEqualToString:@"post"]) { self.currentPost = [TKPost postWithAttributes:attributeDict]; @@ -184,6 +202,12 @@ - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName nam - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName { + if ([elementName isEqualToString:@"user"] && authenticationRequestInProgress && userAuthenticated) { + if (delegate && [delegate respondsToSelector:@selector(tumblrUserAuthenticated)]) { + [delegate tumblrUserAuthenticated]; + } + } + if ([elementName isEqualToString:@"post"]) { if (currentPost != nil) { if (delegate && [delegate respondsToSelector:@selector(tumblrDidReceivePost:withDomain:)]) { @@ -197,6 +221,7 @@ - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName names if (delegate && [delegate respondsToSelector:@selector(tumblrDidReceiveTumblelog:)]) { [delegate tumblrDidReceiveTumblelog:currentTumblelog]; } + [self.usersTumblelogs addObject:currentTumblelog]; } self.currentTumblelog = nil; } @@ -223,6 +248,33 @@ - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string } } +- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError { + if (authenticationRequestInProgress) { + self.userAuthenticated = NO; + if (delegate && [delegate respondsToSelector:@selector(tumblrUserNotAuthenticated)]) { + [delegate tumblrUserNotAuthenticated]; + } + } +} + +- (void)parserDidStartDocument:(NSXMLParser *)parser { + [self.usersTumblelogs removeAllObjects]; + self.userAuthenticated = NO; +} + +- (void)parserDidEndDocument:(NSXMLParser *)parser { + if ([self.usersTumblelogs count]>0) { + if (delegate && [delegate respondsToSelector:@selector(tumblrDidReturnTumblelogs:)]) { + [delegate tumblrDidReturnTumblelogs:self.usersTumblelogs]; + [self.usersTumblelogs removeAllObjects]; + } + } + + if (authenticationRequestInProgress) { + authenticationRequestInProgress = NO; + } +} + #pragma mark TKTumblrDelegate - (void)tumblrDidReceivePost:(TKPost *)thePost withDomain:(NSString *)theDomain