【问题标题】:How to Share on Facebook and Twitter App in Swift 3 without SDK如何在没有 SDK 的 Swift 3 中分享 Facebook 和 Twitter 应用程序
【发布时间】:2017-09-10 12:36:38
【问题描述】:

如果通过按钮点击安装在设备中,我需要在 facebook 和 twitter 应用程序上分享一些链接。我可以使用 Whatsapp 分享相同的内容(代码如下)。我想知道我是否可以在 facebook 和 twitter 上做同样的事情应用程序也。

  @IBAction func whatsappbtn(_ sender: UIButton) {

    var str = "This is the string which you want to share to WhatsApp"
    str=str.addingPercentEncoding(withAllowedCharacters: (NSCharacterSet.urlQueryAllowed))!
    let whatsappURL = URL(string: "whatsapp://send?text=\(str)")
    if UIApplication.shared.canOpenURL(whatsappURL!) {
        if #available(iOS 10.0, *) {
            UIApplication.shared.open(whatsappURL!, options: [:], completionHandler: nil)
        } else {
            // Fallback on earlier versions
        }
    } else {

        self.delegate?.alerting(msg: "Please install Whatsapp and try again.")

    }

}

【问题讨论】:

    标签: ios facebook twitter swift3


    【解决方案1】:

    如果你一定不会使用 FB/Twitter SDK。然后您可以尝试使用活动控制器并从您的应用程序共享。在这里,您将获得所有可能的分享选项。

     var activityViewController:UIActivityViewController?
     textField .text = "Some Test"
    
     @IBAction func shareText(sender: UIButton) {
        activityViewController = UIActivityViewController(
          activityItems: [textField.text as NSString],
          applicationActivities: nil)
    
        presentViewController(activityViewController, animated: true, completion: nil)
    }
    

    【讨论】:

      【解决方案2】:

      首先您需要从 Facebook 的开发者网站获取 FBSDK。

      然后你可以这样做:

         func shareToFacebook() {
              let inviteDialog = FBSDKAppInviteDialog()
      
              if inviteDialog.canShow() {
      
                  guard let appLinkURL = URL(string: "YOUR LINK") else { return } 
                  guard let previewImageURL = URL(string: "YOUR LINK IMAGE") else { return }
      
                  let inviteContent = FBSDKAppInviteContent()
                  inviteContent.appLinkURL = appLinkUrl
                  inviteContent.appInvitePreviewImageURL = previewImageURL
      
                  inviteDialog.content = inviteContent
                  inviteDialog.delegate = self
                  inviteDialog.show()
              }
          }
      

      别忘了设置委托方法:

        func appInviteDialog(_ appInviteDialog: FBSDKAppInviteDialog!, didCompleteWithResults results: [AnyHashable : Any]!) {
              print("Did complete sharing.. ")
          }
      
          func appInviteDialog(_ appInviteDialog: FBSDKAppInviteDialog!, didFailWithError error: Error!) {
              print("Error tool place in appInviteDialog \(error)")
          }
      

      以上适用于 Swift 3+ 请让我知道这对你有没有用。干杯

      【讨论】:

        猜你喜欢
        • 2013-01-03
        • 1970-01-01
        • 2017-09-30
        • 2015-07-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多