gpt4 book ai didi

ios - 如何将 NSDictionary 解析为 NSUserDefaults IOS7

转载 作者:行者123 更新时间:2023-11-28 22:06:26 25 4
gpt4 key购买 nike


在我的应用程序中,我想在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com