Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIActivityViewController.completionWithItemsHandler is not called on iOS 10.0 when I select "Print" menu and cancel it

I created a simple application which uses UIActivityViewController as below.

    let text = "Test Text"
    let printData = UISimpleTextPrintFormatter(text: text)
    let vc = UIActivityViewController(activityItems: [text, printData], applicationActivities: nil)
    vc.completionWithItemsHandler = { (type,completed,items,error) in
        print("completed. type=\(type) completed=\(completed) items=\(items) error=\(error)")
    }

    vc.modalPresentationStyle = .popover
    vc.popoverPresentationController?.sourceView = self.openActivityButton
    vc.popoverPresentationController?.sourceRect = self.openActivityButton.bounds
    vc.popoverPresentationController?.permittedArrowDirections = .up
    vc.popoverPresentationController?.delegate = self

    self.present(vc, animated: true) { () in
    }

and I run this application on iOS 10 (Xcode 8.0 beta 6).

  • When I close activity dialog, the completionWithItemsHandler is called.
  • When I select "Copy" activity, the completionWithItemsHandler is called.
  • When I select "Mail" activity and cancel it, the completionWithItemsHandler is called.
  • But, when I select "Print" activity and cancel it, the completionWithItemsHandler is not called.

This strange behavior occurred on iOS 10 but not occurred on iOS9 (the handler was called on iOS9)

Is this iOS 10's bug? If so, are there any workarounds to detect the UIActivityController is dismissed?

I pushed this sample app on https://github.com/kunichiko/ios10-activity-bug

like image 325
kunichiko Avatar asked Aug 19 '16 06:08

kunichiko


1 Answers

I had a similar problem. In my case, I noticed that the completion handler was not called because the UIActivityController was already dismissed and deallocated. What I did was just add a property to hold a strong reference and set it to nil later. Then the completion handler was called properly.

like image 176
juf Avatar answered Nov 16 '22 10:11

juf