如何将deviceToken字符串传递给'webViewDidFinishLoad'?

时间:2014-03-22 09:17:44

标签: ios objective-c cordova

如何在Objective C中将数据从一个类发送到另一个类?或者如何将字符串存储到全局变量中?我主要是一名JavaScript开发人员,但却因此而陷入困境。我无法记住足够的Obj C来编写纸箱的代码。

我向我的PhoneGap应用添加了推送通知,但是我无法将令牌字符串传递给webview。我正在使用Meteor,所以我在webview中调用Session.set('token', 'abc');来存储它。当我尝试从didRegisterForRemoteNotificationsWithDeviceToken注入此内容时,它会在html页面加载完成之前触发。任何帮助很多赞赏。

- (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];

1 个答案:

答案 0 :(得分:1)

让我们将您的globalToken分配如下

AppDelegate.h

中的

@property (nonatomic, retain) NSString * globalToken;
AppDelegate.m

中的

@synthesize globalToken = _globalToken;

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

webViewDidFinishLoad

#import "AppDelegate.h"

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

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

谢谢!