Search code examples
iphoneobjective-cnsnotifications

Objective C - How to access NSNotification inner objects?


I want to access a inner object from a notification. I need to get the key AVSystemController_AudioVolumeChangeReasonNotificationParameter.

If I call [sender object] I get this output. But if I call

[[sender object] userInfo]

I only get "unrecognized selector sent to instance". Why is that?

NSLog(@"%@", [sender object]) output:

NSConcreteNotification 0x157a30 {name = AVSystemController_SystemVolumeDidChangeNotification; object = AVSystemController: 0x1616c0; userInfo = {
    "AVSystemController_AudioCategoryNotificationParameter" = "Audio/Video";
    "AVSystemController_AudioVolumeChangeReasonNotificationParameter" = ExplicitVolumeChange;
    "AVSystemController_AudioVolumeNotificationParameter" = 1;
}}

If it is not possible to access userInfo, can I get the output of NSLog to do a string search?


Solution

  • Your output from NSLog actually looks like the output of

    NSLog(@"%@", sender);
    

    Are you sure you were calling [sender object]?

    object is often the Object that posted the notification. In your case most likely an object with class AVSystemController

    The userinfo can be accessed with

    [sender userInfo]
    

    So please try

    NSLog(@"%@", [sender userInfo]);
    

    BTW: If you try to map the function of the volume-buttons to some other function (for example "Take Photo"-Action) your app won't be approved. Just in case.