iosobjective-csocketrocket

Calling a delegate function and return result


Im doing a small test project with SocketRocket on ios. But i can't seem to wrap my head around the logic.

Here's the thing: I have need to have a sort of "global" function to call the sockets. When my app opens it should connect using the websockets.

_webSocket.delegate = nil;
[_webSocket close];

_webSocket = [[SRWebSocket alloc] initWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"ws://localhost:12345/connectr"]]];
_webSocket.delegate = self;

self.title = @"Opening Connection...";
[_webSocket open];

However, i need to get the logic on connecting to a delegate function. I already thought of a singleton but i read that the singleton functions are quite cpu expensive.

So basically my question is: What is the best way to initiate a global accesable function that uses the (appdelegate) initalized SocketRocket instance.


Solution

  • you declare variable in appdelegate with property and synthesis

    you import appdelegate class in your requirement class

    AppDelegate  *objappdel=(AppDelegate*)[[UIApplication sharedApplication]delegate];  
    objappdel.variablename;