xcodeswifttwittersharing

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()
    }

Solution

  • 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