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