0

im new to iOS and objective-c, but im trying to use GCDAsyncSocket I'm using it to contact TCP/IP server for the whole application, in each viewController i want to start sending and receiving from/to server, so each time i alloc new instance of cocaSocket, and it send to the server like im new client.

so how can i make sure that one instance of cocaSocket could be applied to the whole application.

thanks for any response !

Omiga
  • 572
  • 3
  • 11

1 Answers1

2

Well, you might create the Socket in your AppDelegate and store it in a property.

 // pseudo code - did not check the documentation how to actually create such a beast
 self.socket = [[GCDAsyncSocket alloc] init];

Then in other classes, you may access the app delegate via

(myAppDelegate *) [[[UIApplication sharedApplication] delegate] socket];

I don't think you'll need a singleton here.

Axel
  • 1,716
  • 1
  • 16
  • 28
  • +1 Technically it *is* a singleton since it is a property on an object that is itself a singleton :) But it's a much better solution IMO than mucking about creating your own singletons. – JeremyP May 28 '12 at 10:14
  • i'll try it and let you know ! – Omiga May 28 '12 at 10:24