Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to handle a URL in the AppDelegate method.
iOS Swift
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
return [1]
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Returning false causes the system to think the URL was not handled.
Returning nil is invalid because the method expects a Bool.
✗ Incorrect
Returning true indicates the app successfully handled the URL.
2fill in blank
mediumComplete the code to extract the host from the incoming URL.
iOS Swift
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
guard let host = url.[1] else { return false }
print("Host: \(host)")
return true
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using url.path returns the path after the host, not the host itself.
Using url.scheme returns the protocol like 'https'.
✗ Incorrect
The host property gives the domain or host part of the URL.
3fill in blank
hardFix the error in the code to correctly parse query parameters from the URL.
iOS Swift
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
let components = URLComponents(url: url, resolvingAgainstBaseURL: false)
if let queryItems = components?.[1] {
for item in queryItems {
print("\(item.name): \(item.value ?? "")")
}
}
return true
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'query' or 'parameters' causes a compile error because those properties do not exist.
Forgetting to unwrap optional components.
✗ Incorrect
URLComponents has a property named queryItems which is an array of URLQueryItem.
4fill in blank
hardFill both blanks to create a URL scheme check and handle it.
iOS Swift
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
if url.[1] == "myapp" {
// Handle the URL
print("Custom scheme detected")
return [2]
}
return false
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Checking host instead of scheme for the URL type.
Returning false even after handling the URL.
✗ Incorrect
The scheme property gives the URL scheme like 'myapp'. Returning true means the URL was handled.
5fill in blank
hardFill all three blanks to safely unwrap the URL, check the scheme, and extract the path.
iOS Swift
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
guard url.[1] == "myapp" else { return false }
let path = url.[2] ?? ""
print("Path: \(path)")
return [3]
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Not unwrapping the optional URL before use.
Checking host instead of scheme.
Returning false even after handling the URL.
✗ Incorrect
First check the scheme, then get the path property, and return true to confirm handling.