How to share a post from my app to twitter iOS

6.4k views Asked by At

I basically want the user to share something from my app to twitter. I was exploring the different methods possible and found SLComposeViewController but it is deprecated. Support for twitterKit has also been officially stopped by twitter. One possible way to do so seems to be like given in the answers in this post. Is there some other better way to do so? I would prefer if the user stays in the app itself (something similar to sharesheet for fb). Thanks!

1

There are 1 answers

5
Ratul Sharker On

You can use twitter-kit-ios.

For posting a tweet sample code from documentation would be following

Objective-c

// Objective-C
TWTRComposer *composer = [[TWTRComposer alloc] init];

[composer setText:@"just setting up my Twitter Kit"];
[composer setImage:[UIImage imageNamed:@"twitterkit"]];

// Called from a UIViewController
[composer showFromViewController:self completion:^(TWTRComposerResult result) {
    if (result == TWTRComposerResultCancelled) {
        NSLog(@"Tweet composition cancelled");
    }
    else {
        NSLog(@"Sending Tweet!");
    }
}];

Swift

// Swift
let composer = TWTRComposer()
    
composer.setText("just setting up my Twitter Kit")
composer.setImage(UIImage(named: "twitterkit"))
    
// Called from a UIViewController
composer.show(from: self.navigationController!) { (result in
    if (result == .done) {
        print("Successfully composed Tweet")
    } else {
        print("Cancelled composing")
    }
}

Straight from the discontinuing blog post you can use, new api endpoints.