简体   繁体   English

在Twitter上分享图片并从iOS原生应用程序获取收藏列表

[英]share image on twitter and get favourites list from iOS native app

I found many tutorials online how to share image on twitter from an iOS app. 我在网上找到了许多教程如何在iOS应用程序上分享图像。 But i want to know 2 things about social sharing with twitter- 但我想知道有关Twitter的社交分享的两件事 -

  1. If i post an image on twitter via my app, Can i get image id from twitter in callback method/block? 如果我通过我的应用程序在Twitter上发布图像,我可以在回调方法/块中从twitter获取图像ID吗? If yes then how? 如果是,那怎么样?
  2. If i fetch favourites of a user, is the response include text posted with that image? 如果我获取用户的收藏夹,响应是否包含与该图像一起发布的文本? I checked for the same on twitter Rest API doc that there is a text property returned in the response. 我在twitter Rest API doc上检查了相同的text ,即响应中返回了一个text属性。 Now my question is that if i post some text with image via my iOS app and later make this post favourite in twitter app and now i get my favourites list through twitter rest API in my app, does the text property in the response is same that i posted with my post? 现在我的问题是,如果我通过我的iOS应用程序发布一些带有图像的文本,然后在twitter应用程序中将此帖子收藏,现在我通过我的应用程序中的twitter rest API获取我的收藏列表,响应中的text属性是否相同我发布了我的帖子?

Edit about #1 above:- from SLComposeViewControllerResult docs i found that completion handler return one of 编辑上面的#1: -SLComposeViewControllerResult docs我发现完成处理程序返回一个

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

constant so there is no info about image just posted. 因此没有关于刚发布的图像的信息。 Am i right? 我对吗? If not please give me some reference about how to get image id please. 如果没有,请给我一些关于如何获取图片ID的参考。

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

Here is the code to share to twitter by using STTwitter library 这是使用STTwitter库分享到twitter的代码

 - (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 response And This is the code to get favorite list 对于您的第二个问题:是的,您将在响应中获得相同的文本。这是获取收藏列表的代码

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM