UIActivityViewController UIActivityViewControllerCompletionWithItemsHandler

  • List item

Using Swift for an app that runs in iOS 8, I need to write a completion handler for the UIActivityViewController to capture the results of which “share” method a user selected.

This is a snippet of the code I have so far. My question is how to I set the avc.completionWithItemsHandler? I’m sure it’s simple, but I don’t see it.

  • Can I tweet an animated GIF from a UIActivityViewController and the native UIActivityTypePostToTwitter yet?
  • UINavigationBar Appearance on Modal Not Setting
  • Share App application shortcut
  • UIActivityViewController Hide More from Actions
  • UIActivityViewController doesn't show FB and Twitter on iOS 7
  • No Title or Cancel Button for MFMessageComposeViewController
  • var activityItems = NSMutableArray()
    activityItems.addObject("Email or text for 'share' goes here")
    
    var avc = UIActivityViewController(activityItems: activityItems, applicationActivities: nil)
    avc.setValue("Subject for Email", forKey: "Subject")
    
    avc.completionWithItemsHandler = //Here is where I dont know what to do.
    
    self.navigationController?.presentViewController(avc, animated: true, completion: nil)
    

    3 Solutions Collect From Internet About “UIActivityViewController UIActivityViewControllerCompletionWithItemsHandler”

    The completionWithItemsHandler typealias:

    typealias UIActivityViewControllerCompletionWithItemsHandler = (String?, Bool, [AnyObject]?, NSError?) -> Void
    

    Note: the previous code block is not to be used in your project, it just shows the type of closure needed (docs).

    So those are the parameters that are passed into the completion handler for you to do with as you will, so the completion handler would look like this:

    avc.completionWithItemsHandler = { activity, success, items, error in 
    
    }
    

    NOTE: Because I didn’t read the “SWIFT” part of the question, I answered the question in Obj-C. My bad, To the OP: I apologize

    Here is a more complete answer that actually can be compiled. I used: dispatch_async in order to do an alert so you can see what “activityType” ended up being.

    avc.completionWithItemsHandler = ^(NSString *activityType, BOOL completed, NSArray *returnedItems, NSError *activityError) {
    dispatch_async(dispatch_get_main_queue(), ^{
                 UIAlertViewQuick(@"Activity Status", activityType, @"OK");
             });
    if (completed)
    {
         NSLog(@"The Activity: %@ was completed", activityType);
    }
    else
    {
         NSLog(@"The Activity: %@ was NOT completed", activityType);
    }
    
    };
    

    This was answered quite a while ago, but has a mix of missing and non-swift info so here’s my version in the hope that it will help someone needing a more complete example of the completion handler:

        avc.completionWithItemsHandler = {[weak self](activityTypeChosen, completed:Bool, returnedItems:[AnyObject]?, error:NSError?) -> Void in
    
            // ReturnedItems is an array of modified NSExtensionItem, or nil of nothing modified
            // if (activityType == nil) User dismissed the view controller without making a selection.
    
            // Dismiss the view controller we presented
            // (assume a reference to it was stored in self.activityVC)
            self?.activityVC?.dismissViewControllerAnimated(true, completion: {
                if activityTypeChosen == nil {
                    NSLog("User canceled without choosing anything")
                }
                else if completed {
                    NSLog(")User chose an activity and iOS sent it to that other app/service/whatever OK")
                }
                else {
                    NSLog("There was an error: \(error)")
                }
            })
        }
    

    Note the line where it dismisses the view controller. The docs for UIActivityViewController say very explicitly that your app is responsible for both presenting the VC and dismissing it.