xcode Swift : 通过 Twitter 分享文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34809342/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 08:26:24  来源:igfitidea点击:

Swift : Share text through Twitter

xcodeswifttwittersharing

提问by user3671619

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.

我已经搜索了互联网,但我得到的只是使用我不想要的 twitter 的本机应用程序。我想用浏览器发推文。

I have implemented this method for FB sharing.

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

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

回答by ronatory

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:

将其放在按钮的操作方法中,或者放在您要使用浏览器发送文本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

在此处输入图片说明

回答by Andrey Gershengoren

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

看看Fabric.io。此 SDK 允许您直接从您的应用撰写推文。

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!")
    }
}

回答by Prakhar Maheshwari

let tweetText = "hy" 
let tweetUrl = "http://rimmi/" 

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!)