Post

Replies

Boosts

Views

Activity

Reply to Xcode 15.1 canvas preview not working right!
@Developer Tools Engineer Thanks for reviewing my code. I replaced your swift code for the ContentView. The iPhone simulator shows: pheripherals count: 0 start list of peripherals end list of peripherals since it has no access to bluetooth, but the mac preview is not working, although it shows the right view if i run the app. This is the whole project: ContentView.swift import SwiftUI struct ContentView: View { @ObservedObject private var central = BLECentral() var body: some View { VStack{ Text("pheripherals count: \(Array(central.discoveredPeripherals).count)") Text("start list of peripherals") ForEach(Array(central.discoveredPeripherals), id: \.key.identifier) { item in HStack { Text(item.key.name ?? "unnamed device") Text("\(item.value)") } } Text("end list of peripherals") } } } #Preview() { ContentView() } BLECentral.swift import Foundation import CoreBluetooth class BLECentral: NSObject, ObservableObject { private var centralManager: CBCentralManager? @Published var discoveredPeripherals: [CBPeripheral:NSNumber] = [:] var onDiscovered: (()->Void)? override init() { super.init() self.centralManager = CBCentralManager(delegate: self, queue: nil) } func scanForPeripherals(){ self.centralManager?.scanForPeripherals(withServices: nil) } } extension BLECentral: CBCentralManagerDelegate { func centralManagerDidUpdateState(_ central: CBCentralManager) { switch central.state { case .poweredOn: scanForPeripherals() case .poweredOff: print("Bluetooth is powered off. Please turn it on to use this app.") case .unauthorized: print("This app is not authorized to use Bluetooth.") case .unsupported: print("Bluetooth is not supported on this device.") case .resetting: print("Bluetooth is resetting.") case .unknown: print("Bluetooth state is unknown.") @unknown default: print("A new state has come up which isn't handled yet.") } } func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi: NSNumber) { if !discoveredPeripherals.keys.contains(peripheral){ self.discoveredPeripherals[peripheral] = rssi onDiscovered?() } else { discoveredPeripherals[peripheral] = rssi } } } This was a multi-platform application with proper bluetooth and its related privacies added to the project.
Jan ’24