user3746428
user3746428

Reputation: 11175

Twitter integration in Swift

I am currently trying to add twitter integration to allow users to share my app on Twitter however I haven't seen any Swift examples yet so I am trying to translate an Objective C version.

This is what I am trying to translate:

- (IBAction)postToTwitter:(id)sender {
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
{
    SLComposeViewController *tweetSheet = [SLComposeViewController
                                           composeViewControllerForServiceType:SLServiceTypeTwitter];
    [tweetSheet setInitialText:@"Great fun to learn iOS programming at appcoda.com!"];
    [self presentViewController:tweetSheet animated:YES completion:nil];
}
}

http://www.appcoda.com/ios-programming-101-integrate-twitter-and-facebook-sharing-in-ios-6/

I am stuck on the first line unfortunately. This is all I currently have:

    @IBAction func twitterShare(sender: AnyObject) {
    if ShareTableViewController
}

I'm not sure if that is correct and if so, where I should go from here. Any help would be much appreciated.

Upvotes: 0

Views: 981

Answers (1)

codester
codester

Reputation: 37189

You can use following code to present the twitter View Controller.You have to configure the twitter account for this in your device.

@IBAction func twitterShare(sender: AnyObject) {
    if  SLComposeViewController.isAvailableForServiceType(SLServiceTypeTwitter){

        var tweetSheet:SLComposeViewController = SLComposeViewController(forServiceType: SLServiceTypeTwitter)
        tweetSheet.setInitialText("Great fun to learn iOS programming at appcoda.com!")
        self.presentViewController(tweetSheet, animated: true, completion: nil)
    }

}

Upvotes: 1

Related Questions