xcode Swift:通过Twitter分享文本

hl0ma9xz  于 2023-05-19  发布在  Swift
关注(0)|答案(5)|浏览(264)

所以我基本上是在做一个事件应用程序。一切都进行得很顺利,但只是在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()
    }
e5nszbig

e5nszbig1#

将其放在按钮的action方法中,或者你想使用浏览器来tweet你的文本的方法中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!)

结果

7xzttuei

7xzttuei2#

@ronatory的解决方案很有魅力。它还打开一个Twitter应用程序,如果它已经安装在用户的设备上。
对于swift 5+,使用UIApplication.shared.open(url!)而不是UIApplication.shared.openURL(url!),因为它已被弃用。

dgenwo3n

dgenwo3n3#

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

wvmv3b1j4#

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

pkwftd7m5#

在SwiftUI Swift5中使用

Button {
    let tweetText = "Text for share"
    let shareString = "https://twitter.com/intent/tweet?text=\(tweetText)"
    let escapedShareString = shareString.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed)!
    let url = URL(string: escapedShareString)
    UIApplication.shared.open(url!)
} label: {
    Text("Share on Twitter")
}

相关问题