Reputation: 1834
In my application , in the appDelegate file , i register fur push notifications take the token and also get the device UDID. I save these variables as global variables.
The next thing i do , is in my viewController connect to a URL using these 2 variables.
However , when from my viewController i call the variables they are (null).
So i thought maybe i was doing something wrong with them. But even when i nslog something on appdelegate() and viewcontroller() , it nslogs first from the viewcontroller...
How is this possible , as what i new is that appdelegate runs first , and secondly how will i be able to use the token and udid variables that i get on the appdelegate , in the viewcontroller?
My code looks like this:
In appdelegate.h just before the @interface
:
extern NSString *newDeviceToken;
extern NSString *udid;
In appdelegate.m before the @implementation
:
NSString *newDeviceToken;
NSString *udid;
An in my view controller i simply import the delegate:
#import "AppDelegate.h"
Upvotes: 1
Views: 897
Reputation: 14687
The delegate method:
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
where you should get your variables, is run asynchronously. And yes, it might get called after your RootViewController has been initialized.
You shouldn't perform the URL connection on your ViewController, as you might not yet have an access token. You should perform this on the delegate method it self.
Now if you want to use a function of your ViewController and don't want to do the server communication in the AppDelegate, get a reference to your ViewController in your AppDelegate, make the URL function on your ViewController public, and call it.
As for getting a reference to your ViewController, this will be hard to answer, without knowing your App's design.
Assuming your App's RootViewController is a NavigationController and let's say the NavigationController RootViewController is named HomeNewViewController, this is how you would get a pointer reference to it from the App Delegate:
NavigationController *navigationController = (NavigationController*) [self.window rootViewController];
HomeNewViewController *homeController = (HomeNewViewController*)[navigationController.viewControllers objectAtIndex:0];
Then let's say in the HomeNewViewController, you have a function called uploadToken.
You could then do:
[homeController uploadToken];
and upload it.
Upvotes: 1
Reputation: 130172
application:didRegisterForRemoteNotificationsWithDeviceToken
is not called immediately after calling [UIApplication registerForRemoteNotificationTypes:]
.
That means you can expect to be having the token at the time your views are being loaded. Separate your UI from the connection code. Your connection must be triggered when application:didRegisterForRemoteNotificationsWithDeviceToken
is called, not at some point of your view controller life cycle. Note that there can be also a dialog asking user for approval before application:didRegisterForRemoteNotificationsWithDeviceToken
is called.
Upvotes: 1