Share photos on Twitter and get your favorites from the native iOS app

I found a lot of tutorials online on how to share your Twitter image from an iOS app. But I want to know 2 things about social sharing with twitter -

  • If I post an image to twitter via my app, can I get the id of the image from twitter in a method / block callback? If so, how?
  • If I select a user's favorites, does the response include the text posted with that image? I checked for the same on Twitter Rest API doc that a property is returned in the response text

    . Now my question is, if I post a text with an image via an iOS app, and later make that post a favorite in the Twitter app, and now I get a list of favorites via the twitter API in my app, does the property text

    in the response have the same what did I send with my message?

Edit about # 1 above: - from SLComposeViewControllerResult Docs I found that the completion handler returns one of

typedef NS_ENUM (NSInteger,
   SLComposeViewControllerResult ) {
   SLComposeViewControllerResultCancelled,
   SLComposeViewControllerResultDone 
};

      

constant, so there is no information about the image just posted. I'm right? If not, please give me some link on how to get the image id please.

+3


source to share


1 answer


Here I have customize alertView,NSLog,etc. You ignore that.

      

Here is the code to share on twitter using the STTwitter library

 - (void)shareToTwitter
    {
        APP_DELEGATE.navController = self.navigationController;

        NSString *strTwitterToken       = [[NSUserDefaults standardUserDefaults] objectForKey:@"TwitterToken"];
        NSString *strTwitterTokenSecret = [[NSUserDefaults standardUserDefaults] objectForKey:@"TwitterTokenSecret"];

        if (strTwitterToken && strTwitterTokenSecret)
        {
            self.twitter = [STTwitterAPI twitterAPIWithOAuthConsumerKey:TwitterConsumerKey consumerSecret:TwitterSecretKey oauthToken:strTwitterToken oauthTokenSecret:strTwitterTokenSecret];

            [self.twitter verifyCredentialsWithSuccessBlock:^(NSString *username) {
                DLogs(@"Twitter User Name");

                [self twitterMediaUpload];

            } errorBlock:^(NSError *error) {
                DLogs(@"-- error: %@", error);
                [AppConstant showAutoDismissAlertWithMessage:[error localizedDescription] onView:self.view];

                [self safariLoginTwitter];
            }];
        }

        else
        {
            [self safariLoginTwitter];
        }

    }

    -(void)safariLoginTwitter
    {
    //    [APP_CONSTANT getNativeTwitterAccountAccessToken:^(id result) {
    //        
    //    }];

        self.twitter = [STTwitterAPI twitterAPIWithOAuthConsumerKey:TwitterConsumerKey
                                                     consumerSecret:TwitterSecretKey];

        [self.twitter postTokenRequest:^(NSURL *url, NSString *oauthToken) {
            DLogs(@"-- url: %@", url);
            DLogs(@"-- oauthToken: %@", oauthToken);

            [[UIApplication sharedApplication] openURL:url];
        } authenticateInsteadOfAuthorize:NO
                            forceLogin:@(YES)
                            screenName:nil
                         oauthCallback:@"myapp://twitter_access_tokens/"
                            errorBlock:^(NSError *error) {
                                DLogs(@"-- error: %@", error);
                                [AppConstant showAutoDismissAlertWithMessage:[error localizedDescription] onView:self.view];
                            }];
    }

    - (void)setOAuthToken:(NSString *)token oauthVerifier:(NSString *)verifier {

        [self.twitter postAccessTokenRequestWithPIN:verifier successBlock:^(NSString *oauthToken, NSString *oauthTokenSecret, NSString *userID, NSString *screenName) {
            DLogs(@"-- screenName: %@", screenName);

            /*
             At this point, the user can use the API and you can read his access tokens with:

             _twitter.oauthAccessToken;
             _twitter.oauthAccessTokenSecret;

             You can store these tokens (in user default, or in keychain) so that the user doesn't need to authenticate again on next launches.

             Next time, just instanciate STTwitter with the class method:

             +[STTwitterAPI twitterAPIWithOAuthConsumerKey:consumerSecret:oauthToken:oauthTokenSecret:]

             Don't forget to call the -[STTwitter verifyCredentialsWithSuccessBlock:errorBlock:] after that.
             */

            [[NSUserDefaults standardUserDefaults] setObject:self.twitter.oauthAccessToken forKey:@"TwitterToken"];
            [[NSUserDefaults standardUserDefaults] setObject:self.twitter.oauthAccessToken forKey:@"TwitterTokenSecret"];
            [[NSUserDefaults standardUserDefaults] synchronize];

            [self twitterMediaUpload];

        } errorBlock:^(NSError *error) {

            [AppConstant showAutoDismissAlertWithMessage:[error localizedDescription] onView:self.view];
            DLogs(@"-- %@", [error localizedDescription]);
        }];
    }

    -(void)twitterMediaUpload
    {
        //    ProfileImageBO *objProfImg = nil;
        //
        //    if ([self.objProfile.arrUserImages count]) {
        //        objProfImg = [self.objProfile.arrUserImages objectAtIndex:0];
        //    }

        [APP_CONSTANT showLoaderWithTitle:@"posting" onView:self.view];

        //    NSURL *urlProfImg = [NSURL URLWithString:[objProfImg.imageUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

        NSURL *screenshotUrl = [self getScreenshotUrl];

        [self.twitter postMediaUpload:screenshotUrl uploadProgressBlock:^(NSInteger bytesWritten, NSInteger totalBytesWritten, NSInteger totalBytesExpectedToWrite) {
            DLogs(@"uploading");
        } successBlock:^(NSDictionary *imageDictionary, NSString *mediaID, NSString *size) {
            DLogs(@"imageDictionary =  %@, mediaID = %@, size %@",imageDictionary.description,mediaID,size);

            [self postToTheTwitterWithMediaId:mediaID];

        } errorBlock:^(NSError *error) {
            DLogs(@"Error in uploading media, try again ...");

            [APP_CONSTANT hideLoader];
            [AppConstant showAutoDismissAlertWithMessage:error.localizedDescription onView:self.view];
        }];
    }

    -(void)postToTheTwitterWithMediaId:(NSString *)mediaID
    {
        NSString *msg = [NSString stringWithFormat:@"Check out My Profile"];

        [self.twitter postStatusUpdate:msg inReplyToStatusID:nil mediaIDs:[NSArray arrayWithObject:mediaID] latitude:nil longitude:nil placeID:nil displayCoordinates:nil trimUser:nil successBlock:^(NSDictionary *status) {
            DLogs(@"Description %@",status.description);

            [self showNotificationToastWithMessage:TwitterPostSuccess];
            [APP_CONSTANT hideLoader];

        } errorBlock:^(NSError *error) {
            DLogs(@"Twitter posting error %@",error.description);
            [APP_CONSTANT hideLoader];

            [AppConstant showAutoDismissAlertWithMessage:error.localizedDescription onView:self.view];
        }];

    }

      



For your second question: yes, you will get the same text in the answer And this is the code for getting the favorites list

-(void)getFavListTwitter
{
    [self.twitter getFavoritesListWithSuccessBlock:^(NSArray *statuses) {
        DLogs(@"%@",statuses.description);
    } errorBlock:^(NSError *error) {
        DLogs(@"%@",error.description);
    }];
}

      

+5


source







All Articles