Created
January 11, 2018 18:54
-
-
Save almassapargali/109fe4145e01f9df272307eb9447b2b4 to your computer and use it in GitHub Desktop.
UIActivity for Instagram image sharing
This file contains 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) | |
} | |
} |
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)
Sharing All images, text and any file with swift Click Here
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
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)