【问题标题】:share image on twitter and get favourites list from iOS native app在 twitter 上分享图片并从 iOS 原生应用中获取收藏列表
【发布时间】:2014-12-17 12:45:34
【问题描述】:

我在网上找到了很多关于如何通过 iOS 应用在 Twitter 上分享图片的教程。但我想知道关于 twitter 社交分享的两件事-

  1. 如果我通过我的应用在 twitter 上发布图像,我可以在回调方法/块中从 twitter 获取图像 id 吗?如果是,那怎么办?
  2. 如果我获取用户的收藏夹,响应是否包含随该图像发布的文本?我在 twitter Rest API doc 上检查了相同的结果,响应中返回了 text 属性。 现在我的问题是,如果我通过我的 iOS 应用程序发布一些带有图像的文本,然后在 twitter 应用程序中将此帖子设为收藏夹,现在我通过我的应用程序中的 twitter rest API 获取我的收藏夹列表,那么响应中的 text 属性是否为和我在帖子中发布的一样吗?

编辑上面的#1:-来自SLComposeViewControllerResult docs我发现完成处理程序返回一个

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

常数,所以没有关于刚刚发布的图像的信息。我对吗?如果没有,请给我一些关于如何获取图像 id 的参考。

【问题讨论】:

  • 我使用 STTwitter 获得了媒体 ID
  • 好的,试一试。你对我的第二个问题有任何想法吗?它比媒体 ID 更重要。
  • 是的,您将在响应中得到相同的文本。我已经编辑了我的答案。检查

标签: ios objective-c twitter


【解决方案1】:
Here I have customize alertView,NSLog,etc. You ignore that.

这是使用 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];
        }];

    }

对于您的第二个问题:是的,您将在回复中得到相同的文本 这是获取收藏列表的代码

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-30
    • 1970-01-01
    • 1970-01-01
    • 2013-11-26
    • 2015-05-13
    • 1970-01-01
    • 2021-01-27
    • 2015-08-17
    相关资源
    最近更新 更多