Posting Image to Twitter from Xamarin iOS using API

I have the following code where I am trying to tweet an image using the Twitter API from Xamarin.iOS.

ACAccountType twitterType = this.accountStore.FindAccountType(ACAccountType.Twitter);

        var hud = new MTMBProgressHUD (View) {
            LabelText = "Tweeting...",
            RemoveFromSuperViewOnHide = true
        };
        View.AddSubview (hud);

        hud.Show (animated: true);

        this.accountStore.RequestAccess(twitterType,null,(granted, error) => {


            if(granted){

//Setting the Twitter account.

                var accountType =  accountStore.FindAccountType(ACAccountType.Twitter);
                var twitterAccount = accountStore.FindAccounts(accountType);

request.Account = twitterAccount[0];

                NSDictionary parameter = new NSDictionary();

                var request = SLRequest.Create(SLServiceKind.Twitter,SLRequestMethod.Post,
                    new NSUrl("https://api.twitter.com//1.1/statuses/update_with_media.json"),parameter);

                NSData imageData = selectedImage.AsJPEG(0.1f);

                request.AddMultipartData(imageData,"media[]","image/jpeg","image.jpg");

DispatchQueue.GetGlobalQueue(DispatchQueuePriority.Low).DispatchAsync(()=>{

                    request.PerformRequest(((NSData responseData, NSHttpUrlResponse urlResponse, NSError error1) => {

                        if(urlResponse.StatusCode>=200 && urlResponse.StatusCode<=300){
                            NSDictionary postResponseData = (NSDictionary)NSJsonSerialization.Deserialize(responseData,NSJsonReadingOptions.MutableContainers,null);

                            NSString postID = (NSString)postResponseData.ValueForKey(new NSString("id_str"));
                            hud.Hide(animated:true);

                            DispatchQueue.MainQueue.DispatchAsync(()=>{
                                UIAlertView alert = new UIAlertView("Tweet", postID,null,"OK",null);
                                alert.Show();
                            });
                        }
                        else{
                            hud.Hide(animated:true);

                            DispatchQueue.MainQueue.DispatchAsync(()=>{
                                UIAlertView alert = new UIAlertView("Tweet", "Error in tweeting",null,"OK",null);
                                alert.Show();

});

But I am not getting any response data as it says its null. I do have a doubt on whether I am accessing the twitter account from the ACAccountStore properly. Please help me out!

Posts

Sign In or Register to comment.