1

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

曾经使用过:- TWTweetComposeViewController

请告知我该怎么做?

提前致谢

4

1 回答 1

3
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-01-16T04:41:35.920 回答