2017-03-24 49 views
0

我是開發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

回答

0

使用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請檢查更新後的代碼,如果您遇到任何問題,請發表評論。謝謝 –

相關問題