Bird
Raised Fist0
iOS Swiftmobile~20 mins

Share sheet (UIActivityViewController) in iOS Swift - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Share Sheet Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate
2:00remaining
What happens when you present UIActivityViewController without excluding any activity types?
You create and present a UIActivityViewController with a text string to share, but you do not set any excludedActivityTypes. What will the user see?
iOS Swift
let textToShare = "Hello from my app!"
let activityVC = UIActivityViewController(activityItems: [textToShare], applicationActivities: nil)
present(activityVC, animated: true)
AThe share sheet shows only the default iOS apps like Messages and Mail, excluding third-party apps.
BThe share sheet shows all available sharing options including Messages, Mail, and social media apps installed.
CThe share sheet shows no options because excludedActivityTypes is nil.
DThe app crashes because excludedActivityTypes must be set before presenting.
Attempts:
2 left
💡 Hint
Think about what happens if you don't limit or exclude any sharing options.
📝 Syntax
intermediate
2:00remaining
Which code snippet correctly excludes the AirDrop option from the share sheet?
You want to present a UIActivityViewController but exclude the AirDrop option. Which code snippet does this correctly?
A
activityVC.excludedActivityTypes = [.airDrop]
present(activityVC, animated: true)
B
activityVC.excludedActivityTypes = .airDrop
present(activityVC, animated: true)
C
activityVC.excludedActivityTypes = ["airDrop"]
present(activityVC, animated: true)
D
activityVC.excludedActivityTypes = [UIActivity.ActivityType.airDrop]
present(activityVC, animated: true)
Attempts:
2 left
💡 Hint
Check the correct enum type and syntax for excludedActivityTypes.
❓ lifecycle
advanced
2:00remaining
What happens if you present UIActivityViewController on iPad without setting popoverPresentationController sourceView?
You present a UIActivityViewController on an iPad but forget to set the popoverPresentationController's sourceView or barButtonItem. What is the expected behavior?
iOS Swift
let text = "Check this out!"
let activityVC = UIActivityViewController(activityItems: [text], applicationActivities: nil)
present(activityVC, animated: true)
AThe share sheet appears but with a warning in the console only.
BThe share sheet appears centered on the screen without any issues.
CThe app crashes with a runtime exception about missing popover sourceView.
DThe share sheet does not appear and the app freezes.
Attempts:
2 left
💡 Hint
iPad requires a popover anchor for modal presentations like UIActivityViewController.
❓ navigation
advanced
2:00remaining
How to dismiss UIActivityViewController programmatically after sharing completes?
You want to dismiss the UIActivityViewController automatically after the user finishes sharing. Which approach correctly detects completion and dismisses it?
iOS Swift
let activityVC = UIActivityViewController(activityItems: ["Hello"], applicationActivities: nil)
// What to add here?
present(activityVC, animated: true)
ASet activityVC.completionWithItemsHandler to a closure that calls dismiss(animated: true).
BSet activityVC.delegate = self and implement activityViewControllerDidFinish to dismiss.
CCall dismiss(animated: true) immediately after present(activityVC, animated: true).
DUse NotificationCenter to listen for UIActivityViewControllerDidFinish notification and dismiss.
Attempts:
2 left
💡 Hint
UIActivityViewController has a completion handler property for this purpose.
🧠 Conceptual
expert
3:00remaining
Why should you avoid sharing UIImages directly in UIActivityViewController on iOS 16+?
Starting with iOS 16, sharing UIImages directly in UIActivityViewController can cause issues. What is the recommended approach and why?
AUse file URLs pointing to saved images instead of UIImage objects to avoid memory and performance issues.
BAlways convert UIImages to Data before sharing to ensure compatibility with all apps.
CSharing UIImages directly is recommended for best performance and no issues exist on iOS 16+.
DWrap UIImages inside a custom UIActivity subclass to fix sharing problems on iOS 16+.
Attempts:
2 left
💡 Hint
Think about how large images affect memory and how file URLs can help.

Practice

(1/5)
1. What is the main purpose of UIActivityViewController in an iOS app?
easy
A. To manage app settings
B. To present a share sheet allowing users to share content
C. To create a custom navigation bar
D. To handle background tasks

Solution

  1. Step 1: Understand the role of UIActivityViewController

    UIActivityViewController is designed to present a share sheet for sharing content.
  2. Step 2: Compare with other options

    Options A, B, and D relate to different app functions not handled by UIActivityViewController.
  3. Final Answer:

    To present a share sheet allowing users to share content -> Option B
  4. Quick Check:

    Share sheet = UIActivityViewController [OK]
Hint: UIActivityViewController always shows sharing options [OK]
Common Mistakes:
  • Confusing UIActivityViewController with navigation or settings controllers
  • Thinking it manages background tasks
  • Assuming it customizes UI elements beyond sharing
2. Which of the following is the correct way to initialize a UIActivityViewController with a text string to share?
easy
A. UIActivityViewController(activityItems: ["Hello World"], applicationActivities: nil)
B. UIActivityViewController(items: "Hello World")
C. UIActivityViewController(shareItems: ["Hello World"])
D. UIActivityViewController(content: "Hello World")

