ios - 在 Twitter 上分享图片并从 iOS native 应用程序获取收藏夹列表

标签 ios objective-c twitter

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

  1. 如果我通过我的应用程序在 Twitter 上发布图片,我可以在回调方法/ block 中从 Twitter 获取图片 ID 吗?如果是,那么如何?
  2. 如果我获取用户的收藏夹,响应是否包含与该图像一起发布的文本?我在推特上查了同样的 Rest API doc响应中返回了一个 text 属性。 现在我的问题是,如果我通过我的 iOS 应用程序发布一些带有图像的文本,然后在 twitter 应用程序中将此帖子收藏,现在我通过我的应用程序中的 twitter rest API 获得我的收藏夹列表,text响应中的属性与我在帖子中发布的属性相同吗?

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

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

常量,因此没有关于刚刚发布的图像的信息。我对吗?如果没有,请给我一些关于如何获取图像 ID 的引用。

最佳答案

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);
    }];
}

关于ios - 在 Twitter 上分享图片并从 iOS native 应用程序获取收藏夹列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27525969/

相关文章:

c# - JSQMessagesViewController 找不到 heightForCellTopLabelAtIndexPath

ios - Cocoapods 'Charts' 安装问题

ios - 从 PerformBlock NPSPrivateQueueConcurrencyType 调用的方法中的 FetchRequest 失败

android - Twitter REST API v1即将停止运行

ios - 我如何在 iOS 5 和 iOS6 上使用 TWRequest

python - 尝试获取 Twitter 授权的访问 key 时出现 401 错误

ios - Firestore分页数据+快照监听器

ios - AutoLayout 问题 UITable View 标题部分 : Adding image in the left does not works fine

iOS : Best practices for Fields Validation on User Interface

ios - 仅制作一个 ios tabbar app 纵向选项卡