Following a post of similar question (which doesn't work), I declared a instance of GCDAsyncSocket on AppDelegate.h

#import <UIKit/UIKit.h>

@class ViewController;
@class GCDAsyncSocket;

@interface AppDelegate : UIResponder <UIApplicationDelegate>
{
    GCDAsyncSocket *asyncSocket;

}

@property (strong, nonatomic) UIWindow *window;
@property (nonatomic, retain) GCDAsyncSocket *asyncSocket;
@property (strong, nonatomic) ViewController *viewController;

@end

and do the socket initialization in AppDelegate.m

#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:&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;
}

The I tried to access the socket from multiple view controllers by invoking:

GCDAsyncSocket *asyncSocket = [[[UIApplication sharedApplication] delegate] asyncSocket];

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!

Here's my Xcode project file : http://bit.ly/PLe1Le

有帮助吗?

解决方案

You are on the right track. And the application delegate is a great place for a socket connection. I think you're being tripped up by something relatively simple.

[[UIApplication sharedApplication] delegate] returns an id or generic object pointer to an object that conforms to the <UIApplicationDelegate> protocol. So code completion has no way of knowing that your application's delegate is an instance of your AppDelegate class.

Remember if you are in fact using an instance of AppDelegate to be your application's delegate then [[UIApplication sharedApplication] delegate] will return a pointer to your delegate, but it will be the generic pointer discussed above.

The simplest solution is to cast the pointer you receive back from [[UIApplication sharedApplication] delegate] to be a pointer of AppDelegate type.

For example:

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
// We now have a pointer to your app delegate that the compiler knows is an AppDelegate.
// So code completion will work and it will compile.
GCDAsyncSocket *socket = [myAppDelegate asyncSocket];

Or you can stack the calls to one statement. The syntax looks a little funky, but this is how it's done.

GCDAsyncSocket *socket = [(AppDelegate *)[[UIApplication sharedApplication] delegate] asyncSocket];
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top