Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a share sheet with a text message.
iOS Swift
let textToShare = "Hello from my app!" let activityVC = UIActivityViewController(activityItems: [[1]], applicationActivities: nil)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing self or view instead of the text variable.
Not putting the item inside an array.
✗ Incorrect
The share sheet needs the content to share inside an array. Here, we share the text stored in textToShare.
2fill in blank
mediumComplete the code to present the share sheet from the current view controller.
iOS Swift
present(activityVC, animated: [1], completion: nil) Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing false disables animation and is less user-friendly.
Passing nil or 0 causes errors.
✗ Incorrect
The share sheet should be presented with animation, so pass true for the animated parameter.
3fill in blank
hardFix the error in the code to avoid a crash on iPad by setting the popoverPresentationController's sourceView.
iOS Swift
if let popover = activityVC.popoverPresentationController { popover.sourceView = [1] }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Setting sourceView to nil causes a crash on iPad.
Using activityVC or UIApplication.shared is incorrect.
✗ Incorrect
On iPad, the share sheet must have a sourceView for the popover. Setting it to self.view fixes the crash.
4fill in blank
hardFill both blanks to share an image named "photo.png" from the app bundle.
iOS Swift
if let image = UIImage(named: [1]) { let activityVC = UIActivityViewController(activityItems: [[2]], applicationActivities: nil) }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Including the file extension in the image name.
Passing the string instead of the image variable to activityItems.
✗ Incorrect
UIImage(named:) expects the image name without extension. The activityItems array needs the image variable.
5fill in blank
hardFill all three blanks to share a URL and exclude the AirDrop activity from the share sheet.
iOS Swift
if let url = URL(string: [1]) { let activityVC = UIActivityViewController(activityItems: [url], applicationActivities: nil) activityVC.excludedActivityTypes = [[2]] present(activityVC, animated: [3], completion: nil) }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Not quoting the URL string.
Using wrong activity type to exclude.
Passing false to animated parameter.
✗ Incorrect
The URL string must be quoted. To exclude AirDrop, use .airDrop. Present the share sheet with animation true.