Skip to content

Instantly share code, notes, and snippets.

@mpociot
Created December 26, 2011 20:34
Show Gist options
  • Save mpociot/1522066 to your computer and use it in GitHub Desktop.
Save mpociot/1522066 to your computer and use it in GitHub Desktop.
Titanium Twitter module request method
-(void)request:(id)args
{
ENSURE_UI_THREAD(request,args);
NSDictionary * params = [args objectAtIndex:0];
NSString *urlString = [params objectForKey:@"url"];
ENSURE_STRING(urlString);
NSDictionary * paramDict = [params objectForKey:@"params"];
NSLog(@"[INFO] params is: %@",paramDict);
NSLog(@"[INFO] params class is: %@",[paramDict class]);
// NSString *method = [args objectForKey:@"method"];
id success = [params objectForKey:@"success"];
id error = [params objectForKey:@"error"];
RELEASE_TO_NIL(requestSuccessCallback);
RELEASE_TO_NIL(requestErrorCallback);
requestSuccessCallback = [success retain];
requestErrorCallback = [error retain];
// Do a simple search, using the Twitter API
TWRequest *request = [[TWRequest alloc] initWithURL:[NSURL URLWithString:urlString]
parameters:paramDict requestMethod:TWRequestMethodGET];
// Notice this is a block, it is the handler to process the response
[request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error)
{
if ([urlResponse statusCode] == 200)
{
// The response from Twitter is in JSON format
// Move the response into a dictionary and print
NSError *error;
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:&error];
if( requestSuccessCallback != nil ){
[self _fireEventToListener:@"requestSuccess" withObject:dict listener:requestSuccessCallback thisObject:nil];
}
}
else
{
if( requestErrorCallback != nil ){
[self _fireEventToListener:@"requestError" withObject:[args objectForKey:@"params"] listener:requestErrorCallback thisObject:nil];
}
}
}];
}
@mpociot
Copy link
Author

mpociot commented Dec 26, 2011

Example call:
module.request({
params: {
screen_name: 'marcelpociot',
count: 10
},
url: "http://api.twitter.com/1/statuses/user_timeline.json",
method: 'get',
success: function(e){
alert(e);
},
error: function(e){
alert(e);
}
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment