Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I make GCDAsyncSocket that is declared in AppDelegate available to view controllers
    text
    copied!<p>Following a post of similar question (which doesn't work), I declared a instance of GCDAsyncSocket on AppDelegate.h</p> <pre><code>#import &lt;UIKit/UIKit.h&gt; @class ViewController; @class GCDAsyncSocket; @interface AppDelegate : UIResponder &lt;UIApplicationDelegate&gt; { GCDAsyncSocket *asyncSocket; } @property (strong, nonatomic) UIWindow *window; @property (nonatomic, retain) GCDAsyncSocket *asyncSocket; @property (strong, nonatomic) ViewController *viewController; @end </code></pre> <p>and do the socket initialization in AppDelegate.m</p> <pre><code>#import "AppDelegate.h" #import "GCDAsyncSocket.h" #import "ViewController.h" @implementation AppDelegate @synthesize asyncSocket; - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Override point for customization after application launch. self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil]; self.window.rootViewController = self.viewController; [self.window makeKeyAndVisible]; dispatch_queue_t mainQueue = dispatch_get_main_queue(); self.asyncSocket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:mainQueue]; NSString *host = @"10.1.100.50"; uint16_t port = 3040; NSError *error = nil; if (![self.asyncSocket connectToHost:host onPort:port error:&amp;error]) { NSLog(@"Error connecting: %@", error); } char bytes[] = "run"; NSData* requestData = [[NSData alloc] initWithBytes:bytes length:sizeof(bytes)]; [self.asyncSocket writeData:requestData withTimeout:-1 tag:0]; return YES; } </code></pre> <p>The I tried to access the socket from multiple view controllers by invoking:</p> <pre><code>GCDAsyncSocket *asyncSocket = [[[UIApplication sharedApplication] delegate] asyncSocket]; </code></pre> <p>the code completion stops at [[UIApplication sharedApplication] delegate] without being able to suggest asyncSocket. What should I do to make asyncSocket accessible in multiple view controllers when the instance of asyncSocket is being declared in AppDelegate? Thanks!</p> <p>Here's my Xcode project file : <a href="http://bit.ly/PLe1Le" rel="nofollow">http://bit.ly/PLe1Le</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload