Search code examples
iosfacebooktwitterswift3

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


I need to share some link on facebook and twitter app if installed in device on a button tap.I can share the same using Whatsapp (code is below).I want to know if i can do the same in facebook and twitter app also.

  @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.")

    }

}

Solution

  • If you are bound not to use FB / Twitter SDK . then you can try using activity controller and share from your app . Here you will get every possible share option .

     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