Skip to content

Instantly share code, notes, and snippets.

@almassapargali
Created January 11, 2018 18:54
Show Gist options
  • Save almassapargali/109fe4145e01f9df272307eb9447b2b4 to your computer and use it in GitHub Desktop.
Save almassapargali/109fe4145e01f9df272307eb9447b2b4 to your computer and use it in GitHub Desktop.
UIActivity for Instagram image sharing
import Foundation
import UIKit
private let InstagramAppURL = URL(string: "instagram://app")!
class InstagramActivity: UIActivity, UIDocumentInteractionControllerDelegate {
override class var activityCategory: UIActivityCategory { return .share }
override var activityType: UIActivityType? { return UIActivityType("postToInstagram") }
override var activityTitle: String? { return "Instagram" }
override var activityImage: UIImage? { return #imageLiteral(resourceName: "instagram_activity") }
var isPerformed = false
var imageUrl: URL!
var text: String?
var document: UIDocumentInteractionController!
weak var viewController: UIViewController?
let presentFrom: PresentFrom
init(with presentFrom: PresentFrom, in vc: UIViewController) {
viewController = vc
self.presentFrom = presentFrom
}
override func canPerform(withActivityItems activityItems: [Any]) -> Bool {
guard UIApplication.shared.canOpenURL(InstagramAppURL),
let image = activityItems.first(where: { $0 is UIImage }) as? UIImage,
let docsUrl = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first,
let data = UIImageJPEGRepresentation(image, 0.7) else { return false }
imageUrl = docsUrl.appendingPathComponent("to-share.ig")
do {
try data.write(to: imageUrl)
return true
} catch {
print("Error writing image", error.localizedDescription)
return false
}
}
override func prepare(withActivityItems activityItems: [Any]) {
text = activityItems.first(where: { $0 is String }) as? String
}
override func perform() {
document = UIDocumentInteractionController(url: imageUrl)
document.uti = "com.instagram.photo"
document.delegate = self
guard text != nil else {
presentDoc(doc: document)
return
}
UIPasteboard.general.string = text
let alert = UIAlertController(title: nil, message: "Caption has been copied into clipboard. You can paste it into Instagram", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default) { _ in
self.presentDoc(doc: self.document)
})
viewController?.present(alert, animated: true, completion: nil)
}
private func presentDoc(doc: UIDocumentInteractionController) {
switch presentFrom {
case .barButtonItem(let barButtonItem):
doc.presentOpenInMenu(from: barButtonItem, animated: true)
case let .view(view, rect):
doc.presentOpenInMenu(from: rect, in: view, animated: true)
}
}
func documentInteractionController(_ controller: UIDocumentInteractionController, willBeginSendingToApplication application: String?) {
isPerformed = true
}
func documentInteractionController(_ controller: UIDocumentInteractionController, didEndSendingToApplication application: String?) {
activityDidFinish(isPerformed)
}
enum PresentFrom {
case barButtonItem(UIBarButtonItem)
case view(UIView, CGRect)
}
}
@almassapargali
Copy link
Author

instagram_icon

@monkeywithacupcake
Copy link

Hi, I can't quite get this to work. I think I just don't understand. I was setting up the functions within my view controller, but i like your implementation that separates the actions. If I init from my view controller, I am not able to perform the actions. Can you post how you use this, @almassapargali ?

it is definitely not like this (this is one of many iterations that I have made)

    @objc func shareIGTapped(_ sender: UIButton) {
        let iga = InstagramActivity(with: .view(containerView, .init(origin: containerView.center, size: .init(width: 100, height: 100))), in: self)
        iga.perform()
    }

@ArtOfShadow
Copy link

ArtOfShadow commented Jul 13, 2018

try it:

let iga = InstagramActivity(with: .view(containerView, .init(origin: containerView.center, size: .init(width: 100, height: 100))), in: self)
let activityViewController = UIActivityViewController(activityItems: [ ...whateverUwant2share...], applicationActivities: [iga])
self.present(activityViewController, animated: true, completion: nil)

@User2004
Copy link

Sharing All images, text and any file with swift Click Here

@burkarddev
Copy link

Please share image again. Above one is invalid

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment