如何在Swift 3中使用Facebook和Twitter应用程序分享而无需SDK

4

我需要在设备上点击按钮时,在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.")

    }

}
2个回答

7
如果您不能使用FB/Twitter SDK,则可以尝试使用activity controller从您的应用程序分享。在这里,您将获得每个可能的共享选项。
 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)
}

enter image description here


-3

首先,您需要从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+,如果您有任何问题,请告诉我。谢谢。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接