Swift:通过Twitter分享文字

时间:2016-01-15 10:45:47

标签: xcode swift twitter sharing

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

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

我已经为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()
    }

4 个答案:

答案 0 :(得分:16)

将此放在按钮的操作方法中或您要使用浏览器发布文字的方法中 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!)

<强>结果

enter image description here

答案 1 :(得分:4)

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

<script type="text/javascript">
$('#continent').on('change', function() {
  var continent= $('#continent').val();

  // update sport list
  $.ajax('./json.php', {
    data: {
      "continent": continent
    }, success: function(data) {
      // clear and update sports SELECT
      $('#country').html('');
      for (var i in data) {
        $('#country').append($('<option>').val(data[i].val_id).text(data[i].country_name)
      }
    }
  });
});

$('#country').on('change', function() {
  var continent= $('#continent').val();
  var country= $('#country').val();

  // update sport list
  $.ajax('./json.php', {
    data: {
      "continent": continent, // most likely not relevant, country itself should suffice
      "country": country  
    }, success: function(data) {
      // clear and update sports SELECT
      $('#sport').html('');
      for (var i in data) {
        $('#sport').append($('<option>').val(data[i].val_id).text(data[i].sport_name)
      }
    }
  });
});
</script>
<body>
<select id="continent">
  <option value="Europe">Europe</option>
</select>

<select id="country">

</select>

<select id="sport">

</select>
</body>

答案 2 :(得分:0)

让tweetText =“ hy”

让tweetUrl =“ http://rimmi/

let shareString =“ https://twitter.com/intent/tweet?text=(tweetText)&url=(tweetUrl)”

//例如将空格编码为%20

让escapedShareString = shareString.addingPercentEncoding(withAllowedCharacters:CharacterSet.urlQueryAllowed)!

//强制转换为网址

让url = URL(字符串:escapedShareString)

//在野生动物园中打开

UIApplication.shared.openURL(url!)

答案 3 :(得分:0)

@ronatory 的解决方案很有魅力。如果用户设备上已经安装了 Twitter 应用程序,它还会打开该应用程序。

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