Bird
Raised Fist0
iOS Swiftmobile~10 mins

Why platform APIs access device capabilities in iOS Swift - Test Your Understanding

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the framework that allows access to device features.

iOS Swift
import [1]
Drag options to blanks, or click blank then click option'
AFoundation
BUIKit
CCoreData
DAVFoundation
Attempts:
3 left
💡 Hint
Common Mistakes
Choosing Foundation which is for basic data types and collections.
Choosing CoreData which is for data storage.
Choosing AVFoundation which is for audio and video.
2fill in blank
medium

Complete the code to request permission to use the device camera.

iOS Swift
AVCaptureDevice.requestAccess(for: .video) { granted in
    if [1] {
        print("Camera access granted")
    } else {
        print("Camera access denied")
    }
}
Drag options to blanks, or click blank then click option'
Agranted
Bpermission
Caccess
Dallowed
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name not defined in the closure parameter.
Confusing the parameter with a permission object.
3fill in blank
hard

Fix the error in the code to get the current device orientation.

iOS Swift
let orientation = UIDevice.current.[1]
Drag options to blanks, or click blank then click option'
Aorientation
BdeviceOrientation
CcurrentOrientation
DorientationState
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect property names that do not exist.
Trying to access orientation from the wrong object.
4fill in blank
hard

Fill both blanks to create a dictionary that maps sensor names to their availability status.

iOS Swift
let sensors = ["Accelerometer": [1], "Gyroscope": [2]]
Drag options to blanks, or click blank then click option'
ACMMotionManager().isAccelerometerAvailable
BCMMotionManager().isGyroAvailable
CCMMotionManager().isMagnetometerAvailable
DCMMotionManager().isDeviceMotionAvailable
Attempts:
3 left
💡 Hint
Common Mistakes
Using properties for other sensors like magnetometer or device motion.
Mixing up the property names.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that filters sensors available on the device.

iOS Swift
let availableSensors = [
    [1]: [2] for [3] in ["Accelerometer", "Gyroscope", "Magnetometer"]
    if CMMotionManager().is[1]Available
]
Drag options to blanks, or click blank then click option'
Asensor
Btrue
Dfalse
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'false' as value which would be incorrect for available sensors.
Using different variable names inconsistently.

Practice

(1/5)
1. Why do iOS platform APIs provide access to device features like the camera and GPS?
easy
A. To let apps use device features safely and protect user privacy
B. To make the device slower by adding extra steps
C. To prevent apps from running in the background
D. To allow apps to change system settings without permission

Solution

  1. Step 1: Understand platform API role

    Platform APIs act as a safe bridge between apps and device hardware.
  2. Step 2: Recognize privacy and security purpose

    They manage permissions to protect user data and privacy when apps access features like camera or GPS.
  3. Final Answer:

    To let apps use device features safely and protect user privacy -> Option A
  4. Quick Check:

    Platform APIs = safe device access [OK]
Hint: Platform APIs protect privacy when apps use device features [OK]
Common Mistakes:
  • Thinking APIs slow down the device
  • Believing APIs allow apps to change system settings freely
  • Confusing background app behavior with device access
2. Which Swift code snippet correctly requests camera access permission using platform APIs?
easy
A. requestCameraAccess()
B. Camera.requestPermission()
C. AVCaptureDevice.requestAccess(for: .video) { granted in /* handle */ }
D. AVCaptureDevice.askPermission()

Solution

  1. Step 1: Identify correct API method

    The official method to request camera access is AVCaptureDevice.requestAccess(for: .video).
  2. Step 2: Check method syntax

    It takes a closure to handle the user's response, matching the code in AVCaptureDevice.requestAccess(for: .video) { granted in /* handle */ }.
  3. Final Answer:

    AVCaptureDevice.requestAccess(for: .video) { granted in /* handle */ } -> Option C
  4. Quick Check:

    Correct API method = AVCaptureDevice.requestAccess(for: .video) { granted in /* handle */ } [OK]
Hint: Look for AVCaptureDevice.requestAccess syntax [OK]
Common Mistakes:
  • Using non-existent methods like requestPermission()
  • Omitting the closure parameter
  • Confusing method names with similar sounding ones
3. What will the following Swift code print if the user denies location permission?
CLLocationManager().requestWhenInUseAuthorization()
print("Permission status: \(CLLocationManager.authorizationStatus())")
medium
A. Permission status: authorizedWhenInUse
B. Permission status: notDetermined
C. Permission status: restricted
D. Permission status: denied

Solution

  1. Step 1: Understand authorizationStatus meaning

    authorizationStatus() returns the current permission state for location access.
  2. Step 2: Analyze execution timing

    requestWhenInUseAuthorization() is asynchronous; the print executes immediately before user response, so status is .notDetermined even if denied later.
  3. Final Answer:

    Permission status: notDetermined -> Option B
  4. Quick Check:

    Immediate status after async request = notDetermined [OK]
Hint: requestWhenInUseAuthorization is async, immediate check = notDetermined [OK]
Common Mistakes:
  • Confusing notDetermined with denied
  • Assuming authorizedWhenInUse prints without user approval
  • Mixing restricted with denied
4. Identify the error in this Swift code that tries to access the microphone without requesting permission:
let audioSession = AVAudioSession.sharedInstance()
try audioSession.setActive(true)
// Missing permission request
let recorder = AVAudioRecorder(url: URL(fileURLWithPath: NSTemporaryDirectory() + "temp.m4a"), settings: nil)
recorder.record()
medium
A. AVAudioRecorder() cannot be initialized without parameters
B. AVAudioSession.sharedInstance() is incorrect usage
C. record() method does not exist on AVAudioRecorder
D. Missing call to request microphone permission before recording

Solution

  1. Step 1: Check permission handling

    The code does not request microphone permission, which is required before recording.
  2. Step 2: Confirm other code parts

    AVAudioSession.sharedInstance() is correct, AVAudioRecorder is initialized with required URL and settings parameters, and record() exists.
  3. Final Answer:

    Missing call to request microphone permission before recording -> Option D
  4. Quick Check:

    Permission request missing = Missing call to request microphone permission before recording [OK]
Hint: Always request permission before accessing device features [OK]
Common Mistakes:
  • Thinking AVAudioSession.sharedInstance() is wrong
  • Ignoring the need for permission requests
  • Assuming AVAudioRecorder() can be used without initialization
5. An app needs to access both camera and location. Which approach best ensures user privacy and app reliability?
hard
A. Request camera and location permissions separately using platform APIs and handle denial gracefully
B. Access camera and location directly without permissions for faster performance
C. Request only camera permission and assume location is allowed
D. Request all permissions at app install without user prompts

Solution

  1. Step 1: Understand permission best practices

    Requesting permissions separately lets users control access and improves privacy.
  2. Step 2: Recognize importance of handling denial

    Handling denial gracefully avoids app crashes and respects user choice.
  3. Step 3: Evaluate other options

    Direct access without permission is not allowed; assuming permissions or requesting all at install violates privacy rules.
  4. Final Answer:

    Request camera and location permissions separately using platform APIs and handle denial gracefully -> Option A
  5. Quick Check:

    Separate permission requests + graceful handling = Request camera and location permissions separately using platform APIs and handle denial gracefully [OK]
Hint: Request permissions separately and handle denial [OK]
Common Mistakes:
  • Trying to access features without permission
  • Assuming permissions are granted by default
  • Requesting all permissions upfront without explanation
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