繁体   English   中英

如何在没有SDK的Swift 3中分享Facebook和Twitter应用程序

[英]How to Share on Facebook and Twitter App in Swift 3 without SDK

我需要在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.")

    }

}

如果您有义务不使用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)
}

在此输入图像描述

首先,您需要从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+让我知道它是否适合您。 干杯

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM