MGTwitterEngine - Fav a Tweet

后端 未结 1 1277
慢半拍i
慢半拍i 2021-01-22 21:50

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 whi

1条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-22 22:20

    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:

    1. Declare your class implements the MGTwitterEngineDelegate
    2. Implement atleast the following method to get status

      (void)statusesReceived:(NSArray *)statuses forRequest:(NSString *)connectionIdentifier
      
    3. The statuses array has a NSDictionary at the first position. Extract it as follows

      NSDictionary *status = (NSDictionary *)[statuses objectAtIndex:0];
      
    4. Extract two keys from the Dictionary "source_api_request_type" and "id". Save both of them as NSString values.

    5. 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
      
    6. 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)

    7. Back in your code you will use something similar to the following to send the tweet.

      [twitterEngine sendUpdate: @"My Tweet Text"]; 
      
    8. 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.

    9. 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];
              }
          }
      }
      
    10. 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.

    0 讨论(0)
提交回复
热议问题