iosswiftios8ios8.3

Calling the system share menu on iOS 8


I'm trying to figure out how to programatically show the system share menu, but all info I can find is how to register an app to said menu.

How can I call the dialog programatically?


Solution

  • You need to create an instance of UIActivityViewController, here's a quick example

    let objectsToShare = ["a string"]
    let activityController = UIActivityViewController(
            activityItems: objectsToShare,
            applicationActivities: nil)
    
        // if your app can run on an iPad, this menu
        // will present as a pop over controller
        // and as such, needs to be configured
        // consider the bar button item property
        // if needed
    
        // should be the rect that the pop over should anchor to
        activityController.popoverPresentationController?.sourceRect = view.frame
        activityController.popoverPresentationController?.sourceView = view
        activityController.popoverPresentationController?.permittedArrowDirections = .any
    
        // present the controller
        present(activityController, animated: true, completion: nil)