通过UIActivityViewController共享到Instagram故事吗?

时间:2018-10-19 19:26:35

标签: ios swift instagram uiactivityviewcontroller

使用下面的代码,我可以捕获图像并分享给Instagram以发布新照片,但是我还希望使用户能够分享其Instagram故事。 (1)我可以使用现有的UIActivityViewController进行此操作吗? (2)如果没有,如何使用URL方案? This answer使用了Objective-C,我需要在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
    }
}

0 个答案:

没有答案