0

i want to know what the possibilities are with sending a Notification. Is it possible to send a NSUserDefaults?

I know you can send another viewcontroller.

Like this:

NSUserDefaultsDidChangeNotification is just a notification that is sent out when the defaults are changed. To listen out for it you need this code :

NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center addObserver:self
           selector:@selector(defaultsChanged:)  
               name:NSUserDefaultsDidChangeNotification
             object:nil];

This will call the method defaultsChanged: when the notification is fired. You need to implement this method like this :

- (void)defaultsChanged:(NSNotification *)notification {
 // Get the user defaults
NSUserDefaults *defaults = (NSUserDefaults *)[notification object];

// Do something with it
NSLog(@"%@", [defaults objectForKey:@"nameOfThingIAmInterestedIn"]);
}
Charan
  • 4,940
  • 3
  • 26
  • 43
David Raijmakers
  • 1,369
  • 1
  • 16
  • 40

2 Answers2

2

Well,

Here is a possibility of sending a dictionary through NSNotificationCenter using

- (void)postNotificationName:(NSString *)notificationName object:(id)notificationSender userInfo:(NSDictionary *)userInfo

In the class where you are posting it:

NSDictionary *dict;

dict = [NSDictionary dictionaryWithObjectsAndKeys: yourStuff, nil];
[[NSNotificationCenter defaultCenter] postNotificationName:@”someString” object:nil userInfo:dict];

In the class doing the listening:

[[NSNotificationCenter deHaultCenter] addObserver:self selector:@selector(someMethod: ) name:@”someString” object:nil];
…
- (void)someMethod:(NSNotification *)notification {
NSDictionary *tmp = notification.userInfo;
//You could access notification.object here too
}

EDIT: But usually while receiving Push Notifications from server you have a method called:

    - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo 
{
for (id key in userInfo) {
        NSLog(@"key: %@, value: %@", key, [userInfo objectForKey:key]);
    }
}

In this method you can get the payload as a Dictionary as well

Charan
  • 4,940
  • 3
  • 26
  • 43
  • So, correct me if i'm wrong but.. I can change the "yourStuff" from the server. I can set the stuff i want to send to different mobile devices. – David Raijmakers Sep 17 '12 at 07:47
0

you can't send NSUserDefault, but you can send entire Class (Archiver/Unarchiver) data converted in base64 for example. And next create your NSUserDefault from NSData.

I wrote an article to exchange data using base64 with applications and use it.

http://www.albertopasca.it/whiletrue/2012/04/objective-c-share-classes-objects-apps/

hope this helps.

elp
  • 8,021
  • 7
  • 61
  • 120