I get the following errors when running the code below in a sandboxed app (it works when not sandboxed) and I have the com.apple.security.device.usb
entitlement enabled.
Error:Unable to open io_service_t object and create user client. with reason: IOServiceOpen failed.
Error Domain=IOUSBHostErrorDomain Code=-536870174 "Failed to create IOUSBHostObject." UserInfo={NSLocalizedRecoverySuggestion=, NSLocalizedDescription=Failed to create IOUSBHostObject., NSLocalizedFailureReason=IOServiceOpen failed.}
import Foundation
import IOKit
import IOKit.usb
import IOKit.usb.IOUSBLib
import IOKit.serial
import IOUSBHost
import IOUSBHost.IOUSBHostInterface
class USBService {
enum UsbError: Error {
case noDeviceMatched
case deviceCriteriaNotUnique
}
init() {
}
func getDevice(idVendor: Int?, idProduct: Int?) throws {
let deviceSearchPattern: [IOUSBHostMatchingPropertyKey : Int] = [
.vendorID : idVendor!,
.productID : idProduct!,
]
let deviceDomain = [ "IOProviderClass": "IOUSBHostDevice" ]
let searchRequest = (deviceSearchPattern as NSDictionary).mutableCopy() as! NSMutableDictionary
searchRequest.addEntries(from: deviceDomain)
let service = IOServiceGetMatchingService(kIOMasterPortDefault, searchRequest)
guard service != 0 else {
throw UsbError.noDeviceMatched
}
let device = try IOUSBHostDevice.init(__ioService: service, options: [], queue: nil, interestHandler: nil)
print(device.deviceDescriptor?.pointee.idProduct)
}
}