Sharing on social media

Sharing on facebook

import Social

//create a sharing function for Facebook, in button clicked

 if let vc = SLComposeViewController(forServiceType: SLServiceTypeFacebook)
        {
         shareOnFb(vc: vc)
         
        }

//add function below

func shareOnFb(vc: SLComposeViewController)
    {
        vc.setInitialText("Posted this from my swift app")
        vc.add(thisImage)
        let theurl = URL(string: "http://swiftcodingreferencebook.blogspot.sg")
        vc.add(theurl)
        present(vc, animated: true, completion: nil)
     
    }

Sharing on Instagram
in info.plist
Create a key of type array:
LSApplicationQueriesSchemes
Control+click and add a row
Item 0 value: instagram
add to class ViewController: UIViewController, UIDocumentInteractionControllerDelegate

Add class variable:
    var documentController: UIDocumentInteractionController!

Add the following code the the button action:
DispatchQueue.main.async {
            
            //Share To Instagrma:
            
            let instagramURL = URL(string: "instagram://app")
            
            if UIApplication.shared.canOpenURL(instagramURL!) {
                
                let imageData = UIImageJPEGRepresentation(self.thisImage, 100)
                
                let writePath = (NSTemporaryDirectory() as NSString).appendingPathComponent("instagram.igo")
                
                do {
                    try imageData?.write(to: URL(fileURLWithPath: writePath), options: .atomic)
                    
                } catch {
                    
                    print(error)
                }
                
                let fileURL = URL(fileURLWithPath: writePath)
                
                self.documentController = UIDocumentInteractionController(url: fileURL)
                
                self.documentController.delegate = self
                
                self.documentController.uti = "com.instagram.exlusivegram"
                
                if UIDevice.current.userInterfaceIdiom == .phone {
                    
                    self.documentController.presentOpenInMenu(from: self.view.bounds, in: self.view, animated: true)
                    
                }
                
                
            } else {
                
                print(" Instagram is not installed ")
            }
        }
     
Whatsapp

in info.plist
Create a key of type array:
LSApplicationQueriesSchemes
Control+click and add a row
Item 0 value: whatsapp
In the button function:
ar 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!) {
            UIApplication.shared.open(whatsappURL!, options: [:], completionHandler: nil)
        } else {
 print("No whatsapp")        }




Comments

Popular posts from this blog

Setting up a playground

Go to another page