ios - 如何将 NSDictionary 解析为 NSUserDefaults IOS7

标签 ios objective-c nsdictionary


在我的应用程序中,我想在 UILabel 中显示远程通知。所以我正在尝试将通知消息从“Appdelegate”传递到我的 Storyboard,它不起作用请告诉我如何解决这个问题。

我的“Appdelegate”代码:

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{

  NSString *messag = [[userInfo description] stringByTrimmingCharactersInSet: [NSCharacterSet characterSetWithCharactersInString:@"<>"]];

 [[NSUserDefaults standardUserDefaults] setObject: messag forKey:@"message"];
 [[NSUserDefaults standardUserDefaults]synchronize];
 NSLog (@"message %@", [[NSUserDefaults standardUserDefaults] objectForKey:@"message"]);
 UIStoryboard *mainstoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
 personalnottypoliticalViewController *ringingVC = [mainstoryboard instantiateViewControllerWithIdentifier:@"notifymess"];

 [self.window setRootViewController: ringingVC];
 }

当我打印我的数据时:

message {
aps =     {
    alert = home;
    badge = 3;
    sound = "";
 };
}

在我的 viewcontroller 中我是这样使用的。

self.message.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"message"];
  NSLog (@"message %@", [[NSUserDefaults standardUserDefaults] objectForKey:@"message"]);

在上面的数据中,我只需要“警报”值如何获取警报值并将其存储到数据中,请告诉我是否可以这样做以及如何实现这一点。我在这里呆了很长时间,请提供一些实现这一目标的想法。

谢谢。

最佳答案

description 方法创建对象的字符串表示形式用于记录目的,但您不会以这种方式使用它来访问对象的内容。

来自 Apple 文档 -

The userInfo dictionary contains the aps key whose value is another dictionary. Although you should not need the information in the aps dictionary, you can retrieve its contents using the following keys:

alert—The value is either a string for the alert message or a dictionary with two keys: body and show-view. The value of the body key is a string containing the alert message and the value of the show-view key is a Boolean. If the value of the show-view key is false, the alert’s View button is not shown. The default is to show the View button which, if the user taps it, launches the app.

badge —A number indicating the quantity of data items to download from the provider. This number is to be displayed on the app icon. The absence of a badge property indicates that any number currently badging the icon should be removed.

sound —The name of a sound file in the app bundle to play as an alert sound. If “default” is specified, the default sound should be played.

因此,您可以使用此答案中的代码 - How to handle push notifications if the application is already running?

NSDictionary *aps=(NSDictionary *)[userInfo objectForKey:@"aps"];

NSString *message;

id alert = [aps objectForKey:@"alert"];
if ([alert isKindOfClass:[NSString class]]) {
   message = alert;
} else if ([alert isKindOfClass:[NSDictionary class]]) {
   message = [alert objectForKey:@"body"];
}

关于ios - 如何将 NSDictionary 解析为 NSUserDefaults IOS7,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23869013/

相关文章:

ios - 如何在 iPhone 锁屏中设置当前正在播放的音频的标题?

iphone - 制作一个跨 View Controller 重复的函数 DRY

objective-c - 将图像保存到核心数据

objective-c - [NSMutableDictionary dictionaryWithDictionary :] and mutableCopy? 之间有区别吗

ios - 使用委托(delegate)将数据传回 VC 时属性为 nil

ios - BackBarButton 不会显示正确的标题

ios - 来自 Apple 的电子邮件 - "Missing Push Notification Entitlement"

ios - 不支持的配置 : This file is set to build for a version older than the deployment target. 功能可能受限

ios - 如何将数据关联到 UITableView 中的特定行

cocoa - 将 NSArrayController 绑定(bind)到 NSDictionary allValues