As part of the Udacity's iOS Development with Swift Nanodegree program, I've been working UIActivityView's recently. The UIActivityView is typically used to share things via email, SMS, as well as iCloud. The built is functionality is great and covers my needs for this project. The issue that I've come across is that I want to wire into the completion event and do some things, like dismiss the view controller, whenever the user has successfully shared something. So have digging a bit I came across the class reference for UIActivityViewController on Apple's website. Beginning in iOS the CompletionHandler property is deprecated and it is recommended that you use the CompletionWithItemsHandler so that is what I'll be using. The CompletionWithItemsHandler property is a typealias that will pass 4 parameters, 3 of which are optional, to handler you create. The four parameters are (in order): activity of type String?, completed of type Bool, returnedItemd of type [AnyObject]?, and error of type NSError?, for a more complete definition see the Apple reference page that I linked earlier. Only bool is required and that is the property that I care about for this exercise. The completed property does pretty much what you'd expect it to do. It is true when the action was successful and false if the action was not successful or if the user canceled the view without taking an action. I'm using a closure to wire up the completion event. Here is my code to create the view controller and wire up the completion event. let activityView = UIActivityViewController(activityItems: [meme.memeImage!], applicationActivities: nil); And that's about it. Nothing too fancy just a simple way to wire up to the completion event handler for the UIActivityViewController and do something before dismissing the view controller. If you wanted to log the by checking if the error is not nil. For my purposes right now I don't care about the errors so I will be removing that in my code. You could also use the activity parameter to do different things based on which activity the user took. Finally, the returnedItems arrays is an array of NSExtenionItems. The array contains any modified data for the activity items that you supplied when you initialized the activity view controller. If no data was modified then this parameter will be nil. I hope that helps and as always. Happy Coding.
Sean Wernimont The Blind Squirrel Copyright 2015-2020
|
AuthorWelcome to The Blind Squirrel (because even a blind squirrel occasionally finds a nut). I'm a full-stack web and mobile developer that writes about tips and tricks that I've learned in Swift, C#, Azure, F# and more. Archives
April 2018
Categories
All
|