Bird
0
0

Given this code snippet in AppDelegate, what will happen when the app opens with URL scheme myapp://profile?id=123?

medium📝 Predict Output Q13 of 15
iOS Swift - Navigation
Given this code snippet in AppDelegate, what will happen when the app opens with URL scheme myapp://profile?id=123?
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
  if url.scheme == "myapp" && url.host == "profile" {
    print("Open profile with id: \(url.query ?? "none")")
    return true
  }
  return false
}
AReturns false without printing anything
BPrints 'Open profile with id: none' and returns false
CCrashes because url.query is nil
DPrints 'Open profile with id: id=123' and returns true
Step-by-Step Solution
Solution:
  1. Step 1: Check URL scheme and host

    The URL scheme is "myapp" and host is "profile", so the if condition is true.
  2. Step 2: Evaluate url.query value

    The query part is "id=123", so url.query is "id=123" and prints correctly.
  3. Final Answer:

    Prints 'Open profile with id: id=123' and returns true -> Option D
  4. Quick Check:

    URL matches scheme and host, query printed [OK]
Quick Trick: Check url.scheme and url.host before using url.query [OK]
Common Mistakes:
  • Assuming url.query is nil and causes crash
  • Ignoring url.host check
  • Returning false despite matching URL

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes