I am building an app which allows a user to draw on the screen. I'd like to add network capability so that user A can draw on user B's screen. My current plan is to build a system where I have my own UserOrNetworkTouch object which can be created based on either a real UITouch, or a message which comes over the network, and base all of the drawing in the app off of UserOrNetworkTouch events, rather than UITouch events.
Another thing I'll want to use this system for is recording touches, so a user will be able to press "record", and then play back their drawing at a later time.
I'd like to make sure I'm not reinventing the wheel here. Are there any libraries available which will handle some or all of this for me?
You probably wouldn't send the UITouch objects over the network (although you could if you wanted). I might package then touch positions into a struct of some kind and just send that to decrease the amount of traffic you were sending. If you needed the entire UITouch object and all of its data then sure, send the object to your server.
You could use CFNetwork framework to send data to a server from your client application. If you do you should really try to use IPv6.
Apple have sample code here for working with CFNetwork streams
If you want to record the touch events, just use an NSArray or an NSDictionary if you wanted to store say the touch along with a timestamp for when the touch occurred.
Then just add each touch to the array or dictionary as the user makes them.
Update: I wouldn't waste your time with Apple's WiTap sample code. I've read though it before and there is a LOT of code in it that is just confusing and irrelevant if you want a simple client/server app up and running quickly. It will more than likely be way too confusing for you if you haven't done any network programming before.
I would get the network transfers working first, then if you like you can refer to WiTap for the Bonjour networking part so you can do auto discovery of the client and server. But add Bonjour support only after you have data steams working first.
A good place to start would be Apple's WiTap sample. It sets up a game over Bonjour and sends taps back and forth.
Also look at GameKit which'll make some of the networking even simpler.
A SQLite DB would be a great place to record events. Search for the 'fmdb' SQLite wrapper for a nice Objective-C wrapper.
Related
I am currently starting my way on developing iPhone applications, and I have recently encountered a problem which I could not find a proper answer to.
In my application, I want one iPhone to send a request and another, far iPhone respond to that request using an Alert View, almost like the way Game Center works when you invite someone to play with you.
Do I do that with Push Notifications? Some server? Or what?
Thanks ahead,
A Newbie Developer.
Not the simplest things to try and do when you are new - but have a look at GameKit.
There is a sample application called GKTap which shows how to hook up two iPhones.
Don't be fooled by the word Game. This framework makes it easy to set up communication between phones, even using Bluetooth.
It depends on what you mean by "far". If you mean in another room, but within Bluetooth range or on the same local network, then there is GameKit. If you mean on a different network, or a long distance away, then you are looking at needing to have a server that keeps track of the devices and the connections between the devices so you can use Push Notifications. If you are thinking of something like Words With Friends where it sends a message to another user to tell them it is their go, then you would need the server.
I'm trying to figure out if game center is the right tool for me. Here's my situation:
I'm building an app which allows users to create their own musical compositions. I plan to make the compositions exportable and loadable via text file (xml or json format, probably). I'd like to allow users to share their compositions with friends. It seems to me that game kit is the default social network for iOS, so it seems worth looking at. However, the social aspect of game kit seems to be designed mainly for real-time, simultaneous play. Is there any way to share data with friends in a non-realtime way via game center? Is there even a way to send game center contacts a message?
If you look at the documentation for peer-to-peer networking in GameKit, you will probably find exactly what you are looking for, with a tiny exception. It is only for wifi/bluetooth connectivity.
The way I would go with your description is a private server that hosts the compositions. If you are familiar to Java and/or Python, I'd recommend using Google App Engine for the server. That would make the headache a little bit less.
You can use GameKit too, but as you describe it, it's more for real-time communication.
If you implement invitation handlers, you could "invite" another player to receive the composition, but if they reject you will have to retry manually at a later point.
The contents of what you sent over an open "GameKit connection" can be "anything" you like, stuffed into a NSData object, so there is no limits there.
I hope this helps you a tiny bit in the right direction, good luck!
I'm trying to understand the basic steps required for a game I'm trying to make using GameKit for bluetooth connections. I only have 1 device, so I can't test out my ideas very easily.
The game involves a maximum of 2 players over bluetooth via GameKit and the only data to send/receive is a 'shot' which includes position and direction data only.
This is what I think should happen, I really just want someone to point out any mistakes I've made:
The game starts by presenting a GKPeerPickerController with a connectionTypeMask of 'GKPeerPickerConnectionTypeNearby' and setting it's delegate to 'self'.
The GKPeerPickerController delegate method 'peerPickerController:didConnectPeer:toSession:' is implemented and sets the sessions dataReceiveHandler to self. The peerID is added to an NSMutableArray which holds all peer IDs connected. (There should only be 1 peer connected at any time).
To enforce only 1 peer connection per game the method 'session:didReceiveConnectionRequestFromPeer:' checks the 'count' property of the NSMutableArray which holds the connected peerIDs. If 'count'>0 then we respond by calling: 'denyConnectionFromPeer:' else, we call: 'acceptConnectionFromPeer:error:'
To send a shot, the game calls: 'sendData:toPeers:withDataMode:error:'
To receive shots, the game responds to: 'receiveData:fromPeer:inSession:context:'
Are there any steps missing? For example, if a user tries to connect to me before I connect to them, what will happen with my GKPeerPickerController? How do I know to dismiss it?
And without another iPhone/iPod I can't test.
First off, if you want to test Bluetooth in Gamekit you really are going to need a 2nd device. If you're serious about your app, try and buy a cheap/refurbished iPhone or iPod Touch that will support GameKit Bluetooth (the oldest devices don't do it). I actually think you do even better with two Macs for testing, but if you don't already have a second one that's an expensive proposition.
What you've posted above is pretty much correct - actually using the PeerPicker is pretty straightforward and you should be able to get through it easily using Apple's docs. The problem is that in my experience, it's a bit janky. It might be because I'm using a 3GS and a 3G to test and they have different Bluetooth inside, but a lot of times I see weird things when the scenario you outlined occurred - "if a user tries to connect to me before I connect to them." In the end it typically still works, but not without a little overhead of waiting and perhaps the user having to repeat the process a time or two.
I'm working on a game where really the only game part is (at least right now) that the user can unlock achievements as she does various things throughout the game. I have a database that can keep track of certain user actions and record how many times a user does something but I'm having trouble figuring out the best way to architect the app so that I have to do the least amount of work. Kind of suck with the timing because gamecenter is not ready and it seems like openfeint is changing gears, but maybe I'm wrong. I'd prefer to do everything "in house" if it is not too ridiculous. looking for suggestions.
One of the parts I'm having the most trouble figuring out is how to manage the state of all the achievements. NSuserdefaults vs. core data vs. a flat data file.
Also is there anyway to send a notification if a core data field reaches a certain amount?
Thanks,
Nick
Are you keeping the achievements on the device only, or also on the server? On the device, there is no reason not to use Core Data.
Using Core Data, you could use KVO on your model properties to monitor when certain values reach a new achievements.
I'm wondering if it would be possible at all to transfer data between two different iphones using gestures on one iphone? For instance, flick an image on iphone A and it would show up on iphone B.
I realize this would require some sort of communication channel between the two iphones (may be bluetooth?). Assuming such a communication channel has been established, is it possible? I'm looking for ideas from the community on how this can be accomplished if it is possible.
Thanks! - Azeem
Take a look at the Bump API. Sure the gesture would be knocking the two devices together, but it looks really easy to implement.
Sure, just open a connection between the devices (GameKit could provide the peer-to-peer connectivity). Create a method that sends content, then call that method when you detect the gesture that you'd like to use.
You'll obviously need to have views that display the content that you'd like to send, but you can easily detect that a user has initiated a touch within one of your views in order to determine which content to actually send.