1

In my app the user should upload a picture to Twitter (with the Rest API). Now the user can only tweet with text but this works with this code:

let textToTweet = textView.text

    let url = NSURL(string: "https://api.twitter.com/1.1/statuses/update.json")

    let request = SLRequest(forServiceType: SLServiceTypeTwitter, requestMethod: .POST, URL: url, parameters: ["status": textToTweet!])
    request.account = twitterAccount
    request.performRequestWithHandler { (data, response, error) -> Void in

        if response.statusCode != 200 {

            print("not working")

        }
    }

Now I wanted to take a step forward and used this, but it´s not working with this code. :

 let textToTweet = textView.text
    let imageToTweet = image.image

    let url = NSURL(string: "https://api.twitter.com/1.1/statuses/update_with_media.json")

    let request = SLRequest(forServiceType: SLServiceTypeTwitter, requestMethod: .POST, URL: url, parameters: ["status": textToTweet!, "media[]": imageToTweet!])

    request.account = twitterAccount
    request.performRequestWithHandler { (data, response, error) -> Void in

        if response.statusCode != 200 {

            print(error)

        }
    }

Here´s the information that gives Twitter about POST statuses/update_with_media. The console is showing "fatal error: unexpectedly found nil while unwrapping an Optional value" and the app isn´t posting the post or the image. Thank you!

dev
  • 88
  • 1
  • 11
  • 1
    Start by printing the actual error message instead of printing "not working". It will help you understand what is happening. – Eric Aya Nov 24 '15 at 15:09
  • @EricD. Thanks! It say´s "fatal error: unexpectedly found nil while unwrapping an Optional value" – dev Nov 24 '15 at 15:16
  • `imageToTweet` is probably `nil`, and it crashes the app because you're using force-unwrapping with `!` to extract its value. – Eric Aya Nov 24 '15 at 15:19
  • It´s not nil there is an image and it´s not crashing – dev Nov 24 '15 at 15:19
  • `It´s not nil there is an image`: did you verify this? The compiler tells you that you **unexpectedly found nil while unwrapping an Optional value**. If it's not this one, it's the other one (textToTweet). But since you said text works, it must be the image one. // Oh, and your app *does* crash. Otherwise you wouldn't get this error message... – Eric Aya Nov 24 '15 at 15:22
  • I set the image in the viewDidLoad() and it´s showing the right one. And with "print(textToTweet)" it show´s the right text in the console... – dev Nov 24 '15 at 15:25
  • How can I check that there is this image once again? I´m sorry your right the app is crashing...forgot that the app stops while testing – dev Nov 24 '15 at 15:28
  • I´ve renamed the image and it´s still not working – dev Nov 24 '15 at 15:31
  • @EricD. Even if the textView is empty and i click the button it show´s this error and not the "photo or text is nil" message – dev Nov 24 '15 at 16:02
  • 1
    Ok, I've deleted my answer since the error doesn't actually come from these two forced-unwrapped properties. But given the content of the error message, the error you have *is* an Optional which is nil and then forced-unwrapped. // I just had a look at your link, and... look: **POST statuses/update_with_media (deprecated) - This endpoint has been DEPRECATED. Please use [POST statuses/update](https://dev.twitter.com/rest/reference/post/statuses/update) for uploading one or more media entities.** That would explain why you get nil... !! :) – Eric Aya Nov 24 '15 at 16:45
  • @EricD. Thank you! I did not saw it....But with this code it´s still not working:/ `let url = NSURL(string: "https://api.twitter.com/1.1/statuses/update.json") if let txt = textView.text, let img = imageView.image { let request = SLRequest(forServiceType: SLServiceTypeTwitter, requestMethod: .POST, URL: url, parameters: ["status": txt, "media_ids": img]) request.account = twitterAccount ` The app is only posting the text but without any crash or issue... – dev Nov 24 '15 at 19:05
  • I think you have to use POST media/upload endpoint to first upload the media files the response would be a media_id. Use that meda_id (could be multiple if multiple images are uploaded) to compose the tweet in the POST status/update parameter. – Anjan Biswas Oct 28 '17 at 06:08

0 Answers0