Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ARC compatibility and fixed compatibility with current revision of OAuthConsumer #104

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions MGTwitterEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@
#import "MGTwitterEngineDelegate.h"
#import "MGTwitterParserDelegate.h"

#import "OAToken.h"

@class OAToken;

@interface MGTwitterEngine : NSObject <MGTwitterParserDelegate>
{
__weak NSObject <MGTwitterEngineDelegate> *_delegate;
__unsafe_unretained NSObject <MGTwitterEngineDelegate> *_delegate;
NSMutableDictionary *_connections; // MGTwitterHTTPURLConnection objects
NSString *_clientName;
NSString *_clientVersion;
Expand Down
117 changes: 47 additions & 70 deletions MGTwitterEngine.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#import "MGTwitterEngine.h"
#import "MGTwitterHTTPURLConnection.h"
#import "OAuthConsumer.h"
#import <OAuthConsumer/OAuthConsumer.h>

#import "NSData+Base64.h"

Expand Down Expand Up @@ -79,9 +79,9 @@ @implementation NSDictionary (MGTwitterEngineExtensions)
-(NSDictionary *)MGTE_dictionaryByRemovingObjectForKey:(NSString *)key{
NSDictionary *result = self;
if(key){
NSMutableDictionary *newParams = [[self mutableCopy] autorelease];
NSMutableDictionary *newParams = [self mutableCopy];
[newParams removeObjectForKey:key];
result = [[newParams copy] autorelease];
result = [newParams copy];
}
return result;
}
Expand Down Expand Up @@ -139,7 +139,7 @@ @implementation MGTwitterEngine

+ (MGTwitterEngine *)twitterEngineWithDelegate:(NSObject *)theDelegate
{
return [[[self alloc] initWithDelegate:theDelegate] autorelease];
return [[self alloc] initWithDelegate:theDelegate];
}


Expand All @@ -148,11 +148,11 @@ - (MGTwitterEngine *)initWithDelegate:(NSObject *)newDelegate
if ((self = [super init])) {
_delegate = newDelegate; // deliberately weak reference
_connections = [[NSMutableDictionary alloc] initWithCapacity:0];
_clientName = [DEFAULT_CLIENT_NAME retain];
_clientVersion = [DEFAULT_CLIENT_VERSION retain];
_clientURL = [DEFAULT_CLIENT_URL retain];
_clientSourceToken = [DEFAULT_CLIENT_TOKEN retain];
_APIDomain = [TWITTER_DOMAIN retain];
_clientName = DEFAULT_CLIENT_NAME;
_clientVersion = DEFAULT_CLIENT_VERSION;
_clientURL = DEFAULT_CLIENT_URL;
_clientSourceToken = DEFAULT_CLIENT_TOKEN;
_APIDomain = TWITTER_DOMAIN;
#if YAJL_AVAILABLE || TOUCHJSON_AVAILABLE
_searchDomain = [TWITTER_SEARCH_DOMAIN retain];
#endif
Expand All @@ -173,20 +173,10 @@ - (void)dealloc
_delegate = nil;

[[_connections allValues] makeObjectsPerformSelector:@selector(cancel)];
[_connections release];

[_username release];
[_password release];
[_clientName release];
[_clientVersion release];
[_clientURL release];
[_clientSourceToken release];
[_APIDomain release];

#if YAJL_AVAILABLE || TOUCHJSON_AVAILABLE
[_searchDomain release];
#endif

[super dealloc];
}


Expand All @@ -209,54 +199,49 @@ + (NSString *)version

- (NSString *)clientName
{
return [[_clientName retain] autorelease];
return _clientName;
}


- (NSString *)clientVersion
{
return [[_clientVersion retain] autorelease];
return _clientVersion;
}


- (NSString *)clientURL
{
return [[_clientURL retain] autorelease];
return _clientURL;
}


- (NSString *)clientSourceToken
{
return [[_clientSourceToken retain] autorelease];
return _clientSourceToken;
}


- (void)setClientName:(NSString *)name version:(NSString *)version URL:(NSString *)url token:(NSString *)token;
{
[_clientName release];
_clientName = [name retain];
[_clientVersion release];
_clientVersion = [version retain];
[_clientURL release];
_clientURL = [url retain];
[_clientSourceToken release];
_clientSourceToken = [token retain];
_clientName = name;
_clientVersion = version;
_clientURL = url;
_clientSourceToken = token;
}


- (NSString *)APIDomain
{
return [[_APIDomain retain] autorelease];
return _APIDomain;
}


- (void)setAPIDomain:(NSString *)domain
{
[_APIDomain release];
if (!domain || [domain length] == 0) {
_APIDomain = [TWITTER_DOMAIN retain];
_APIDomain = TWITTER_DOMAIN;
} else {
_APIDomain = [domain retain];
_APIDomain = domain;
}
}

Expand Down Expand Up @@ -360,7 +345,7 @@ - (NSDateFormatter *)_HTTPDateFormatter
{
// Returns a formatter for dates in HTTP format (i.e. RFC 822, updated by RFC 1123).
// e.g. "Sun, 06 Nov 1994 08:49:37 GMT"
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
//[dateFormatter setDateFormat:@"%a, %d %b %Y %H:%M:%S GMT"]; // won't work with -init, which uses new (unicode) format behaviour.
[dateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];
[dateFormatter setDateFormat:@"EEE, dd MMM yyyy HH:mm:ss GMT"];
Expand Down Expand Up @@ -412,12 +397,12 @@ - (NSString *)_dateToHTTP:(NSDate *)date

- (NSString *)_encodeString:(NSString *)string
{
NSString *result = (NSString *)CFURLCreateStringByAddingPercentEscapes(NULL,
(CFStringRef)string,
NSString *result = (__bridge_transfer NSString *)CFURLCreateStringByAddingPercentEscapes(NULL,
(__bridge CFStringRef)string,
NULL,
(CFStringRef)@";/?:@&=$+{}<>,",
kCFStringEncodingUTF8);
return [result autorelease];
return result;
}


Expand Down Expand Up @@ -448,7 +433,6 @@ - (NSString *)getImageAtURL:(NSString *)urlString
return nil;
} else {
[_connections setObject:connection forKey:[connection identifier]];
[connection release];
}

if ([self _isValidDelegateForSelector:@selector(connectionStarted:)])
Expand Down Expand Up @@ -519,7 +503,6 @@ -(NSString*)_sendRequest:(NSURLRequest *)theRequest withRequestType:(MGTwitterRe
return nil;
} else {
[_connections setObject:connection forKey:[connection identifier]];
[connection release];
}

if ([self _isValidDelegateForSelector:@selector(connectionStarted:)])
Expand Down Expand Up @@ -581,7 +564,6 @@ - (NSString *)_sendDataRequestWithMethod:(NSString *)method
return nil;
} else {
[_connections setObject:connection forKey:[connection identifier]];
[connection release];
}

if ([self _isValidDelegateForSelector:@selector(connectionStarted:)])
Expand Down Expand Up @@ -668,12 +650,12 @@ - (NSMutableURLRequest *)_baseRequestWithMethod:(NSString *)method
// Construct an NSMutableURLRequest for the URL and set appropriate request method.
NSMutableURLRequest *theRequest = nil;
if(_accessToken){
theRequest = [[[OAMutableURLRequest alloc] initWithURL:finalURL
consumer:[[[OAConsumer alloc] initWithKey:[self consumerKey]
secret:[self consumerSecret]] autorelease]
theRequest = [[OAMutableURLRequest alloc] initWithURL:finalURL
consumer:[[OAConsumer alloc] initWithKey:[self consumerKey]
secret:[self consumerSecret]]
token:_accessToken
realm:nil
signatureProvider:nil] autorelease];
signatureProvider:nil];
[theRequest setCachePolicy:NSURLRequestReloadIgnoringCacheData ];
[theRequest setTimeoutInterval:URL_REQUEST_TIMEOUT];
}else{
Expand Down Expand Up @@ -782,8 +764,8 @@ - (void)_parseDataForConnection:(MGTwitterHTTPURLConnection *)connection
#else
- (void)_parseDataForConnection:(MGTwitterHTTPURLConnection *)connection
{
NSString *identifier = [[[connection identifier] copy] autorelease];
NSData *xmlData = [[[connection data] copy] autorelease];
NSString *identifier = [[connection identifier] copy];
NSData *xmlData = [[connection data] copy];
MGTwitterRequestType requestType = [connection requestType];
MGTwitterResponseType responseType = [connection responseType];

Expand Down Expand Up @@ -863,10 +845,12 @@ - (void)_parseDataForConnection:(MGTwitterHTTPURLConnection *)connection
[MGTwitterSocialGraphParser parserWithXML:xmlData delegate:self
connectionIdentifier:identifier requestType:requestType
responseType:responseType];
case MGTwitterOAuthToken:;
OAToken *token = [[[OAToken alloc] initWithHTTPResponseBody:[[[NSString alloc] initWithData:xmlData encoding:NSUTF8StringEncoding] autorelease]] autorelease];
case MGTwitterOAuthToken:
{
OAToken *token = [[OAToken alloc] initWithHTTPResponseBody:[[NSString alloc] initWithData:xmlData encoding:NSUTF8StringEncoding]];
[self parsingSucceededForRequest:identifier ofResponseType:requestType
withParsedObjects:[NSArray arrayWithObject:token]];
}
default:
break;
}
Expand Down Expand Up @@ -1092,7 +1076,7 @@ - (void)connectionDidFinishLoading:(MGTwitterHTTPURLConnection *)connection
#if TARGET_OS_IPHONE
UIImage *image = [[[UIImage alloc] initWithData:[connection data]] autorelease];
#else
NSImage *image = [[[NSImage alloc] initWithData:[connection data]] autorelease];
NSImage *image = [[NSImage alloc] initWithData:[connection data]];
#endif

// Inform delegate.
Expand Down Expand Up @@ -2053,29 +2037,26 @@ @implementation MGTwitterEngine (BasicAuth)

- (NSString *)username
{
return [[_username retain] autorelease];
return _username;
}

- (void)setUsername:(NSString *)newUsername
{
// Set new credentials.
[_username release];
_username = [newUsername retain];
_username = newUsername;
}

- (NSString *)password
{
return [[_password retain] autorelease];
return _password;
}


- (void)setUsername:(NSString *)newUsername password:(NSString *)newPassword
{
// Set new credentials.
[_username release];
_username = [newUsername retain];
[_password release];
_password = [newPassword retain];
_username = newUsername;
_password = newPassword;

if ([self clearsCookies]) {
// Remove all cookies for twitter, to ensure next connection uses new credentials.
Expand All @@ -2098,10 +2079,8 @@ - (void)setUsername:(NSString *)newUsername password:(NSString *)newPassword
@implementation MGTwitterEngine (OAuth)

- (void)setConsumerKey:(NSString *)key secret:(NSString *)secret{
[_consumerKey autorelease];
_consumerKey = [key copy];

[_consumerSecret autorelease];
_consumerSecret = [secret copy];
}

Expand All @@ -2114,8 +2093,7 @@ - (NSString *)consumerSecret{
}

- (void)setAccessToken: (OAToken *)token{
[_accessToken autorelease];
_accessToken = [token retain];
_accessToken = token;
}

- (OAToken *)accessToken{
Expand All @@ -2124,7 +2102,7 @@ - (OAToken *)accessToken{

- (NSString *)getXAuthAccessTokenForUsername:(NSString *)username
password:(NSString *)password{
OAConsumer *consumer = [[[OAConsumer alloc] initWithKey:[self consumerKey] secret:[self consumerSecret]] autorelease];
OAConsumer *consumer = [[OAConsumer alloc] initWithKey:[self consumerKey] secret:[self consumerSecret]];

OAMutableURLRequest *request = [[OAMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"https://api.twitter.com/oauth/access_token"]
consumer:consumer
Expand All @@ -2135,9 +2113,9 @@ - (NSString *)getXAuthAccessTokenForUsername:(NSString *)username
[request setHTTPMethod:@"POST"];

[request setParameters:[NSArray arrayWithObjects:
[OARequestParameter requestParameter:@"x_auth_mode" value:@"client_auth"],
[OARequestParameter requestParameter:@"x_auth_username" value:username],
[OARequestParameter requestParameter:@"x_auth_password" value:password],
[OARequestParameter requestParameterWithName:@"x_auth_mode" value:@"client_auth"],
[OARequestParameter requestParameterWithName:@"x_auth_username" value:username],
[OARequestParameter requestParameterWithName:@"x_auth_password" value:password],
nil]];

// Create a connection using this request, with the default timeout and caching policy,
Expand All @@ -2148,13 +2126,12 @@ - (NSString *)getXAuthAccessTokenForUsername:(NSString *)username
requestType:MGTwitterOAuthTokenRequest
responseType:MGTwitterOAuthToken];

[request release], request = nil;
request = nil;

if (!connection) {
return nil;
} else {
[_connections setObject:connection forKey:[connection identifier]];
[connection release];
}

if ([self _isValidDelegateForSelector:@selector(connectionStarted:)])
Expand Down
8 changes: 7 additions & 1 deletion MGTwitterEngine.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,11 @@
isa = PBXProject;
buildConfigurationList = C01FCF4E08A954540054247B /* Build configuration list for PBXProject "MGTwitterEngine" */;
compatibilityVersion = "Xcode 3.0";
developmentRegion = English;
hasScannedForEncodings = 1;
knownRegions = (
en,
);
mainGroup = 29B97314FDCFA39411CA2CEA /* MATwitterIntegration */;
projectDirPath = "";
projectRoot = "";
Expand Down Expand Up @@ -716,8 +720,9 @@
"\"$(SRCROOT)/yajl/build/yajl-1.0.7/lib\"",
"\"$(SRCROOT)\"",
);
ONLY_ACTIVE_ARCH_PRE_XCODE_3_1 = "$(NATIVE_ARCH_ACTUAL)";
ONLY_ACTIVE_ARCH_PRE_XCODE_3_1 = "$(NATIVE_ARCH_64)";
PRODUCT_NAME = MGTwitterEngine;
SDKROOT = macosx;
WRAPPER_EXTENSION = app;
ZERO_LINK = YES;
};
Expand All @@ -742,6 +747,7 @@
"\"$(SRCROOT)\"",
);
PRODUCT_NAME = MGTwitterEngine;
SDKROOT = macosx;
WRAPPER_EXTENSION = app;
};
name = Release;
Expand Down
Loading