TWTRComposer Swift 3 Fails to post tweet

ios
swift
twitterkit

#1

Hi,

So i’m trying to share to twitter on iOS with the native Twitter SDk (TwitterKit.) I’m using Swift 3.0

I get this issue whenever I try to send just a tweet or even with an image. It always fails. See below my code and error message.

Initialize code:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        
    Twitter.sharedInstance().start(withConsumerKey:K.twitterConsumerKey, consumerSecret:K.twitterConsumerSecret)

    Fabric.with([Crashlytics.self])

    ...

    return true
}

Send tweet code:

let composer = TWTRComposer()
composer.setImage(self.getFinalImage())
composer.show(from: self.navigationController!, completion: { (result:TWTRComposerResult) in
    if (result == .cancelled) {
        DebugLog("Tweet composition cancelled.")
    }
    else {
        DebugLog("Sending tweet...")
    }
})

I get the composer window appear with my image attached, and I can type a message. I press the “Tweet” button but then it doesn’t post the tweet.

In the UI I get a native popup with;
Title: Tweet failed to send.
Message: Tweet not sent
Button: [OK]

And the console says this:

Did encounter error sending Tweet: Error Domain=TWTRNetworkingErrorDomain Code=-1011 "Request failed: unauthorized (401)" UserInfo={NSLocalizedFailureReason=, TWTRNetworkingStatusCode=401, NSErrorFailingURLKey=https://api.twitter.com/1.1/statuses/update.json, NSLocalizedDescription=Request failed: unauthorized (401)}

I’ve also tried this way:

Twitter.sharedInstance().logIn { [unowned self] (session, error) in
	if session != nil {
	    let composer = TWTRComposerViewController(initialText: nil, image: self.getFinalImage(), videoURL: nil)
        composer.delegate = self
        self.navigationController!.present(composer, animated: true, completion: nil)
	} else {
	    let alert = UIAlertController(
	        title: Localised.key("connect_twitter_account"),
	        message: Localised.key("connect_twitter_account_blurb"),
	        preferredStyle: .alert
	    )
	    self.navigationController!.present(alert, animated: false, completion: nil)
	}
}

But I get the same issue.

I even log this:

DebugLog("hasLoggedInUsers: \(Twitter.sharedInstance().sessionStore.hasLoggedInUsers())")
if let userID = Twitter.sharedInstance().sessionStore.session()?.userID {
    DebugLog("userID: \(userID)")
}

Where hasLoggedInUsers is true, and I get a valid userID output in the console. I just dont understand why the tweet isn’t sending…

For reference I’m using Swift 3.0 with cocoapods.

I have:

pod 'Fabric'
pod 'Crashlytics'
pod 'TwitterKit'

In my pod file, which at the time of writing is using the following versions:

Fabric = 1.6.12
Crashlytics = 3.8.5
TwitterKit = 3.0.3
TwitterCore = 3.0.0

I noticed if I use SLComposeViewController to share to twitter it works though, but I’m specifically looking to use the native twitter SDK to share.

Please can somebody help me? I’m pulling my hair out over this issue…

Thanks


#2

Not sure what the rules are here… but bumping incase anyone can assist or is experiencing a similar issue.

My Twitter app is setup correctly and it has read/write permissions. I have the consumer key and secret in the correct places… so really not sure why sharing with the SDK is not working.

Thanks for any assistance you can give.


#3

This also happened to me a few minutes ago, and I had to clear the Twitter session & re-authenticate to fix the issue. Haven’t been able to reproduce it since.

The frustrating issue with this bug is that the docs state that as long as a local Twitter session exists, the composer works. If the error message is correct (authentication failed), why display the composer in the first place? It looks like this could be a bug on Twitter’s end.


#4

Yep it’s a really annoying issue. Can you describe the steps you took to clear the Twitter session please? I’m convinced its a bug Twitters side, but I’m happy to hear if anybody else has had the same issue and how they fixed it.

In the mean time, I’m just using SLComposer


#5

This also happen to me. Sometimes it succeeds to post tweets, sometimes not. I can’t tell which situation occurs errors


#6

It may be caused by the issue below:

If a tweet includes ^ < > \ | ` or \r , the SDK returns 401 error.