Bronze Challenge: no idea why it doesn't work


#1

First, my code. I’ve even removed anything involving changing the screen colors, and simply trying to post a notification, but to no avail.

HeavyViewController.h

[code]#import <UIKit/UIKit.h>

@interface HeavyViewController : UIViewController

@end[/code]

HeavyViewController.m

[code]@implementation HeavyViewController

  • (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)x {
    return (x == UIInterfaceOrientationPortrait) || UIInterfaceOrientationIsLandscape(x);
    }

  • (void)sensorStateChange:(NSNotification *)note {
    if ([[UIDevice currentDevice] proximityState] == YES) {
    NSLog(@“Close”);
    } else {
    NSLog(@“Far”);
    }
    NSLog(@“End of sensor state change notification”);
    }
    @end[/code]

RotationAppDelegate.h

[code]#import <UIKit/UIKit.h>

@interface RotationAppDelegate : UIResponder

@property (strong, nonatomic) UIWindow *window;

@end[/code]

RotationAppDelegate.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]];
    // Override point for customization after application launch.

    // Get the device object
    UIDevice *device = [UIDevice currentDevice];

    // Tell it to start monitoring the accelerometer for orientation
    [device beginGeneratingDeviceOrientationNotifications];

    // Get the notification center for the app
    NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];

    // Add yourself as an observer
    [nc addObserver:self
    selector:@selector(orientationChanged:)
    name:UIDeviceOrientationDidChangeNotification
    object:device];

    // Now, set the device to enable proximity monitoring
    [device setProximityMonitoringEnabled:YES];
    NSLog(@“Proximity monitoring enabled? %@”, [device isProximityMonitoringEnabled] ? @“YES” : @“NO”);

    HeavyViewController *hvc = [[HeavyViewController alloc] init];
    [nc addObserver:hvc
    selector:@selector(sensorStateChange:)
    name:UIDeviceProximityStateDidChangeNotification
    object:device];
    [[self window] setRootViewController:hvc];

    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
    }

  • (void)orientationChanged:(NSNotification *)note {
    // Log the constant that represents the current orientation
    NSLog(@“orientationChanged: %d”, [[note object] orientation]);
    }

@end[/code]

I’ve tried simply copy-pasting other poster’s successful solutions and running them, and it still gets nothing. Is there a chance my physical proximity sensor is broken? Or is it some really obvious thing I’m missing in the code.

Any and all help appreciated!


#2

Just ran your code on my phone- it works fine. You’re not trying in landscape mode, are you? See http://forums.bignerdranch.com/viewtopic.php?f=220&t=4777

You can also test your proximity sensor by making a phone call to yourself. Placing your hand over the earpiece should dim the screen during an active call. If not, it’s broken.

Finally, this should be obvious but only iPhones have proximity sensors (not iPod Touch nor iPads). Good luck!


#3

… that did it. Thank you so much!


#4

“You’re not trying in landscape mode, are you?”

This was my problem. This is after the part of the chapter where you force landscape only, and I didn’t realize the sensor did not work in landscape setting. Thanks!