在twitter上分享一个简单的文字

时间:2015-08-17 11:06:04

标签: ios objective-c twitter

我正在努力分享功能,我想在Twitter上分享一些简单的文字。我知道我们同时使用Compose和Account Twitter功能,这些功能已在设置中注册。我找到了一个共享媒体的样本,但我没有媒体:只是简单的文字。任何人都可以帮我找到分享简单文字所需的请求网址吗?

UIImage *image = [UIImage imageNamed:@"shareit.png"];
NSData *imageData = UIImageJPEGRepresentation(image, 0.7);
NSString *statusesShowEndpoint = @"https://upload.twitter.com/1.1/media/upload.json"; // (This url is used to share media, but I want text rather than an image.)
NSDictionary *params = @{@"media" : imageData}; // **** (Don't know the parameter for share text)
NSError *clientError;
NSURLRequest *request = [[[Twitter sharedInstance] APIClient] 
                         URLRequestWithMethod:@"POST"
                         URL:statusesShowEndpoint
                         parameters:params
                         error:&clientError];

if (request) {
    [[[Twitter sharedInstance] APIClient]
     sendTwitterRequest:request
     completion:^(NSURLResponse *response,
                  NSData *data,
                  NSError *connectionError) {
         if (data) {
             // handle the response data e.g.
             NSError *jsonError;
             NSDictionary *json = [NSJSONSerialization
                                   JSONObjectWithData:data
                                   options:0
                                   error:&jsonError];
             NSLog(@"%@",json);
         }
         else {
             NSLog(@"Error: %@", connectionError);
         }
     }];
}
else {
    NSLog(@"Error: %@", clientError);
}

我想要两件事:

  1. 请求网址
  2. 分享参数

1 个答案:

答案 0 :(得分:1)

首先下载FHSTwitterEngine

检查用户是否在Twitter上登录后,如果用户没有登录,则询问登录信息。在推特按钮上。

-(IBAction)tweet:(id)sender
{
    if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"SavedAccessHTTPBody"] length] > 0)
    {
        [self TwitterStatus];
    }
    else
    {
        UIViewController *loginController = [[FHSTwitterEngine sharedEngine]loginControllerWithCompletionHandler:^(BOOL success) {

            if (success)
            {
                NSLog(@"== %@",[[NSUserDefaults standardUserDefaults] objectForKey:@"SavedAccessHTTPBody"]);

                [self TwitterStatus];
            }

            NSLog(success?@"L0L success":@"O noes!!! Loggen faylur!!!");

            // NSLog(@"Sucess = %d",[success integervalue]);
        }];
        [self presentViewController:loginController animated:YES completion:nil];
    }
}

现在,如果用户已经登录,则TwitterStatus会在您的帐户中拨打并发送推文,如下方法。

-(void)TwitterStatus
{
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        @autoreleasepool {
            NSString *tweet = @"This is testing message with image.";

            NSData *data=UIImagePNGRepresentation([UIImage imageNamed:@"scan.png"]);
            NSError *returned = [[FHSTwitterEngine sharedEngine]postTweet:tweet withImageData:data];

            NSLog(@"== %@",returned);

            [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

            NSString *title = nil;
            NSString *message = nil;

            if ([returned isKindOfClass:[NSError class]]) {
                NSError *error = (NSError *)returned;
                title = [NSString stringWithFormat:@"Error %ld",(long)error.code];
                message = error.localizedDescription;
            } else {
                NSLog(@"%@",returned);
                title = @"Tweet Posted";
                message = tweet;
            }

            dispatch_sync(dispatch_get_main_queue(), ^{
                @autoreleasepool {
                    UIAlertView *av = [[UIAlertView alloc]initWithTitle:title message:message delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
                    [av show];
                }
            });
        }
    });

}

您还可以回复,收藏和转发Refer