如何将deviceToken字符串传递到'webViewDidFinishLoad'? [英] How can I pass the deviceToken string to 'webViewDidFinishLoad'?

查看:183
本文介绍了如何将deviceToken字符串传递到'webViewDidFinishLoad'?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Objective C中将数据从一个类发送到另一个类?或者也许我如何存储一个字符串到全局变量?我主要是一个JavaScript开发人员,但坚持这样做。我不能记得足够的Obj C代码自己的纸板盒。

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.

我添加了推送通知到我的PhoneGap应用程序,但我有麻烦传递令牌字符串webview。我使用Meteor,所以我在webview中调用 Session.set('token','abc'); 来存储它。当我尝试从 didRegisterForRemoteNotificationsWithDeviceToken 注入这在html页面加载完成之前触发。

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


推荐答案

让您的globalToken如下所示

Let's assign your globalToken as below

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);
}

谢谢!

这篇关于如何将deviceToken字符串传递到'webViewDidFinishLoad'?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