SkinnyGeek1010
SkinnyGeek1010

Reputation: 536

How can I pass the deviceToken string to 'webViewDidFinishLoad'?

How do I send data from one class to another in Objective C? Or perhaps how do I store a string into a global variable? I'm primarily a JavaScript developer but got stuck doing this. I can't remember enough Obj C to code myself of of a cardboard box.

I added push notifications to my PhoneGap app but I am having trouble passing the token string to the webview. I'm using Meteor so I an call Session.set('token', 'abc'); in the webview to store it. When I try and inject this from didRegisterForRemoteNotificationsWithDeviceToken it fires before the html page is finished loading. Any help is much appreciated.

- (void)application:(UIApplication*)application  didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
{
    NSLog(@"My token is: %@", deviceToken);
    // would like to do:
    globalToken = deviceToken;
}

.

- (void)webViewDidFinishLoad:(UIWebView*)theWebView
{
    // Black base color for background matches the native apps
    theWebView.backgroundColor = [UIColor blackColor];

     // inject token
     NSString* jsString = [NSString stringWithFormat:@"setTimeout(function(){ Session.set('push:ios', '%@'); }, 7000);", globalToken];
    [theWebView stringByEvaluatingJavaScriptFromString:jsString];

    return [super webViewDidFinishLoad:theWebView];

Upvotes: 0

Views: 723

Answers (1)

Natarajan
Natarajan

Reputation: 3271

Let's assign your globalToken as below

in AppDelegate.h

@property (nonatomic, retain) NSString * globalToken;

in AppDelegate.m

@synthesize globalToken = _globalToken;

- (void)application:(UIApplication*)application  didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
{
    NSLog(@"My token is: %@", deviceToken);
    // would like to do:
    _globalToken = deviceToken;
}

In Your webViewDidFinishLoad

#import "AppDelegate.h"

-(void)webViewDidFinishLoad:(UIWebView*)theWebView
{    
    AppDelegate * appDel = (AppDelegate *) [[UIApplication sharedApplication] delegate];

    NSLog(@"appDel.globalToken :%@", appDel.globalToken);
}

Thanks!

Upvotes: 1

Related Questions