IOS Uidevice distance sensor (turn Off)

Source: Internet
Author: User
<span id="Label3"></p><span style="font-size: 18.000000pt; font-family: "STSongti-SC-Regular"; color: rgb(34.901960%, 34.901960%, 34.901960%)"><span style="font-size: 18.000000pt; font-family: "STSongti-SC-Regular"; color: rgb(34.901960%, 34.901960%, 34.901960%)"><span style="font-size: 18.000000pt; font-family: "STSongti-SC-Regular"; color: rgb(34.901960%, 34.901960%, 34.901960%)">What is a sensor?</span></span></span> <span style="font-size: 13.000000pt; font-family: "LucidaGrande"; color: rgb(30.980390%, 50.588240%, 74.117650%)"><span style="font-size: 13.000000pt; font-family: "LucidaGrande"; color: rgb(30.980390%, 50.588240%, 74.117650%)"> <span style="font-size: 18.000000pt; font-family: "STSongti-SC-Regular"; color: rgb(34.901960%, 34.901960%, 34.901960%)">Sensor is a sensing <span style="font-size: 18.000000pt; font-family: "Times"; color: rgb(34.901960%, 34.901960%, 34.901960%)">\ <span style="font-size: 18.000000pt; font-family: "STSongti-SC-Regular"; color: rgb(34.901960%, 34.901960%, 34.901960%)">detection device <span style="font-size: 18.000000pt; font-family: "Times"; color: rgb(34.901960%, 34.901960%, 34.901960%)">, which is <span style="font-size: 18.000000pt; font-family: "STSongti-SC-Regular"; color: rgb(34.901960%, 34.901960%, 34.901960%)">widely used in Smartphones.</span> </span> </span> </span> </span> </span></span><p><p></p></p> <span style="font-size: 18.000000pt; font-family: "Times"; color: rgb(34.901960%, 34.901960%, 34.901960%)"><span style="font-size: 18.000000pt; font-family: "Times"; color: rgb(34.901960%, 34.901960%, 34.901960%)"><span style="font-size: 18.000000pt; font-family: "Times"; color: rgb(34.901960%, 34.901960%, 34.901960%)"> <span style="font-size: 18.000000pt; font-family: "STSongti-SC-Regular"; color: rgb(34.901960%, 34.901960%, 34.901960%)">The sensors built into the iPhone5 are</span> </span></span></span><p><span style="font-size: 13.000000pt; font-family: "LucidaGrande"; color: rgb(30.980390%, 50.588240%, 74.117650%)"><span style="font-size: 18.000000pt; font-family: "STSongti-SC-Regular"; color: rgb(100.000000%, 0.000000%, 0.000000%)">Motion sensor<span style="font-size: 18.000000pt; font-family: "Times"; color: rgb(100.000000%, 0.000000%, 0.000000%)"><span style="font-size:18.000000pt; font-family:" times"; color:rgb (100%, 0%, 0%)">\ <span style="font-size:18.000000pt; Font-family: " stsongti-sc-regular"; color:rgb (100%, 0%, 0%) "> acceleration sensor <span style=" font-size:18.000000pt; font-family: " times "; color:rgb (100.000000%, 0.000000%, 0.000000%) ">\<span style=" font-size:18.000000pt; font-family: " stsongti-sc-regular"; color:rgb (100%, 0%, 0%) "> Accelerometer <span style=" font-size:18.000000pt; font-family: " stsongti-sc-regular"; color:rgb (34.901960%, 34.901960%, 34.901960%) "> (<span style=" font-size:18.000000pt; font-family: " times"; color:rgb (100.000000%, 0.000000%, 0.000000%) ">motion/accelerometer sensor<span style=" font-size:18.000000pt; Font-family: " stsongti-sc-regular"; color:rgb (34.901960%, 34.901960%, 34.901960%) ">) </span> </span> </span> </span> </span> </span> </span></span></span></span></p><p><p> <span style="font-size:13.000000pt; font-family:" lucidagrande"; color:rgb (30.98039%, 50.58824%, 74.11765%) ">  <span style=" font-size:18.000000pt; Font-family: " stsongti-sc-regular"; color:rgb (34.90196%, 34.90196%, 34.90196%) "> ambient light sensor (<span style=" font-size:18.000000pt; font-family: " times& quot;; color:rgb (34.901960%, 34.901960%, 34.901960%) ">ambient light sensor<span style=" font-size:18.000000pt; font-family: & quot stsongti-sc-regular" ; color:rgb (34.901960%, 34.901960%, 34.901960%) ">) </span> </span> </span> </span> </p></p><p><p> <span style="font-size:13.000000pt; font-family:" lucidagrande"; color:rgb (30.98039%, 50.58824%, 74.11765%) ">  <span style=" font-size:18.000000pt; Font-family: " stsongti-sc-regular"; color:rgb (34.90196%, 34.90196%, 34.90196%) "> distance sensor (<span style=" font-size:18.000000pt; font-family: " times "; color:rgb (34.901960%, 34.901960%, 34.901960%) ">proximity sensor<span style=" font-size:18.000000pt; font-family: " stsongti-sc-regular"; color:rgb (34.901960%, 34.901960%, 34.901960%) ">) </span> </span> </span> </span> </p></p><p><p> <span style="font-size:13.000000pt; font-family:" lucidagrande"; color:rgb (30.98039%, 50.58824%, 74.11765%) ">  <span style=" font-size:18.000000pt; Font-family: " stsongti-sc-regular"; color:rgb (34.90196%, 34.90196%, 34.90196%) "> magnetometer sensor (<span style=" font-size:18.000000pt; font-family: " times& quot;; color:rgb (34.901960%, 34.901960%, 34.901960%) ">magnetometer sensor<span style=" font-size:18.000000pt; font-family: & quot stsongti-sc-regular" ; color:rgb (34.901960%, 34.901960%, 34.901960%) ">) </span> </span> </span> </span> </p></p><p><p> <span style="font-size:13.000000pt; font-family:" lucidagrande"; color:rgb (30.98039%, 50.58824%, 74.11765%) ">  <span style=" font-size:18.000000pt; Font-family: " stsongti-sc-regular"; color:rgb (34.90196%, 34.90196%, 34.90196%) "> internal temperature sensor (<span style=" font-size:18.000000pt; font-family: " times "; color:rgb (34.901960%, 34.901960%, 34.901960%) ">internal temperature Sensor<span style=" font-size:18.000000pt; Font-family: " stsongti-sc-regular"; color:rgb (34.901960%, 34.901960%, 34.901960%) ">) </span> </span> </span> </span> </p></p><p><p> <span style="font-size:13.000000pt; font-family:" lucidagrande"; color:rgb (30.98039%, 50.58824%, 74.11765%) ">  <span style=" font-size:18.000000pt; Font-family: " stsongti-sc-regular"; color:rgb (34.90196%, 34.90196%, 34.90196%) "> humidity sensor (<span style=" font-size:18.000000pt; font-family: " times "; color:rgb (34.901960%, 34.901960%, 34.901960%) ">moisture sensor<span style=" font-size:18.000000pt; font-family: " stsongti-sc-regular"; color:rgb (34.901960%, 34.901960%, 34.901960%) ">) </span> </span> </span> </span> </p></p><p><p><span style="font-size: 13.000000pt; font-family: "LucidaGrande"; color: rgb(30.980390%, 50.588240%, 74.117650%)"><span style="font-size: 18.000000pt; font-family: "STSongti-SC-Regular"; color: rgb(34.901960%, 34.901960%, 34.901960%)">Gyroscope (<span style="font-size: 18.000000pt; font-family: "Times"; color: rgb(34.901960%, 34.901960%, 34.901960%)">Gyroscope<span style="font-size: 18.000000pt; font-family: "STSongti-SC-Regular"; color: rgb(34.901960%, 34.901960%, 34.901960%)">)</span> </span> </span></span></p></p><p><p></p></p><p><p></p></p>Sensor summary<p><p></p></p><p><p></p></p><span style="font-size: 18pt"><span style="font-size: 18pt">use of distance sensors:</span></span><p><p></p></p>//<span style="font-size: 14.000000pt; font-family: "STSongti-SC-Regular"; color: rgb(0.000000%, 45.490200%, 0.000000%)"><span style="font-size: 14.000000pt; font-family: "STSongti-SC-Regular"; color: rgb(0.000000%, 45.490200%, 0.000000%)">Open Range Sensing function<br><span style="font-size: 14.000000pt; font-family: "Menlo"">[<span style="font-size: 14.000000pt; font-family: "Menlo"; color: rgb(36.078430%, 14.901960%, 60.000000%)">Uidevice<span style="font-size: 14.000000pt; font-family: "Menlo"; color: rgb(18.039220%, 5.098039%, 43.137250%)">Currentdevice<span style="font-size: 14.000000pt; font-family: "Menlo"">].<span style="font-size: 14.000000pt; font-family: "Menlo"; color: rgb(36.078430%, 14.901960%, 60.000000%)">Proximitymonitoringenabled<span style="font-size: 14.000000pt; font-family: "Menlo"">=<span style="font-size: 14.000000pt; font-family: "Menlo"; color: rgb(66.666670%, 5.098039%, 56.862750%)">YES<span style="font-size: 14.000000pt; font-family: "Menlo"">;<span style="font-size: 14.000000pt; font-family: "Menlo"; color: rgb(0.000000%, 45.490200%, 0.000000%)">//<span style="font-size: 14.000000pt; font-family: "STSongti-SC-Regular"; color: rgb(0.000000%, 45.490200%, 0.000000%)">Notification of listening distance sensing<br><span style="font-size: 14.000000pt; font-family: "Menlo"">[[<span style="font-size: 14.000000pt; font-family: "Menlo"; color: rgb(36.078430%, 14.901960%, 60.000000%)">Nsnotificationcenter<span style="font-size: 14.000000pt; font-family: "Menlo"; color: rgb(18.039220%, 5.098039%, 43.137250%)">Defaultcenter<span style="font-size: 14.000000pt; font-family: "Menlo"">]<span style="font-size: 14.000000pt; font-family: "Menlo"; color: rgb(18.039220%, 5.098039%, 43.137250%)">Addobserver<span style="font-size: 14.000000pt; font-family: "Menlo"">:<span style="font-size: 14.000000pt; font-family: "Menlo"; color: rgb(66.666670%, 5.098039%, 56.862750%)">Self<span style="font-size: 14.000000pt; font-family: "Menlo"; color: rgb(18.039220%, 5.098039%, 43.137250%)">Selector<span style="font-size: 14.000000pt; font-family: "Menlo"">:<span style="font-size: 14.000000pt; font-family: "Menlo"; color: rgb(66.666670%, 5.098039%, 56.862750%)">@selector<span style="font-size: 14.000000pt; font-family: "Menlo""><span style="font-size: 14.000000pt; font-family: "Menlo"">(proximitychange:)<span style="font-size: 14.000000pt; font-family: "Menlo"; color: rgb(18.039220%, 5.098039%, 43.137250%)">name<span style="font-size: 14.000000pt; font-family: "Menlo"">:<span style="font-size: 14.000000pt; font-family: "Menlo"; color: rgb(36.078430%, 14.901960%, 60.000000%)">uideviceproximitystatedidchangenotification<span style="font-size: 14.000000pt; font-family: "Menlo"; color: rgb(18.039220%, 5.098039%, 43.137250%)">object<span style="font-size: 14.000000pt; font-family: "Menlo"">:<span style="font-size: 14.000000pt; font-family: "Menlo"; color: rgb(66.666670%, 5.098039%, 56.862750%)">nil<span style="font-size: 14.000000pt; font-family: "Menlo"">];</span> </span> </span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span><p><p></p></p><p><span style="font-size: 14.000000pt; font-family: "Menlo"">- (<span style="font-size: 14.000000pt; font-family: "Menlo"; color: rgb(66.666670%, 5.098039%, 56.862750%)">void<span style="font-size: 14.000000pt; font-family: "Menlo"">) Proximitychange: (<span style="font-size: 14.000000pt; font-family: "Menlo"; color: rgb(36.078430%, 14.901960%, 60.000000%)">Nsnotificationcenter<span style="font-size: 14.000000pt; font-family: "Menlo"">*) Notification {<span style="font-size: 14.000000pt; font-family: "Menlo"; color: rgb(66.666670%, 5.098039%, 56.862750%)">If<span style="font-size: 14.000000pt; font-family: "Menlo"">([<span style="font-size: 14.000000pt; font-family: "Menlo"; color: rgb(36.078430%, 14.901960%, 60.000000%)">Uidevice<span style="font-size: 14.000000pt; font-family: "Menlo"; color: rgb(18.039220%, 5.098039%, 43.137250%)">Currentdevice<span style="font-size: 14.000000pt; font-family: "Menlo"">].<span style="font-size: 14.000000pt; font-family: "Menlo"; color: rgb(36.078430%, 14.901960%, 60.000000%)">Proximitystate<span style="font-size: 14.000000pt; font-family: "Menlo"">==<span style="font-size: 14.000000pt; font-family: "Menlo"; color: rgb(66.666670%, 5.098039%, 56.862750%)">YES<span style="font-size: 14.000000pt; font-family: "Menlo"">) {<span style="font-size: 14.000000pt; font-family: "Menlo"; color: rgb(18.039220%, 5.098039%, 43.137250%)">NSLog<span style="font-size: 14.000000pt; font-family: "Menlo"">(<span style="font-size: 14.000000pt; font-family: "Menlo"; color: rgb(76.862750%, 10.196080%, 8.627451%)">@"<span style="font-size: 14.000000pt; font-family: "STSongti-SC-Regular"; color: rgb(76.862750%, 10.196080%, 8.627451%)"><span style="font-size:14.000000pt; Font-family: " stsongti-sc-regular"; color:rgb (76.86275%, 10.19608%, 8.627451%) "> An object is close to the device screen <span style=" font-size:14.000000pt; font-family: " menlo"; color:rgb (76.86275%, 10.19608%, 8.627451%) ">" <span style="font-size:14.000000pt; font-family:" menlo""); <span menlo"; color:rgb (0%, 45.4902%, 0%)">/ /<span style="font-size:14.000000pt; Font-family: " stsongti-sc-regular"; color:rgb (0%, 45.4902%, 0%) "> screen will automatically lock <br> <span style=" font-size:14.000000pt; font-family: " menlo""> <span style="font-size:14.000000pt; Font-family: " menlo"; color:rgb (66.666670%, 5.098039%, 56.862750%) ">else <span style="font-size:14.000000pt; font-family:" menlo"">{</span> </span> </span> </span> </span> </span> </span> /span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></p><p><p> <span style="font-size:14.000000pt; font-family:" menlo"; color:rgb (18.039220%, 5.098039%, 43.137250%)">NS Log<span style="font-size:14.000000pt; Font-family: " menlo"" (<span menlo"; color:rgb (76.862750%, 10.196080%, 8.627451%) ">@" <span style="font-size:14.000000pt; font-family:" stsongti-sc-regular"; color:rgb (76.86275%, 10.19608%, 8.627451%) "> an object away from the device screen <span style=" font-size:14.000000pt; font-family: " menlo"; color:rgb (76.86275%, 10.19608%, 8.627451%) ">" <span style="font-size:14.000000pt; font-family:" menlo""); <span menlo"; color:rgb (0%, 45.4902%, 0%)">/ /<span style="font-size:14.000000pt; Font-family: " stsongti-sc-regular"; color:rgb (0%, 45.4902%, 0%) "> screen will automatically unlock </span> </span> </span> </span> </span> </span> </p></p><p><p><span style="font-size: 14.000000pt; font-family: "Menlo"">}}</span></p></p><p><p>Open Distance sensing function</p></p><pre><span style="color: #0000ff"><span style="color: #0000ff">#import</span></span> <span style="color: #800000"><span style="color: #800000">"</span></span><span style="color: #800000"><span style="color: #800000">ViewController.h</span></span><span style="color: #800000"><span style="color: #800000">"</span></span><span style="color: #0000ff"><span style="color: #0000ff">@interface</span></span><span style="color: #000000"><span style="color: #000000">Viewcontroller ()</span></span><span style="color: #0000ff"><span style="color: #0000ff">@end</span></span><span style="color: #0000ff"><span style="color: #0000ff">@implementation</span></span><span style="color: #000000"><span style="color: #000000">Viewcontroller</span></span>- (<span style="color: #0000ff"><span style="color: #0000ff">void</span></span><span style="color: #000000"><span style="color: #000000">) Viewdidload {[super viewdidload]; </span></span><span style="color: #008000"><span style="color: #008000">//</span></span><span style="color: #008000"><span style="color: #008000">additional setup after loading the view, typically from a nib. </span></span><span style="color: #008000"><span style="color: #008000">//</span></span><span style="color: #008000"><span style="color: #008000">1. Turn on the distance sensor (note: the distance sensor is off by Default)</span></span><span style="color: #008000"><span style="color: #008000">//</span></span><span style="color: #008000"><span style="color: #008000">[uiapplication sharedapplication].proximitysensingenabled = YES; </span></span><span style="color: #008000"><span style="color: #008000">//</span></span><span style="color: #008000"><span style="color: #008000">as soon as it's switched on, real-time monitoring begins</span></span>[uidevice currentdevice].proximitymonitoringenabled =<span style="color: #000000"><span style="color: #000000">YES; </span></span><span style="color: #008000"><span style="color: #008000">//</span></span><span style="color: #008000"><span style="color: #008000">2. When the monitor hears an object near the device, the system will give notice</span></span>[[nsnotificationcenter defaultcenter] addobserver:self selector: @selector (proximitystatedidchange:) name: Uideviceproximitystatedidchangenotification<span style="color: #0000ff"><span style="color: #0000ff">Object</span></span><span style="color: #000000"><span style="color: #000000">: nil]; }</span></span>- (<span style="color: #0000ff"><span style="color: #0000ff">void</span></span><span style="color: #000000"><span style="color: #000000">) dealloc{[[nsnotificationcenter defaultcenter] removeobserver:self];}</span></span><span style="color: #008000"><span style="color: #008000">//</span></span><span style="color: #008000"><span style="color: #008000">called when the monitor hears an object near the device</span></span>- (<span style="color: #0000ff"><span style="color: #0000ff">void</span></span>) proximitystatedidchange: (nsnotification *<span style="color: #000000"><span style="color: #000000">) note{</span></span><span style="color: #008000"><span style="color: #008000">//</span></span><span style="color: #008000"><span style="color: #008000">NSLog (@ "%@", note);</span></span> <span style="color: #0000ff"><span style="color: #0000ff">if</span></span><span style="color: #000000"><span style="color: #000000">([uidevice Currentdevice].proximitystate) {NSLog (</span></span><span style="color: #800000"><span style="color: #800000">@"</span></span><span style="color: #800000"><span style="color: #800000">there are objects near</span></span><span style="color: #800000"><span style="color: #800000">"</span></span><span style="color: #000000"><span style="color: #000000">); }</span></span><span style="color: #0000ff"><span style="color: #0000ff">Else</span></span><span style="color: #000000"><span style="color: #000000">{NSLog (</span></span><span style="color: #800000"><span style="color: #800000">@"</span></span><span style="color: #800000"><span style="color: #800000">Object Left</span></span><span style="color: #800000"><span style="color: #800000">"</span></span><span style="color: #000000"><span style="color: #000000">); }}</span></span><span style="color: #0000ff"><span style="color: #0000ff">@end</span></span></pre><span class="cnblogs_code_collapse"><span class="cnblogs_code_collapse">View Code</span></span><p><p></p></p><p style="margin-left: 90px"><p style="margin-left: 90px"><br><br></p></p><p><p>IOS Uidevice distance sensor (turn Off)</p></p></span>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.