I am writing a controller for an audio server on the iPhone. Each ‘view’ generally needs to get data from the TCP/IP socket as a client. I have sockets working from one class using the AsyncSocket class. (which, after trying to get a socket client working for more time than I’d like to admit, is a very impressive and helpful class).. This requires delegate functions to be written for recieving data…
- (void)onSocket:(AsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag
I have a standard table view interface where I see a list of Artists on one view, then when an artist is selected I move to the albums on the next view and so on.
My question is: When I go from view to view, what is the best way to still send and receive data? Do I need to create a whole new socket for each view class? (seems a bit over the top) Somehow link the local receiver delegate again?
I can’t seem to think of the ‘proper’ way to get this working outside of 1 class and there isn’t much online about socket client communications on the iphone.
It sounds like you want to have the delegate methods received by multiple objects, right? On Mac OS X, the solution is to use the notification system. I haven’t looked at the
AsyncSocketclass, but it looks like it only supports a delegate out of the box.Notifications are great because they let an object broadcast information to any other objects that are interested in receiving it. Objects register themselves with the notification center to be notified when particular notifications are posted. You could very easily add this capability by implementing your own class to wrap
AsyncSocket.Here’s what you’d do. You’d write your own class that has an
AsyncSocketas an instance variable. You would set this class as the delegate for theAsyncSocketobject. Then, when the delegate methods get called, you would post notifications to theNSNotificationCenter. You’ll probably need to stuff the parameters from the delegate methods into the notification’suserInfodictionary.On the flip side, your view controllers would sign up with the
NSNotificationCenteras observers for the notifications your custom class sends. Then, every time the delegate methods fire, each view controller will receive a notification of that event.Enough talk; here’s some code:
In the .m file:
Finally, in your various view controllers, you can write code like this:
Obviously, this code isn’t entirely complete, and I may have gotten some method names wrong (I’m doing this from memory) but this is a solution that should work for you.