<html><head><meta http-equiv="Content-Type" content="text/html charset=us-ascii"><meta http-equiv="Content-Type" content="text/html charset=us-ascii"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">Hi swift community,<div class=""><br class=""></div><div class="">I am trying to access an USB device in a swift project, with quite limited knowledge of IOKit and not a log of experience using unsafe pointers with swift.</div><div class=""><br class=""></div><div class="">My goal is to create a class that will listen to connections and disconnections of a device matching a productID and vendorID.</div><div class="">What I have done up to now is extensively based on that documentation (not swift oriented) :</div><div class=""><a href="https://developer.apple.com/library/content/documentation/DeviceDrivers/Conceptual/USBBook/USBDeviceInterfaces/USBDevInterfaces.html#//apple_ref/doc/uid/TP40002645-BBIEIEII" class="">https://developer.apple.com/library/content/documentation/DeviceDrivers/Conceptual/USBBook/USBDeviceInterfaces/USBDevInterfaces.html#//apple_ref/doc/uid/TP40002645-BBIEIEII</a></div><div class=""> </div><div class="">The result of my tests and trial can be found here : <a href="http://pastebin.com/rSqdFMwH" class="">http://pastebin.com/rSqdFMwH</a></div><div class="">(sorry for the pastebin link, but the code is almost 300 lines, which is quite a lot for a mail)</div><div class=""><br class=""></div><div class="">This code compiles on Xcode 8, and the callbacks on plugging and unplugging the device works fine.</div><div class="">But it systematically crashes with an EXC_BAD_ACCESS on the first use of the IOUSBDeviceInterface when a device is detected (on line 143, when trying to open the device).</div><div class=""><br class=""></div><div class="">I guess to have such an error, I must be messing badly with the UnsaveMutablePointer.</div><div class=""><br class=""></div><div class="">Would anyone master IOKit and swift enough to spot my mistake ?</div><div class="">You can test it by executing that code in a sample project :</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class="">try</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #3495af" class="">USBDevice</span><span style="font-variant-ligatures: no-common-ligatures" class="">(vendorId: 0x04d8, productId: 0xf372)</span></div></div><div class=""><br class=""></div><div class="">where vendorId and productId should match one of your connected devices.</div><div class=""><br class=""></div><div class="">Also, there are very few exemples of swift code dealing with IOKit and USB, I would be glad to contribute by publishing that code if I can have it working !</div><div class=""><br class=""></div><div class="">Thanks for your help !</div><div class=""><br class=""></div><div class="">Jerome</div><div class=""><br class=""></div></body></html>