2017-03-24 6 views
0

저는 IOS 앱을 개발하는 초보자입니다. 이제 AppDelegate의 메소드에서 mainTextController로 refreshToken 데이터를 보내려고합니다. 다운로드 할 때 처음으로 가져올 수 없습니다. 그러나 두 번째로 나는 그것을 얻을 수있다. 처음 생성 할 때 약간의 시간이 걸리고 나중에 저장된다고 생각합니다. AppDelegate에서 ViewController로 메서드에서 데이터를 보내는 방법을 알려주십시오. 궁극적으로, 나는 그것을 데이터베이스에 저장하고 서버 측에서 사용자에게 푸시 알림 메시지를 보내려고합니다. 다음 코드를 사용합니다.어떻게 refreshTooken을 viewcontroller에서 appdelegate로 보내야합니까?

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

(토큰을 사용할 수있는 응용 프로그램의 위임에 다음 코드를 추가) 그리고 당신의 기본보기에이 코드를 넣어 컨트롤러

- (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'의 명시 적 메시지 전송을 금지한다고 말합니다. 마지막 하나가 (String * token = userInfo [@ "token"];) 에 있습니다. xcode가 NSString에 문자열을 수정합니다. 그것 때문에 프로젝트를 실행할 수 없습니다. 어떻게하면됩니까? 감사합니다 – anichars

+0

죄송합니다. 몇 분 안에 코드를 업데이트하겠습니다. –

+0

@anichars 업데이트 된 코드를 확인하고 문제가 있으면 의견을 말하십시오. thanks –