Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to perform action after UIActivityViewController called and then closed

I can't understand how to perform actions after UIActivityViewController is called. For example when I save image with the following code:

let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
let activity = UIActivityViewController(activityItems: [image], applicationActivities: nil)
presentViewController(activity, animated: true, completion: nil)

User receives new window with ability to save image somewhere. After the image was successfully saved I want to perform some action (for example jump to root view controller). But I can't figure out how to track that this UIActivityViewController was closed. If I write code after this block, nothing happens, as I understand, because this code is implemented in the activity VC, not in original VC.

I thought that viewWillDisappear will help me, but it tracks the original VC, from where I called activity VC and I can't figure out how can I track activity VC. At the same time, even if I had ability to track this event, there is a question still remains: how can I differ successful save from the cancelation?

like image 263
Denis Blinov Avatar asked Oct 12 '15 21:10

Denis Blinov


1 Answers

You can use a completionHadler, in this case:

activity.completionWithItemsHandler = { activity, success, items, error in
    print("activity: \(activity), success: \(success), items: \(items), error: \(error)")
}
like image 160
PabloLerma Avatar answered Nov 03 '22 14:11

PabloLerma