Bronze Challenge Help


#1

Hi All,

I’m stumped trying to implement the bronze challenge on my iPhone 4S. I believe my codes are alright but suspect that the notifications are not firing. Here’s the code in my AppDelegate class:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
  
    UIDevice *device = [UIDevice currentDevice];
    NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
    
    HeavyViewController *hvc = [[HeavyViewController alloc] init];
    self.window.rootViewController = hvc;
    
    [device setProximityMonitoringEnabled:YES];
    
    [nc addObserver:hvc 
           selector:@selector(sensorStateChange:) 
               name:UIDeviceProximityStateDidChangeNotification
             object:nil];
    
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}

- (void) sensorStateChange: (NSNotification *) note {
    NSLog(@"Hello");
}

I’m expecting “Hello” to be printed out everything there’s a change in the proximity state, but there is no output to the console at all (no matter how I close I plaster my face/ hands on the device)! According to Apple’s documentation, I should test UIDevice’s proximityState property after enabling the proximity monitoring, but I find that it’s always returning NO on my iPhone. I would appreciate if someone could enlighten me on where perhaps I have gone wrong?

Thanks in advance.


#2

The object whose notifications the observer wants to receive:

object:nil];is set to “nil”. This parameter should be changed to “device”, which refers to UIDevice currentDevice instance. The device is the one who tells you about its proximity state.

The observer of the proximity changed notification “hvc”, refers to an instance of HeavyViewController. Your sensorStateChanged: method should be defined in HeavyViewController.m rather than RotationAppDelegate.m. Hopefully these changes should enable you to see proximity change notifications in the console.


#3

Blame it on my bad copying. :blush: The method sensorStateChange is in my view controller.

Setting the object to device or nil does not matter in this case since I’m not querying the proximity state yet. I’m having problem just trying to log the occurrence of notification events. :confused:


#4

I tried deleting my entire code and replaced it with yours and I’m still getting notifications. For comparison, here is the entire source code for all four files of the working program:
RotationAppDelegate.h

#import <UIKit/UIKit.h> @interface RotationAppDelegate : UIResponder <UIApplicationDelegate> @property (strong, nonatomic) UIWindow *window; @endRotationAppDelegate.m

[code]#import “RotationAppDelegate.h”
#import “HeavyViewController.h”
@implementation RotationAppDelegate
@synthesize window = _window;

  • (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    UIDevice *device = [UIDevice currentDevice];
    NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
    HeavyViewController *hvc = [[HeavyViewController alloc] init];
    self.window.rootViewController = hvc;
    [device setProximityMonitoringEnabled:YES];
    [nc addObserver:hvc
    selector:@selector(sensorStateChange:)
    name:UIDeviceProximityStateDidChangeNotification
    object:nil];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
    }
    @end[/code]HeavyViewController.h

#import <UIKit/UIKit.h> @interface HeavyViewController : UIViewController @endHeavyViewController.m

[code]#import “HeavyViewController.h”
@implementation HeavyViewController

  • (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)x
    {
    return (x == UIInterfaceOrientationPortrait) || UIInterfaceOrientationIsLandscape(x);
    }
  • (void)sensorStateChange:(NSNotification *)note
    {
    NSLog(@“hello”);
    }
    @end[/code]
    If that doesn’t work, you can also download source files from bignerdranch.com/solutions/i … ing3ed.zip
    The solutions folder has the source code for each chapter (minus the challenges).

#5

Thanks Bryan for the assist. Everything works fine now after I rebooted and retried again. Cheers!


#6

So is there any way of testing this without an iphone? :stuck_out_tongue: