-
-
Save AitoApps/483477ca2273bd93eba746c3ea2d4f95 to your computer and use it in GitHub Desktop.
UIActivity for Instagram image sharing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment