Newbie iOS Developer
Newbie iOS Developer

Reputation: 395

Passing Data From AppDelegate to View in iOS

I'm trying to fetch a JSON during the launch time and want to use JSON as a resource.

I make request in didFinishLaunchingWithOptions and request looking like:

NSString *urlAsString = @"myurl";
NSURL *url = [NSURL URLWithString:urlAsString];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL: url];

NSURLResponse *response = nil;
NSError *error = nil;

NSData *data = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:&error];
if ([data length] > 0 && error == nil) {
    NSString* responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    NSLog(@"Data is okay. Size: %lu and result: %@", (unsigned long)[data length], responseString);
}

It's working well, but I want to pass data from MyAppDelegate to myViewController. So I will use data with UILabel.

What exactly should I do? Is making request in AppDelegate wrong?

Any help would be great.

Upvotes: 3

Views: 380

Answers (1)

Jlexyc
Jlexyc

Reputation: 517

First: making request in app-delegate not good solution. You should make some start-up task that run on background so you will be able to show some spinner to user. Second: you can store you NSString variable in appDelegate and create getter for it (property). So you can access your App Delegate entity with

[[UIApplication sharedApplication] delegate];

Third: You can set it via UIViewController custom constructor or with property.

Upvotes: 4

Related Questions