The distance sensor on the iphone is turned on Via Uidevice, and the system is notified by notification.
① Open distance sensor, monitoring notification:
-(void) viewdidload { [super viewdidload]; Turn on the sensor after iOS3 via Uidevice's currentdevice proximitymonitoringenabled settings [Uidevice Currentdevice]. proximitymonitoringenabled = YES; When listening to objects approaching, the system notifies [[Nsnotificationcenter defaultcenter] addobserver:self selector: @selector ( Proximitystatechange:) name:uideviceproximitystatedidchangenotification Object:nil]; }
② Implementation Notification Method: When the object is close to the distance sensor, the phone will automatically lock the screen, and proximitystate is yes, otherwise no.
-(void) Proximitystatechange: (nsnotification *) nof{ NSLog (@ "%d", [Uidevice currentdevice].proximitystate); if ([Uidevice currentdevice].proximitystate) { NSLog (@ "near"); } else{ NSLog (@ "Leave"); } }
To remove a notification when ③dealloc:
-(void) dealloc{ [[Nsnotificationcenter Defaultcenter] removeobserver:self]; }
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
(91) Use of distance sensor