robreuss / VirtualGameController

Software-based game controllers for iOS, tvOS, OS X and watchOS in Swift 4.2.
MIT License
462 stars 44 forks source link

Objective-C Usage #3

Closed phil-m closed 8 years ago

phil-m commented 8 years ago

Hi, This library seems really promising, & the samples seem to work great (tested between tvOS & iOS devices & it all ran very nicely).

I'm trying to use this in an existing objective-c based project, & have it compiling ok, but it seems a load of methods are missing from the 'VirtualGameController-Swift.h' file on the current version. I'm guessing this is just as no ones tried to use it outside of swift yet perhaps.

I've not really used swift much (so excuse if I'm missing something obvious), but on closer inspection, any methods that use certain swift specific features don't seem to be exposed in the header file, & so can't be accessed from objective-c code.

Adding a custom startAs alternative method to VgcManager as a test, which just changed the AppRole arg to a string & then it mapped the string arg to the 'CustomStringConvertible' based AppRole enum before calling the normal swift methods, which seemed to work. It exported the method in the header ok, & I could call & start the VGC setup up ok from objective-c.

I'm now trying to get the VgcService from the VgcPeripheralFoundService notification which is firing ok, but the whole of VgcService doesn't seem to be in the header file, & because of that, VgcPeripheral's connectToService etc. methods don't seem to be exposed. The object seems to only log out as 'VirtualGameController.VgcService' in the objective-c side.

Is there any plan to support objective-c usage? Or should it work already & maybe I've missed something obvious? Any tips on getting it going?

Thanks Phil

robreuss commented 8 years ago

Hi Phil,

I made a quick attempt to prepare it for Objective C use recently but ran into enough roadblocks (which I didn't understand without having to research) that I put it aside temporarily. I think it is hugely important to get it working with Objective C, so happy to turn my attention to that now.

I was conservative about exposing methods as public because I was originally planning to distribute just frameworks. Insofar as I went the open source route, I can probably ease up on that. I would still like to keep the public API sufficient though, just to ensure future compatibility.

Let's get this working! I'll start into it tomorrow.

Best, Rob

phil-m commented 8 years ago

Hey Rob,

That would be fantastic! Its seems a very feature complete & well thought out lib from what I've seen so far. I was only pondering recently about a similar 'bridging' type feature to find you've already implemented it!

Please let me know if I can help testing or anything, can't wait to get networked controllers working. Have already briefly tried & failed at Game Center based multi-player controllers, which seems to have quirks on tvOS, & was about to write a custom BLE controller interface till I read even raw BLE connections have the 3 device limit on tvOS currently. Started to look into writing a networking later instead, & then stumbled on your lib & realised I didn't have to write anything from scratch, happy days! ;)

Thanks, Phil

robreuss commented 8 years ago

I'm happy to report that I managed to get things working with Objective C, in both Central and Peripheral mode.

Maybe it's easiest if I send the sample via e-mail - prefer not to add still another sample project to the distribution at this time. Everything should be pretty obvious, except one thing to watch out for is a required build setting "Embedded content contains swift code" - that needs to be YES. FYI, the Central sample is minimally implemented so you'll need to look to the console for activity.

Send your e-mail address to virtualgamecontroller@gmail.com and I'll get it out to you. Plan to write up instructions to add to the documentation.

phil-m commented 8 years ago

That was fast! ;) Massive thanks for looking into it & fixing things up for obj-c so quickly.

Peeking over the commits, looks like it was only AppRole that needed porting in the end? So I'm guessing the other issue I had with VgcService is fixed by just adding the "Embedded content contains swift code" build setting? I thought I had that enabled at one point, but I was following various different suggestions I'd seen & may have toggled it back off at some point.

Will send an email for the sample, very much appreciated. Big thanks, Phil

robreuss commented 8 years ago

I haven't pushed the changes up yet. I think I'll wait for you to possibly identify more gaps.

The VgcService class was not available to Objective C because I had not sub-classed it from NSObject, and had not marked it with an @objc - you have to do one or the other to make a Swift class available to Objective C.

phil-m commented 8 years ago

Ah yep ok, had wondered if it really was that simple ;) Have received the sample project email, thank you. I should hopefully have a little time to test it out later today & will get back to you on how things go. Cheers! Phil

loretoparisi commented 8 years ago

It would be great to add a new example TARGET in Objective-C in order to have the bridge and everything in the example to run in a objective-c project. That would be awesome, thank you for your work.

robreuss commented 8 years ago

I've got a sample project that works - looking to clean it up a bit, create documentation, and roll it into the distribution in the next 24 hours!

loretoparisi commented 8 years ago

:100:

robreuss commented 8 years ago

I've added some instructions to the Wiki: https://github.com/robreuss/VirtualGameController/wiki/Implementing-in-Objective-C

Should have the sample project up in the next few hours!

Let me know if you see areas for improvement, or if you need help getting things up and running.

robreuss commented 8 years ago

I've pushed compatibility changes and the new sample project. Let me know if it gives you any trouble!

phil-m commented 8 years ago

Big thanks Rob for all the work & quick responses here & to the emails. Just to update this issue, I managed to get the sample working in my objective-c project after a few emails back & forth with Rob, who kindly (& very quickly) got the lib updated to work as needed. I'll update to the latest repo version soon.

Thanks also loretoparisi for showing an interest in the obj-c version, its nice to see others also might find it useful & warrant Rob's effort to get the samples & documentation updated to cover it.