2

I want to share image only on whatsapp, but the code I use, shows other online share platform i.e. messenger & email.

Code I used:

    func share(shareText:String?,shareImage:UIImage?){

    var objectsToShare = [AnyObject]()

    if let shareTextObj = shareText{
        objectsToShare.append(shareTextObj as AnyObject)
    }

    if let shareImageObj = shareImage{
        objectsToShare.append(shareImageObj)
    }

    if shareText != nil || shareImage != nil{
        let activityViewController = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)
        activityViewController.popoverPresentationController?.sourceView = self.view

        present(activityViewController, animated: true, completion: nil)
    }else{
        print("There is nothing to share")
    }
}

and to share

    let imageToShare = UIImage(named: "05")
    share(shareText: "", shareImage: imageToShare)
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Lalit Pratap
  • 119
  • 1
  • 11

2 Answers2

2

You can not hide all the option that are showing in the UIActivityViewController because it depends on the content you are sharing and also installed app in your device, but you can hide most of it like all default options like this way:

let activityController = UIActivityViewController(activityItems: sharingItems, applicationActivities: nil)

    activityController.excludedActivityTypes = [
        UIActivityType.assignToContact,
        UIActivityType.print,
        UIActivityType.addToReadingList,
        UIActivityType.saveToCameraRoll,
        UIActivityType.openInIBooks,
        UIActivityType(rawValue: "com.apple.reminders.RemindersEditorExtension"), 
        UIActivityType(rawValue: "com.apple.mobilenotes.SharingExtension"),
    ]

    present(activityController, animated: true, completion: nil)

OR

If you want to share only in whatapp so here is the way to do it. please refer below url:

Share image/text through WhatsApp in an iOS app

Patrick R
  • 6,621
  • 1
  • 24
  • 27
  • Mr. Patrick thanks for reply , but direct whatsapp link is used to share only text not images . any other way to do this ? – Lalit Pratap Jan 06 '18 at 07:26
  • See this answer https://stackoverflow.com/questions/8354417/share-image-text-through-whatsapp-in-an-ios-app#answer-40433392 – Patrick R Jan 08 '18 at 06:56
2

By adding some extra line to Mr. Patricks code I reduce the extra online sharing option that satified my need , If any one facing the same problem for those I want share that. I remove all other sharing option except WhatsApp using below code and it just shows WhatsApp and more option to share on screen . Thanks

 func shareImg(){

    let activityViewController:UIActivityViewController = UIActivityViewController(activityItems: shareItems, applicationActivities: nil)
    activityViewController.excludedActivityTypes = [
        UIActivityType.postToTwitter,
        UIActivityType.postToWeibo,
        UIActivityType.message,
        UIActivityType.mail,
        UIActivityType.print,
        UIActivityType.copyToPasteboard,
        UIActivityType.assignToContact,
        UIActivityType.saveToCameraRoll,
        UIActivityType.addToReadingList,
        UIActivityType.postToFlickr,
        UIActivityType.postToVimeo,
        UIActivityType.postToTencentWeibo,
        UIActivityType.airDrop
    ]
    self.present(activityViewController, animated: true, completion: nil)

    activityViewController.completionWithItemsHandler = {(activityType: UIActivityType?, completed: Bool, returnedItems: [Any]?, error: Error?) in
        if !completed {

            print("User canceled")
            self.shareItems.removeAll()
            return
        }

        print("share successfully")

      }
  }
Lalit Pratap
  • 119
  • 1
  • 11