简体   繁体   English

如何将NSDictionary解析为NSUserDefaults IOS7

[英]how to parse NSDictionary into NSUserDefaults IOS7


In my application i want to display the remote notification in UILabel . 在我的应用程序中,我想在UILabel显示远程通知。 So I'm trying pass the notification message form the "Appdelegate" to my storyboard its not working please tell me how to resolve this one. 因此,我尝试将通知消息形式的“ Appdelegate”传递到我的情节提要中,但该方法不起作用,请告诉我如何解决此问题。

My "Appdelegate" code: 我的“ 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];
 }

When i print my data its coming like: 当我打印数据时,它会像:

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

in my viewcontroller i have used like this. 在我的视图控制器中,我已经这样使用过。

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

In the above data i need only the "Alert" value how to get the alert value and store into the data please tell me is that possible to do and how to achieve this one. 在以上数据中,我仅需要“ Alert”值,如何获取警报值并将其存储到数据中,请告诉我这是可能的以及如何实现这一目标。 I'm stack here for long not please give some ideas to achieve this one. 我在这里待了很长时间,请不要提出一些想法来实现这一目标。

Thanks. 谢谢。

The description method creates a string representation of an object for logging purposes, but you wouldn't use it in that way to access the contents of the object. description方法创建对象的字符串表示形式以进行记录,但是您不会以这种方式使用它来访问对象的内容。

From the Apple documentation - 从Apple文档-

The userInfo dictionary contains the aps key whose value is another dictionary. userInfo词典包含aps键,其值是另一个词典。 Although you should not need the information in the aps dictionary, you can retrieve its contents using the following keys: 尽管您不需要aps词典中的信息,但是可以使用以下键检索其内容:

alert —The value is either a string for the alert message or a dictionary with two keys: body and show-view. 提醒 -The值是为该警报消息的字符串或具有两个键的字典:主体和显示图。 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. 如果show-view键的值为false,则不会显示警报的“查看”按钮。 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. 如果没有badge属性,则表明应该删除当前用于标识图标的任何数字。

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. 如果指定“默认”,则应播放默认声音。

So, you can use the code from this answer - 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"];
}

Hi try using this code 嗨,尝试使用此代码

NSDictionary *dict=userInfo; NSDictionary * dict = userInfo;

NSString *messag=[[[dict objectForKey:@"message"]objectForKey:@"aps"]objectForKey:@"alert"]; NSString * messag = [[[dict objectForKey:@“ message”] objectForKey:@“ aps”] objectForKey:@“ alert”];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM