present in a popover vs. present modally

At https://developer.apple.com/reference/uikit/uiactivityviewcontroller it says about UIActivityViewController,


"When presenting the view controller, you must do so using the appropriate means for the current device. On iPad, you must present the view controller in a popover. On iPhone and iPod touch, you must present it modally."


So I wrote the following code to set an instance of the UIActivityViewController to be a popover:


activityViewController.modalPresentationStyle = UIModalPresentationStyle.popover


What do I set the modalPresentationStyple property to if I present the instance of the UIActivityViewController to present modally?

Answered by ShinehahGnolaum in 230602022

I figured it out. I had to set other properties. Here's my code:


@IBAction func showMore(_ sender: UIBarButtonItem) {

let string = "Hello World!"

let activityItems = [string]

let activityViewController = UIActivityViewController.init(activityItems: activityItems, applicationActivities: nil)

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiom.pad) {

activityViewController.modalPresentationStyle = UIModalPresentationStyle.popover

activityViewController.preferredContentSize = CGSize(width: 0, height: 0)

activityViewController.popoverPresentationController?.barButtonItem = barButtonItemMore

present(activityViewController, animated: true, completion: nil)

}

else

{

present(activityViewController, animated: true, completion: nil)

}

}

Hi,


I don't set modalPresentationStyle at all. I've a sharing helper class, which just creates the UIActivityViewController instance, sets sharing related properties like excludedActivityTypes and calls

    [presentingViewController presentViewController:avc animated:YES completion:nil];


On an iPad it is presented in a popover and on an iPhone fullscreen.


Dirk

Wouldn't I use:


let activityViewController = UIActivityViewController.init(activityItems: activityItems, applicationActivities: nil)

present(activityViewController, animated: true, completion: nil)


or would I need to check whether the device is an iPhone or iPad by checking UI_USER_INTERFACE_IDIOM() and then have a separate call to present(_:animated:completion:) depending on the value of UI_USER_INTERFACE_IDIOM()?

I just tried the code I commented above which is:


let activityViewController = UIActivityViewController.init(activityItems: activityItems, applicationActivities: nil)


present(activityViewController, animated: true, completion: nil)


I ran the project on the simulator for an iPad and it raised an uncaught exception error.

Accepted Answer

I figured it out. I had to set other properties. Here's my code:


@IBAction func showMore(_ sender: UIBarButtonItem) {

let string = "Hello World!"

let activityItems = [string]

let activityViewController = UIActivityViewController.init(activityItems: activityItems, applicationActivities: nil)

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiom.pad) {

activityViewController.modalPresentationStyle = UIModalPresentationStyle.popover

activityViewController.preferredContentSize = CGSize(width: 0, height: 0)

activityViewController.popoverPresentationController?.barButtonItem = barButtonItemMore

present(activityViewController, animated: true, completion: nil)

}

else

{

present(activityViewController, animated: true, completion: nil)

}

}

present in a popover vs. present modally
 
 
Q