Swift:通过Twitter分享文字

use*_*619 2 twitter xcode sharing swift

所以基本上我正在制作一个活动应用.一切都进展顺利,但只是将活动分享到Twitter.

我已经搜索了互联网,但我得到的只是使用我不想要的Twitter原生应用程序.我想用浏览器发推文.

我已经为FB共享实现了这种方法.

任何想法都会对我有所帮助.

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()
    }
Run Code Online (Sandbox Code Playgroud)

ron*_*ory 18

把它放在一个按钮的动作方法中,或者在你想使用浏览器发布你的文本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!)
Run Code Online (Sandbox Code Playgroud)

结果:

在此输入图像描述


小智 5

@ronatory 的解决方案非常有效。如果用户设备上已安装 Twitter 应用程序,它还会打开该应用程序。

对于 swift 5+ 使用UIApplication.shared.open(url!)而不是UIApplication.shared.openURL(url!)因为它已被弃用。