Introduction
Platform APIs let apps use phone features like camera or GPS safely and easily.
Platform APIs let apps use phone features like camera or GPS safely and easily.
import AVFoundation import CoreLocation let captureSession = AVCaptureSession() // Use platform API to access camera let locationManager = CLLocationManager() locationManager.requestWhenInUseAuthorization() // Use platform API to access GPS
import AVFoundation
let captureSession = AVCaptureSession()
// Start camera capture sessionimport CoreLocation
let locationManager = CLLocationManager()
locationManager.requestWhenInUseAuthorization()
// Ask user permission to use locationThis simple app asks permission to use location and prints the current latitude and longitude in the console.
import UIKit import CoreLocation class ViewController: UIViewController, CLLocationManagerDelegate { let locationManager = CLLocationManager() override func viewDidLoad() { super.viewDidLoad() locationManager.delegate = self locationManager.requestWhenInUseAuthorization() locationManager.startUpdatingLocation() } func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { if let location = locations.first { print("Current location: \(location.coordinate.latitude), \(location.coordinate.longitude)") } } func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) { print("Failed to get location: \(error.localizedDescription)") } }
Always ask user permission before accessing sensitive device features.
Platform APIs simplify working with hardware and system services.
Using these APIs helps keep apps secure and user data private.
Platform APIs let apps safely use device features like camera and GPS.
They handle permissions and security for user privacy.
Using platform APIs makes app development easier and more reliable.
CLLocationManager().requestWhenInUseAuthorization()
print("Permission status: \(CLLocationManager.authorizationStatus())")let audioSession = AVAudioSession.sharedInstance() try audioSession.setActive(true) // Missing permission request let recorder = AVAudioRecorder(url: URL(fileURLWithPath: NSTemporaryDirectory() + "temp.m4a"), settings: nil) recorder.record()
Pass the test and this pattern moves unknown