El manejador de finalización UIActivityViewController llama a la acción si el usuario presiona cancelar

En mi UIActivityViewController, utilizo el manejador de finalización para ejecutar una notificación de "compartición exitosa". Funciona, pero mi único problema es que sigue mostrando la notificación si el usuario presiona cancelar.

Aquí está mi código de controller de finalización,

[controller setCompletionHandler:^(NSString *activityType, BOOL completed) { CWStatusBarNotification *notification = [CWStatusBarNotification new]; [notification displayNotificationWithMessage:@"✓ Successfully Shanetworking Centre!" forDuration:3.0f]; notification.notificationLabelBackgroundColor = [UIColor colorWithRed:38.0f/255.0f green:81.0f/255.0f blue:123.0f/255.0f alpha:1.0f]; notification.notificationLabelTextColor = [UIColor whiteColor]; }]; 

¡Gracias por la ayuda!

Para eso es el argumento completed :

 [controller setCompletionHandler:^(NSString *activityType, BOOL completed) { if (!completed) return; CWStatusBarNotification *notification = [CWStatusBarNotification new]; [notification displayNotificationWithMessage:@"✓ Successfully Shanetworking Centre!" forDuration:3.0f]; notification.notificationLabelBackgroundColor = [UIColor colorWithRed:38.0f/255.0f green:81.0f/255.0f blue:123.0f/255.0f alpha:1.0f]; notification.notificationLabelTextColor = [UIColor whiteColor]; }]; 

Nota: la propiedad completionHandler está en desuso en iOS8, por lo que ya no es posible saber el resultado de una acción de compartir. https://developer.apple.com/documentation/uikit/uiactivityviewcontroller/1622010-completionhandler

Actualización: como dijo adruzh, en iOS8 hay un nuevo Handler de finalización que Apple olvidó mencionar en la documentation:

 [activityController setCompletionWithItemsHandler: ^(NSString *activityType, BOOL completed, NSArray *returnedItems, NSError *activityError) { }]; 

https://developer.apple.com/documentation/uikit/uiactivityviewcontroller/1622022-completionwithitemshandler

Para Swift, esto es lo que funcionó para nosotros:

  ... // Configure UIActivityViewController let activityViewController = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil) activityViewController.excludedActivityTypes = [UIActivityTypeAirDrop, UIActivityTypeAddToReadingList, UIActivityTypeAssignToContact, UIActivityTypePrint, UIActivityTypeCopyToPasteboard] // Show UIActivityViewController presentViewController(activityViewController, animated: true, completion: nil) // Define completion handler activityViewController.completionWithItemsHandler = doneSharingHandler ... func doneSharingHandler(activityType: String!, completed: Bool, returnedItems: [AnyObject]!, error: NSError!) { // Return if cancelled if (!completed) { return } // If here, log which activity occurnetworking println("Shanetworking video activity: \(activityType)") } 

Para los Swifties, así es como codificarías esto en Swift junto con la detección de algunos services compartidos:

 activityViewController.completionHandler = {(activityType, completed:Bool) in if !completed { //cancelled return } //shanetworking successfully //below is how you would detect for different sharing services var activity:String = "other" if activityType == UIActivityTypePostToTwitter { activity = "twitter" } if activityType == UIActivityTypeMail { activity = "mail" } //more code here if you like } 

El parámetro completed será NO el usuario cancela.

 [controller setCompletionHandler:^(NSString *activityType, BOOL completed) { if (completed) { CWStatusBarNotification *notification = [CWStatusBarNotification new]; [notification displayNotificationWithMessage:@"✓ Successfully Shanetworking Centre!" forDuration:3.0f]; notification.notificationLabelBackgroundColor = [UIColor colorWithRed:38.0f/255.0f green:81.0f/255.0f blue:123.0f/255.0f alpha:1.0f]; notification.notificationLabelTextColor = [UIColor whiteColor]; } }]; 

SWIFT 2.0, iOS 8.0> , debe usar el manejador de finalización de la siguiente manera:

 self.presentViewController(activityVC, animated: true, completion: nil) activityVC.completionWithItemsHandler = {(activityType, completed:Bool, returnedItems:[AnyObject]?, error: NSError?) in //do some action } 

Mira mi respuesta aquí: https://stackoverflow.com/a/34581940/1109892

Swift 3

  func completionHandler(activityType: UIActivityType?, shanetworking: Bool, items: [Any]?, error: Error?) { if (shanetworking) { print("Cool user shanetworking some stuff") } else { print("Bad user canceled sharing :(") } } activityController.completionWithItemsHandler = completionHandler