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

OME*_*RMA 4 twitter facebook ios swift3

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

    }

}
Run Code Online (Sandbox Code Playgroud)

小智 6

如果您有义务不使用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)
}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述