【问题标题】:upload pic without opening Tweetsheet in twitter for ios 5 & ios 6在 twitter 中为 ios 5 和 ios 6 打开 Tweetsheet 上传图片
【发布时间】:2012-12-26 01:29:09
【问题描述】:

我想在 ios5 和 ios6 不打开 Tweetsheet 的情况下上传图片。 可能吗? 我曾尝试使用 TweetSheet 上传图片,我可以上传照片,但不打开 tweetsheet 就无法上传照片。

曾经使用过:- TWTweetComposeViewController

请告诉我怎么做?

提前致谢

【问题讨论】:

  • @H2CO3..已经编辑了这个问题。希望你知道我尝试了什么..如果你有任何关于 TWTweetComposeViewController 的知识,你能帮助我吗?

标签: iphone ios ios5 twitter ios6


【解决方案1】:
if (appDelegate.PHONE_OS >= 5)
{        
    NSString *strTwitterMessage;
    strTwitterMessage = [NSString stringWithFormat:@"test twitter post : Photo link URL "];        

    ACAccountStore *accountStore = [[ACAccountStore alloc] init];
    ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
    [accountStore requestAccessToAccountsWithType:accountType withCompletionHandler:^(BOOL granted, NSError *error) {
        if(granted) {
            // Get the list of Twitter accounts.
            NSArray *accountsArray = [accountStore accountsWithAccountType:accountType];

            NSLog(@"Count : %d",[accountsArray count]);                
            ACAccount *twitterAccount;                
            int flagTwitter;
            flagTwitter = 0;
            for(int i = 0;i<[accountsArray count];i++)
            {
                if([[[accountsArray objectAtIndex:i]username] isEqualToString:appDelegate.strTwitterEmailSelected])
                {                        
                    flagTwitter = 1;                        
                    twitterAccount = [accountsArray objectAtIndex:i];                        
                }

            }                
            if(flagTwitter == 1)
            {
                appDelegate.strTwitterEmail = appDelegate.strTwitterEmailSelected;

            }
            else
            {
                appDelegate.strTwitterEmail = [[accountsArray objectAtIndex:0]username];
                twitterAccount = [accountsArray objectAtIndex:0];
            }

            //ACAccount *twitterAccount = [accountsArray objectAtIndex:0];
            //appDelegate.strTwitterEmail = [[accountsArray objectAtIndex:0]username];

             UIImage *image =[UIImage imageNamed:@"image.png"];


             NSURL *url =
             [NSURL URLWithString:
             @"https://upload.twitter.com/1/statuses/update_with_media.json"];

             //  Create a POST request for the target endpoint
             TWRequest *request =
             [[TWRequest alloc] initWithURL:url
             parameters:nil
             requestMethod:TWRequestMethodPOST];

             [request setAccount:twitterAccount];

             NSData *imageData = UIImagePNGRepresentation(image);

             [request addMultiPartData:imageData
             withName:@"media[]"
             type:@"multipart/form-data"];

             NSString *status = strTwitterMessage;

             [request addMultiPartData:[status dataUsingEncoding:NSUTF8StringEncoding]
             withName:@"status"
             type:@"multipart/form-data"];

             [request performRequestWithHandler:
             ^(NSData *responseData1, NSHTTPURLResponse *urlResponse, NSError *error) {
             NSDictionary *dict =
             (NSDictionary *)[NSJSONSerialization
             JSONObjectWithData:responseData1 options:0 error:nil];

             // Log the result
             NSLog(@"%@", dict);

             NSString *output = [NSString stringWithFormat:@"HTTP response status: %i", [urlResponse statusCode]];

             NSLog(@"status : %@", output);

             dispatch_async(dispatch_get_main_queue(), ^{
             // perform an action that updates the UI...

             UIAlertView *customAlertView = [[UIAlertView alloc]initWithTitle:@"Social Media"
             message:@"successful share"
             delegate:nil
             cancelButtonTitle:nil
             otherButtonTitles:@"OK",nil];
             [customAlertView show];
             [customAlertView release];

             });
             }];

        }

        else
        {
            // Alert Please login through seeting


            dispatch_async(dispatch_get_main_queue(), ^{
                // perform an action that updates the UI...



                UIAlertView *customAlertView = [[UIAlertView alloc]initWithTitle:@"Social Media"
                                                                         message:@"Please enable twitter through iPhone Settings"
                                                                        delegate:nil
                                                               cancelButtonTitle:nil
                                                               otherButtonTitles:@"OK",nil];
                [customAlertView show];
                [customAlertView release];

            });


        }

    }];
}

【讨论】:

    猜你喜欢
    • 2013-12-20
    • 1970-01-01
    • 2015-08-27
    • 2012-10-03
    • 1970-01-01
    • 2013-04-12
    • 2012-12-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多