Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8533839
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:03:32+00:00 2026-06-11T10:03:32+00:00

Following a post of similar question (which doesn’t work), I declared a instance of

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T10:03:33+00:00Added an answer on June 11, 2026 at 10:03 am

    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];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My question is similar to this following post Render a view of another controller
Simple question. I have the following HTML: <a rel=nofollow id=claim-job-link data-method=post class=button green claim-job
I have almost the exact same question as at the following post (and I
I have a question regarding checkboxes. <form method=post> I speak the following languages: <input
I have a WP7 game that has symptoms similar to the following post: WP7
Someone asked a similar question before , getting the following error when I run
Following on from this question about setting Culture, I have a similar issue with
I've read the following post. My Code looks exactly the same, but does not
I have the following POST request: POST http://blah/Request HTTP/1.1 Host: blah Content-Length: 322 Proxy-Connection:
I have been following the following post on using multiple ItemTemplates in a ListView

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.