Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Swift - Import IOKit HID Files

I'm trying to use IOKit HID manager in swift and used to import the files in OBJ C IOHIDManager.h and IOHIDKey.h etc


I've tried simply using :


import IOKit

import IOKit.hid

and import IOKit.hid.IOHIDManager which it spits it's dummy out at


but when doing the first two I don't get access to things like IOHIDManagerRef etc


How do I get these framework files to load. Do I need one of these 'Bridging header files' which I don't seem to need with the Foundation framework.


Cheers

Steve

Posted on Oct 22, 2014 5:18 AM

Reply
5 replies

Nov 22, 2014 7:23 AM in response to Cacus

I tried going with an Objective-C bridging header, but that didn't work either.


It seems that the problem simply is that the IOKit framework is written in C++ (or atleast a subset of it), which is plain not supported in Swift in a direct manner.

"You cannot import C++ code directly into Swift. Instead, create an Objective-C or C wrapper for C++ code."

See here: https://developer.apple.com/library/mac/documentation/DeviceDrivers/Conceptual/I OKitFundamentals/Features/Features.html

(and here: http://stackoverflow.com/questions/24042774/can-i-mix-swift-with-c-like-the-obje ctive-c-mm-files for a discussion about the matter.)


Mar 19, 2015 1:29 AM in response to cuxdev

Hi Cuxdev


This is sort of a yes and no answer really. First take into consideration that I'm not a accomplished programmer but here's what I found.

The import IOKit.hid seems to (as far as auto complete in your code) work as long as the class does not subclass from say NSObject eg


class hid : NSObject {


}


would only give IOHIDButtonModes as an auto complete option in your code but nothing else starting with IOHID...


class hid {

}


Works fine (for Xcode to auto complete)


However there are a couple of issues I hit. First most things need a takeRetainValue() adding and other ARC friendly additives eg


let hidManager : IOHIDManagerRef = IOHIDManagerCreate(kCFAllocatorDefault, 0).takeRetainedValue()


Also as you can see from above if using HIDkeys the things like HIDOption types didn't work (for me) so in the above were you may have had


IOHIDManagerCreate(kCFAllocatorDefault, kIOHIDOptionsTypeNone)


in ObjC I had to place the actual int. Not a Major issue but finding these things out was a major pain in the butt


I then had all sorts of issues with the call backs eg

IOHIDManagerRegisterDeviceMatchingCallback(hidManager, &Handle_DeviceMatchingCallback, self);


Xcode errors in it's usual cryptic way. In the end I gave in and left the my code in objective C. (This had been an exercise in seeing if I could convert an old app to Swift).


Now don't get me wrong there's a lot of things I like about Swift especially as I came to ObjC from PHP and Swift is more PHP like but for anything that uses IOKit I'd stick with ObjC until Apple creates a pure swift/arc version of IOKit. But in honesty I'm not sure they will.


Cheers

Steve

Oct 5, 2015 2:06 PM in response to etresoft

Maybe I'm just weird, but I would have thought that "read a joystick controller" is something simple an app developer - especially an apple game app developer - might desire. I mean - back in the day, STICK(0) and a bitmask worked (and we liked it, whippersnappers!)


If we take it as a given for a moment that the existing HID frameworks are ugly or impossible to use directly with swift (ie. you need to write an Objective-C shim) - what are the alternatives? HIDAPI is one, but you're still back to wrapping an external C interface (right? I am using it with FFI in other languages, but I am not sure what the analog to that is in swift, other than "yes, it can be done"). I've seen a few other wrappers, but do not have the expertise yet to tell if one or the other is "the right way".


And if there is not one... really? (It always boggles me a bit how a company as quality-minded as apple could let something like this slide. Don't even get me started about MacRuby...)

Swift - Import IOKit HID Files

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.