【问题标题】:ios how to send refreshToken from appdelegate to viewcontrollerios如何将refreshToken从appdelegate发送到viewcontroller
【发布时间】:2017-03-24 11:21:44
【问题描述】:

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

//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.    

}

谢谢大家。

【问题讨论】:

  • 好的,所以你不是第一次获得代币价值,对吧?
  • 什么方法触发tokenRefreshCallback的通知?它看起来是异步的——所以当你第一次从你的 ViewController 调用它时,令牌还没有设置。第二次,回调被调用并保存了令牌。

标签: ios objective-c viewcontroller appdelegate


【解决方案1】:

使用 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"];
    }

 }

【讨论】:

  • 出现三个错误。首先是 (object:self,userInfo:userInfo];) 。 xcode 在 userInfo 之间说预期的']'。第二个在 ([super dealloc];) xcode 中说 ARC 禁止显式发送“dealloc”消息。最后一个是 in(String* token = userInfo[@"token"];) xcode 说将 String 修复为 NSString,因为它们,我无法运行该项目。我该怎么办?谢谢
  • 哎呀对不起伙计。我会在几分钟内更新代码
  • @anichars 请检查更新的代码,如果遇到任何问题,请发表评论。谢谢
  • 没有错误了。我在哪里可以查看 refreshToken。帮我在某处做 NSLog。谢谢朋友。
  • @anichars 您可以在视图控制器内的 receiveNotification 方法中找到令牌。 (NSString* token) 是您要使用的令牌。您可以从那里做进一步的代码,即使用令牌进行 api 调用或将其存储到服务器等...如果您找到解决方案,也接受此帖子作为答案谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-01-17
  • 2018-09-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多