Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift : Share text through Twitter

So basically I am making an event app. Everything has been going smoothly but there's just sharing the event to twitter.

I have searched the internet but all I am getting is using the native app of twitter which I don't want. I want to use the browser to tweet.

I have implemented this method for FB sharing.

Any idea would help me a lot.

let content = FBSDKShareLinkContent()

    content.contentURL=NSURL(string: "http://facebook.com")
    content.imageURL = NSURL(string: "http://facebook.com")

    content.contentTitle = "Shou 3emlin test app "
    content.contentDescription = "testing testing testing"

    let shareDialog = FBSDKShareDialog()
    shareDialog.fromViewController = self
    shareDialog.mode=FBSDKShareDialogMode.Browser
    shareDialog.shareContent = content


    if !shareDialog.canShow() {
        shareDialog.mode=FBSDKShareDialogMode.Native
        shareDialog.shareContent = content
    }

    if shareDialog.canShow() {
        shareDialog.show()
    }
like image 331
user3671619 Avatar asked Nov 28 '22 01:11

user3671619


2 Answers

Put this in an action method of a button or in the method where you want to use the browser to tweet your text Swift 3.0:

let tweetText = "your text"
let tweetUrl = "http://stackoverflow.com/"

let shareString = "https://twitter.com/intent/tweet?text=\(tweetText)&url=\(tweetUrl)"

// encode a space to %20 for example
let escapedShareString = shareString.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed)!

// cast to an url
let url = URL(string: escapedShareString)

// open in safari
UIApplication.shared.openURL(url!)

Result:

enter image description here

like image 115
ronatory Avatar answered Dec 06 '22 16:12

ronatory


Take a look at Fabric.io. This SDK allows you to compose tweets directly from your app.

let composer = TWTRComposer()

composer.setText("just setting up my Fabric")
composer.setImage(UIImage(named: "fabric"))

// Called from a UIViewController
composer.showFromViewController(self) { result in
    if (result == TWTRComposerResult.Cancelled) {
         print("Tweet composition cancelled")
    }
    else {
       print("Sending tweet!")
    }
}
like image 43
Andrey Gershengoren Avatar answered Dec 06 '22 15:12

Andrey Gershengoren