Swift : 通过 Twitter 分享文字
Swift : Share text through 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()
}
看看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!")
}
}
将其放在按钮的操作方法中或要使用浏览器发送文本的方法中 Swift 3.0:
let tweetText = "your text"
let tweetUrl = "http://whosebug.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!)
结果:
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!)
@ronatory 的解决方案非常有效。如果用户设备上已经安装了 Twitter 应用程序,它还会打开它。
对于 swift 5+,请使用 UIApplication.shared.open(url!)
而不是 UIApplication.shared.openURL(url!)
,因为它已被弃用。
所以基本上我正在制作一个活动应用程序。一切进展顺利,但只是将活动分享到推特。
我已经在互联网上进行了搜索,但我得到的只是使用我不想要的 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()
}
看看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!")
}
}
将其放在按钮的操作方法中或要使用浏览器发送文本的方法中 Swift 3.0:
let tweetText = "your text"
let tweetUrl = "http://whosebug.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!)
结果:
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!)
@ronatory 的解决方案非常有效。如果用户设备上已经安装了 Twitter 应用程序,它还会打开它。
对于 swift 5+,请使用 UIApplication.shared.open(url!)
而不是 UIApplication.shared.openURL(url!)
,因为它已被弃用。