UIActivityViewController completion handler returns success when tweet has failed UIActivityViewController completion handler returns success when tweet has failed ios ios

UIActivityViewController completion handler returns success when tweet has failed


Use completion handler like this For SWIFT 3 AND 4, iOS 10 AND 11 :

activityVC.completionWithItemsHandler = {(activityType: UIActivityType?, completed: Bool, returnedItems: [Any]?, error: Error?) in    if !completed {        // User canceled         return    }    // User completed activity}self.present(activityVC, animated: true, completion: nil)


SWIFT 2.0 iOS 8.0 >, you should use completion handler like this:

self.presentViewController(activityVC, animated: true, completion: nil)activityVC.completionWithItemsHandler = {(activityType, completed:Bool, returnedItems:[AnyObject]?, error: NSError?) in     // Return if cancelled     if (!completed) {         return     }     //activity complete     //some code here}


I don't think you can affect the feedback flow of the UIActivityViewController as it is high-level, easy-to-use component that is not tailored for fine-grained customization.

What you can do, though, is to save the state that user has tweeted this exact message after the first tweet and then disable Twitter from UIActivityController using excludedActivityTypes and UIActivityTypePostToTwitter. So, instead of showing an error for a duplicate tweet, you prevent the action sequence even from happening.