HiJack In the Box – Part 3 – iPhone HiJack Tutorial
August 21, 2011
Now that our setup is done let us get on with the code!
From the HiJack readme we would do just a couple of things and “The simplest way is to open the project, and make it. Then, add the libHiJack.a and HiJackMgr.h to your iPhone application“ to easily integrate the HiJack lib to our app. But from Part 2 of our tutorial, I think I had experienced otherwise.
For those of you who would like to view the code and follow through, you can download it from here:
Anyway, to start, let us go to the HiJackInTheBoxAppDelegate.h file and then add in the delegate and the instance variable:
Then going to our HiJackInTheBoxAppDelegate.mm file, we will have to initialize the hiJackMgr instance variable:
Of course, it won’t be complete withouth deallocating it, so here we go:
Then let us implement the (int) receive delegate still on our HiJackInTheBoxAppDelegate.mm file:
Let us test our code and build it, I hope you’d get the a success:
We can now move on to our view! But first, how can we pass our sensor’s value to the view? We can create a new “(float) sensorValue” property on our HiJackInTheBoxViewController.h file:
and synthesize it on our implementation file HiJackInTheBoxViewController.m:
Going back to our HiJackInTheBoxViewController.m code, we can look for the code that makes the gradient box bounce:
and replace it with our sensorValue property:
Hmmm.. now we have a clue that the transY variable is the one that calls the shots! So let us find more of it and replace it with our sensorValue variable:
Now let us run the app! Be sure to plug in your HiJack to your iOS device and when you do, turn up the volume too. Hmmm.. but wait, if you are running it you now would notice that there is no movement from the box. It is because that our implementation before for the (int) receive delegate is not yet complete. Here it is now going back to the HiJackInTheBoxAppDelegate.mm source:
Why divide? Since the value of UInt8 may range from 0 to 255, we can get a “percentage” value instead and pass this to the viewController.
So try it now and run it!
Here is a video showing the action:
I hope you can now control the box, up and down a little. But there is a bit of a snag with it because it does not respond quickly. I will contact the HiJack lib developers about this and keep you posted.
Here is the link to the code again:
I hope you have enjoyed this 3 part tutorial! Comments are very welcome.