0

Using the code below I'm able to capture an image and share to Instagram to post a new photo, however I'd like to also give the user the ability to share to their Instagram Story. (1) Can I do this using my existing UIActivityViewController and if so, how? (2) If not, how can I do it using URL Schemes? This answer uses Objective-C and I need to figure it out in Swift

@IBAction func shareButtonIsPressed(_ sender: UIBarButtonItem) {


        //        // Screenshot:
        UIGraphicsBeginImageContextWithOptions(self.view.frame.size, true, 0.0)
        self.view.drawHierarchy(in: self.view.frame, afterScreenUpdates: false)
        let img = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()

        //Set the link, message, image to share.
        if let img = img {

            let activityVC = UIActivityViewController(activityItems:  [ImageProvider(image: img), TextProvider()], applicationActivities: nil)
            activityVC.excludedActivityTypes = [UIActivity.ActivityType.airDrop, UIActivity.ActivityType.addToReadingList]
            self.present(activityVC, animated: true, completion: nil)
        }


    }


}

//These classes are a hack from https://stackoverflow.com/questions/43380570/share-image-with-hashtag-via-uiactivityviewcontroller-twitter-facebook-instag
//so that Instagram will show as a sharable action (they won't allow text posted, neither does Facebook)  
class TextProvider: NSObject, UIActivityItemSource {
    func activityViewControllerPlaceholderItem(_ activityViewController: UIActivityViewController) -> Any {
        return NSObject()
    }

    func activityViewController(_ activityViewController: UIActivityViewController, itemForActivityType activityType: UIActivity.ActivityType?) -> Any? {

        let link = URL(string: "https://itunes.apple.com/us/app/hockeytracker/id1297971760?mt=8&at=1010l5Qq&ct=htsharestats")

        if activityType == .postToTwitter || activityType == .message || activityType == .mail { //|| activityType == .postToFacebook {
            return  "Check out my stats from HockeyTracker! \(link!)"
        }
        return nil
    }
}

class ImageProvider: NSObject, UIActivityItemSource {

    var image: UIImage

    init(image: UIImage) {
        self.image = image
    }

    func activityViewControllerPlaceholderItem(_ activityViewController: UIActivityViewController) -> Any {
        return image
    }

    func activityViewController(_ activityViewController: UIActivityViewController, itemForActivityType activityType: UIActivity.ActivityType?) -> Any? {
        return image
    }
}
GarySabo
  • 5,806
  • 5
  • 49
  • 124
  • Here is my answer under the question you've linked, written in Swift: https://stackoverflow.com/a/52898962/3151675 – Tamás Sengel Oct 19 '18 at 19:43
  • @TamásSengel thank you! I don't suppose I'd be able to use this in my existing share sheet (UIActivityViewController)? I'll need to incorporate a separate share button for Instagram Stories? – GarySabo Oct 20 '18 at 05:25

0 Answers0