MGTwitterEngine - Fav a Tweet
I am attempting to favorite a tweet using MGTwitterEngine
I am using "Tweet" a sub-class I made which handles the user ids, names, etc. So I put that into a string which then gets converted to a number that can be used to handle the act of fav. a tweet
My Code: http://pastie.org/开发者_如何学Go1467311
This is a very old post and not sure if anyone is looking out for it, but I managed to do this exact thing today after some 'hit-and-miss'. Here is what you have to do:
- Declare your class implements the MGTwitterEngineDelegate
Implement atleast the following method to get status
(void)statusesReceived:(NSArray *)statuses forRequest:(NSString *)connectionIdentifier
The statuses array has a NSDictionary at the first position. Extract it as follows
NSDictionary *status = (NSDictionary *)[statuses objectAtIndex:0];
Extract two keys from the Dictionary "source_api_request_type" and "id". Save both of them as NSString values.
Update the MGTwitterEngine.h and MGTwitterEngine.m to change method signature of the markUpdate method to send the updateID as a NSString instead of unsigned int. It will look similar to the following after change:
(NSString *)markUpdate:(NSString *)updateID asFavorite:(BOOL)flag; // favorites/create, favorites/destroy
Change the
%u
in markUpdate method to%@
so that the input parameter change applies correctly. (You have to make the change at two places in the method)Back in your code you will use something similar to the following to send the tweet.
[twitterEngine sendUpdate: @"My Tweet Text"];
This will raise the statusRecieved event once the tweet is posted successfully. In statusRecieved event as mentioned earlier we need two values the tweetId and the request type.
Use the following code to check if request Type == 5, and if it is call the markUpdate method by passing the values the tweet Id and boolean value of YES to favorite (or NO to un-favorite) the tweet. Your code will look like this:
(void)statusesReceived:(NSArray *)statuses forRequest:(NSString *)connectionIdentifier { if([statuses count] > 0) { NSDictionary *status = (NSDictionary *)[statuses objectAtIndex:0]; NSString *stringId = (NSString *)[status objectForKey:@"id"]; NSNumber *requestType = (NSNumber *)[status objectForKey:@"source_api_request_type"]; NSLog(@"Tweet ID String - %@ and Request Type: %@.", stringId, requestType); if ([requestType isEqualToNumber: [NSNumber numberWithInt: 5]]) { [twitterEngine markUpdate: stringId asFavorite:YES]; } } }
The secret sauce of 'request type' 5 is that a new tweet posting has 'api request id' of 5 and we want want to mark new tweets as favorite only. (When you watch the id after the tweet is marked favorite it will be status 26).
With iOS 5 looming MGTwitterEngine will soon be deprecated. But it was fun for me to figure this out in my own project. Hope someone finds it useful.
精彩评论