ios如何从appdelegate发送refreshToken到视图控制器

问题描述:

我是开发IOS应用程序的初学者。现在我想从AppDelegate中的方法向mainViewController发送一个refreshToken数据。 下载它时,我无法第一时间得到它。但第二次,我可以得到它。 我认为它需要一些时间,它创建第一次,它被存储后。 请帮助我,如何将数据从AppDelegate发送到ViewController。最终,我想将它存储到数据库并从服务器端向用户发送推送通知消息。 我使用下面的代码。ios如何从appdelegate发送refreshToken到视图控制器

//AppDelegate.h

@class ViewController; 
#import <UIKit/UIKit.h> 
#import "ViewController.h" 
@interface AppDelegate : UIResponder <UIApplicationDelegate> 
@property (weak, nonatomic) ViewController *myViewController; 
@property (strong, nonatomic) UIWindow *window; 
@property (weak, nonatomic) IBOutlet UIWebView *webView;  
@end 

//AppDelegate.m

- (void)tokenRefreshCallback: (NSNotification *)notification { 
NSString *refreshToken = [[FIRInstanceID instanceID] token]; 
NSLog(@"InstanceID token: %@", refreshToken); 

// 
if(refreshToken){ 
    [[NSUserDefaults standardUserDefaults] setObject:refreshToken forKey:@"token"]; 
    [[NSUserDefaults standardUserDefaults] synchronize]; 
} 
// Connect to FCM since connection may have failed when attempted before having a token. 

[self connectToFirebase]; 

} 

//ViewController.h

#import <UIKit/UIKit.h> 
#import "AppDelegate.h" 
@interface ViewController : UIViewController 

@end 

//ViewController.m

- (void)viewDidLoad { 
[super viewDidLoad]; 

AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate]; 
appDelegate.myViewController = self; 

//get tokenId 
NSString *token = [[NSUserDefaults standardUserDefaults] objectForKey:@"token"]; 
if(token){ 
    NSLog(@"i have token~!!!: %@ ", token); 
}else NSLog(@"no token!: %@ ", token); 
// Do any additional setup after loading the view, typically from a nib.  

} 

谢谢大家。

+0

好吧,所以你第一次没有得到令牌值,对吧? –

+0

触发'tokenRefreshCallback'通知的方法是什么?它看起来是异步的 - 所以你第一次从你的ViewController调用它时,令牌还没有被设置。第二次,回调被调用并且令牌被保存。 – dmorrow

使用NSNotifiactionCenter

在当令牌可用后使用令牌通知您的appdelegate

NSDictionary* userInfo = @{@"token": @"YOUR_TOKEN"}; 

[[NSNotificationCenter defaultCenter] postNotificationName:@"refreshTokenAvailale" object:self userInfo:userInfo]; 

(在应用程序委托其中token可以添加以下代码),并把这个代码在你的主视图控制器

- (void) dealloc 
{ 
// If you don't remove yourself as an observer, the Notification Center 
// will continue to try and send notification objects to the deallocated 
// object. 
    [[NSNotificationCenter defaultCenter] removeObserver:self]; 

} 

- (id) init 
{ 

    [[NSNotificationCenter defaultCenter] addObserver:self 
    selector:@selector(receiveNotification:) 
    name:@"refreshTokenAvailale" 
    object:nil]; 

    return self; 
} 

- (void) receiveNotification:(NSNotification *) notification 
{ 

    if ([notification.name isEqualToString:@"refreshTokenAvailale"]) 
    { 
     NSDictionary* userInfo = notification.userInfo; 
     NSString* token = userInfo[@"token"]; 
    } 

} 
+0

有三个错误。 首先在(object:self,userInfo:userInfo];)。 xcode表示在userInfo之间预期']'。 第二个是在[[super dealloc];) xcode说ARC禁止'dealloc'的显式消息发送。 最后一个是在(字符串*标记=用户信息[@“标记”];) xcode说固定字符串到NSString 我无法运行项目,因为他们。 我怎么办?谢谢 – anichars

+0

哎呀对不起队友。我会在几分钟内更新代码 –

+0

@anichars请检查更新后的代码,如果您遇到任何问题,请发表评论。谢谢 –