I permessi dell'applicazione vanno salvati nel file info.plist (il corrispettivo di manifest.xml di Android).
Per visualizzare una mappa di usa MapView:
struct MapView: UIViewRepresentable {
var locationManager = CLLocationManager()
func setupManager() {
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()
locationManager.requestAlwaysAuthorization()
}
func makeUIView(context: Context) -> MKMapView {
setupManager()
let mapView = MKMapView(frame: UIScreen.main.bounds)
mapView.showsUserLocation = true
mapView.userTrackingMode = .follow
return mapView
}
func updateUIView(_ uiView: MKMapView, context: Context) {
}
}
struct ContentView: View {
var body: some View {
MapView()
}
}
Nella nuova versione di Xcode 14 è possibile effettuare:
struct ContentView: View {
@State private var region = MKCoordinateRegion(
center: CLLocationCoordinate2D(latitude: 45.563487, longitude: 9.198354),
span: MKCoordinateSpan(latitudeDelta: 0.03, longitudeDelta: 0.03)
)
var body: some View {
Map(coordinateRegion: $region)
}
}
class LocationManager: NSObject,CLLocationManagerDelegate, ObservableObject {
@Published var region = MKCoordinateRegion()
private let manager = CLLocationManager()
override init() {
super.init()
manager.delegate = self
manager.desiredAccuracy = kCLLocationAccuracyBest
manager.requestWhenInUseAuthorization()
manager.startUpdatingLocation()
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
locations.last.map {
region = MKCoordinateRegion(
center: CLLocationCoordinate2D(latitude: $0.coordinate.latitude, longitude: $0.coordinate.longitude),
span: MKCoordinateSpan(latitudeDelta: 0.5, longitudeDelta: 0.5)
)
}
}
}
struct ContentView: View {
@StateObject var manager = LocationManager()
var body: some View {
Map(coordinateRegion: $manager.region)
}
}
Gli User Defaults sono le impostazioni dell'utente, equivalenti agli Shared Preferences di Android.
Per salvare e caricare gli User Defaults si usa il decoratore @AppStorage:
@AppStorage("tapCountKey") private var tapCount = 0
Per il resto funziona esattamente come per gli @State.
Core Data è un framework di modellazione dati (basato su SQLite).
Si fa uso di un persistence controller per effettuare controlli di persistenza del database.
Il DB veniva gestito dal Database Manager (MOC).
Il successore di CoreData è SwiftData, definita con @Model.
import SwiftData
@Model
class Person {
var name: String
var age: Int
init(name: String, age: Int) {
self.name = name
self.age = age
}
}
Il main sarà costituito in questo modo:
import SwiftUI
import SwiftData
@main
struct MyApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
.modelContainer(for: [Persona])
}
}
List {
ForEach(orders) { order in
HStack {
VStack(alignment: .leading) {
Text("\(order.pizzaType) - \(order.numberOfSlices) slices")
.font(.headline)
Text("Table \(order.tableNumber)")
.font(.subheadline)
}
Spacer()
Button(action: {print("Update order")}) {
Text(order.orderStatus == .pending ? "Prepare" : "Complete")
.foregroundColor(.blue)
}
}
.frame(height: 50)
}
}
.listStyle(PlainListStyle())
@Model
class Company {
var name: String
var employees: [Person]
init(name: String, employees: [Person]) {
self.name = name
self.employees = employees
}
}
Per undo e redo:
context.undo()
context.redo()