Solution

  1. Step 1: Recall the initializer signature

    The correct initializer is UIActivityViewController(activityItems: [Any], applicationActivities: [UIActivity]?).
  2. Step 2: Match the correct syntax

    UIActivityViewController(activityItems: ["Hello World"], applicationActivities: nil) matches the correct syntax with an array of items and nil for activities.
  3. Final Answer:

    UIActivityViewController(activityItems: ["Hello World"], applicationActivities: nil) -> Option A
  4. Quick Check:

    Initializer uses activityItems array [OK]
Hint: Use activityItems array to pass share content [OK]
Common Mistakes:
  • Passing a single string instead of an array
  • Using wrong parameter names like items or shareItems
  • Omitting applicationActivities parameter
3. Given the code snippet below, what will happen when the button is tapped?
let textToShare = "Check this out!"
let activityVC = UIActivityViewController(activityItems: [textToShare], applicationActivities: nil)
present(activityVC, animated: true)
medium
A. The text is copied automatically to clipboard
B. The app crashes due to missing delegate
C. A share sheet appears allowing the user to share the text
D. Nothing happens because the share sheet is not presented

Solution

  1. Step 1: Analyze the code behavior

    The code creates a UIActivityViewController with a text item and presents it modally.
  2. Step 2: Understand UIActivityViewController presentation

    Presenting it modally shows the share sheet with sharing options for the text.
  3. Final Answer:

    A share sheet appears allowing the user to share the text -> Option C
  4. Quick Check:

    Presenting UIActivityViewController shows share sheet [OK]
Hint: Presenting UIActivityViewController shows share sheet [OK]
Common Mistakes:
  • Assuming delegate is required to present share sheet
  • Thinking share sheet appears automatically without present()
  • Confusing share sheet with clipboard copy
4. Identify the error in the following code that tries to present a share sheet:
let image = UIImage(named: "photo.png")
let activityVC = UIActivityViewController(activityItems: [image], applicationActivities: nil)
present(activityVC, animated: true)
medium
A. present() must be called on the activityVC instance
B. UIActivityViewController requires a delegate to present
C. The activityItems parameter must be a single item, not an array
D. UIImage named "photo.png" may be nil

Solution

  1. Step 1: Check UIImage initialization

    UIImage(named:) returns an optional and may be nil if the image is missing.
  2. Step 2: Understand impact on activityItems

    Passing nil in activityItems array results in no shareable content.
  3. Final Answer:

    UIImage named "photo.png" may be nil -> Option D
  4. Quick Check:

    UIImage(named:) can be nil if image missing [OK]
Hint: Always unwrap UIImage before sharing [OK]
Common Mistakes:
  • Assuming delegate is required for UIActivityViewController
  • Passing single item instead of array to activityItems
  • Calling present() on wrong object
5. You want to share both a URL and a text message using UIActivityViewController. Which code snippet correctly sets this up and presents the share sheet?
hard
A. let url = URL(string: "https://example.com")! let message = "Check this out!" let activityVC = UIActivityViewController(activityItems: [url, message], applicationActivities: nil) present(activityVC, animated: true)
B. let url = URL(string: "https://example.com") let message = "Check this out!" let activityVC = UIActivityViewController(activityItems: url, applicationActivities: message) present(activityVC, animated: true)
C. let url = URL(string: "https://example.com")! let message = "Check this out!" let activityVC = UIActivityViewController(items: [url, message]) present(activityVC, animated: true)
D. let url = URL(string: "https://example.com")! let message = "Check this out!" let activityVC = UIActivityViewController(activityItems: [message], applicationActivities: [url]) present(activityVC, animated: true)

Solution

  1. Step 1: Verify correct initializer usage

    UIActivityViewController requires activityItems as an array of items to share and applicationActivities as optional custom activities.
  2. Step 2: Check each option's parameters

    let url = URL(string: "https://example.com")! let message = "Check this out!" let activityVC = UIActivityViewController(activityItems: [url, message], applicationActivities: nil) present(activityVC, animated: true) correctly passes an array with URL and text, and nil for applicationActivities.
  3. Step 3: Confirm safe URL unwrapping

    let url = URL(string: "https://example.com")! let message = "Check this out!" let activityVC = UIActivityViewController(activityItems: [url, message], applicationActivities: nil) present(activityVC, animated: true) force unwraps URL safely assuming it exists, which is common in examples.
  4. Final Answer:

    let url = URL(string: "https://example.com")! let message = "Check this out!" let activityVC = UIActivityViewController(activityItems: [url, message], applicationActivities: nil) present(activityVC, animated: true) -> Option A
  5. Quick Check:

    Pass array of items to activityItems [OK]
Hint: Pass all share items in activityItems array [OK]
Common Mistakes:
  • Passing items or applicationActivities incorrectly
  • Not using an array for activityItems
  • Confusing applicationActivities with share content
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard