The task is pretty much old and solved but I can't solve even after reading all other answers on SO. I want to generate an image and open the Instagram with my image loaded into the filter screen. No "open in" menu. I want to go straight to the Instagram app.
Here's the code:
@objc func share() {
let fileManager = FileManager.default
do {
let documentDirectory =
try fileManager.url(
for: .documentDirectory, in: .userDomainMask,
appropriateFor: nil, create: true)
let fileURL = documentDirectory.appendingPathComponent("challenge.igo")
if let imageData = theImage.jpegData(compressionQuality: 0.5) {
try imageData.write(to: fileURL)
DLog(message: "file written to \(fileURL)")
let dic = UIDocumentInteractionController(url: fileURL)
dic.delegate = self
dic.uti = "com.instagram.exclusivegram"
let res = dic.presentPreview(animated: false)
DLog(message: "presentPreview result \(res)")
}
} catch {
DLog(message: "\(error)")
}
}
I also implement the protocol
UIDocumentInteractionControllerDelegate
:extension ICShareController: UIDocumentInteractionControllerDelegate {
func documentInteractionControllerViewControllerForPreview() -> UIViewController {
return self
}
}
But alas the
presentPreview
function always returns false
and no preview is opened.What can I do wrong?