`

Creating an iPhone Daemon – Part 3

    博客分类:
  • ios
 
阅读更多

This is part three of the blog series “Creating an iPhone Daemon”, for part one please click this link

In the last part of the series, we created the header file for our DLocationDelegate class, now lets create the implementation file (DLocationDelegate.h)

//
//  DLocationDelegate.m
//  
//
//  Created by Chris Alvares on 3/25/09.
//  Copyright 2009 Chris Alvares. All rights reserved.
//
 
#import "DLocationDelegate.h"
 
#define NSURLRequestReloadIgnoringLocalCacheData 1
 
@implementation DLocationDelegate
@synthesize locationManager;
 
-(id) init
{
	if (self = [super init])
	{
		trackingGPS = false;
 
		NSLog(@"starting the location Manager");
		self.locationManager = [[CLLocationManager alloc] init];
		self.locationManager.delegate = self;
	}
	return self;
}
 
//this function is to only be called once.
-(void) startIt:(NSTimer *) timer
{
	if(timer != nil) [timer invalidate];
	trackingGPS = true;
	[self.locationManager startUpdatingLocation];
}
 
//the difference in this function is that it invalidates the timer function, and can run more than one time
-(void) startItAgain:(NSTimer *)timer
{
	if(!trackingGPS)
	{
		trackingGPS = true;
		[self.locationManager startUpdatingLocation];
	}
}
 
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
	srandom(time(0)); //do this to make sure that it does not use a cached page
	NSLog(@"Location found");
	//if the horizontalAccuracy is negative, CoreLocation failed, and we want a good reading, so we want at least 100 meter accuracy
	if([newLocation horizontalAccuracy] < 100 && [newLocation horizontalAccuracy] > 0)
	{
		[self.locationManager stopUpdatingLocation];
		NSNumber *num = [NSNumber numberWithInt:(random())];
		NSLog(@"Latitude %lf  Longitude %lf", newLocation.coordinate.latitude, newLocation.coordinate.longitude);
 
		NSNumber *latitude = [[NSNumber alloc] initWithDouble:newLocation.coordinate.latitude];
		NSNumber *longitude = [[NSNumber alloc] initWithDouble:newLocation.coordinate.longitude];
		NSNumber *altitude = [[NSNumber alloc] initWithDouble:newLocation.altitude];
 
		NSMutableString *str = [[NSMutableString alloc] 
								initWithString:@"http://chrisalvares.com/iPhoneLocationService.php?ID=2&LATITUDE="];
		[str appendString:[latitude stringValue]];
		[str appendString:@"&LONGITUDE="];
		[str appendString:[longitude stringValue]];
 
		[str appendString:@"&ALTITUDE="];
		[str appendString:[altitude stringValue]];
		[str appendString:@"&RANDOM="];
		[str appendString:[num stringValue]];
 
		NSURL *theURL = [[NSURL alloc] initWithString:str];
 
		NSURLRequest *theRequest = [NSURLRequest requestWithURL:theURL
													cachePolicy:NSURLRequestReloadIgnoringLocalCacheData 
												timeoutInterval:120];
 
 
		NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:theRequest 
																	  delegate:self 
															  startImmediately:YES];
		if(connection == nil)
		{
			trackingGPS = NO;
		}		
 
		NSLog(@"setting timer for 30 minutes");
		NSTimer *timer =  [[NSTimer
							timerWithTimeInterval:1800.0
							target:self
							selector:@selector(startItAgain:)
							userInfo:nil
							repeats:NO
							] retain];
		[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
 
		[timer release];
		[latitude release];
		[longitude release];
		[altitude release];
		[theURL release];
	}
	else
	{
		NSLog(@"Accuracy not good enough %lf", [newLocation horizontalAccuracy]);
 
	}
}
 
- (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error
{
	trackingGPS = false;
 
	NSLog(@"trackingGPS failed");
}
 
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
	NSLog(@"GPS information Sent");
	trackingGPS = false;	
}
 
 
-(void) dealloc
{
	[locationManager release];
	[super dealloc];
}
@end

Yes, it is a pretty big file, so I won’t explain it all (it has comments to help you).

self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;

These two lines are important, after we init the locationManager we must make sure that the delegate is set to our DLocationDelegate class.

if([newLocation horizontalAccuracy] < 100 && [newLocation horizontalAccuracy] > 0)

Inside this function, you will notice the CLLocation *newlocation’s horizontalAccuracy property. If the horizontal accuracy is less than 0, than there was an error, if it is greater than 100, the accuracy is very poor, so the DLocationDelegate class will wait for a better reading.

[self.locationManager stopUpdatingLocation];

This line is VERY important, we have to stop the GPS from updating, otherwise the iPhone’s batter will die super quickly.

NSMutableString *str = [[NSMutableString alloc] 
						initWithString:@"http://youriphonelocationserver.com/locationService.php?ID=2&LATITUDE="];
[str appendString:[latitude stringValue]];
[str appendString:@"&LONGITUDE="];
[str appendString:[longitude stringValue]];
 
[str appendString:@"&ALTITUDE="];
[str appendString:[altitude stringValue]];
[str appendString:@"&RANDOM="];
[str appendString:[num stringValue]];

When you put everything together, you should get a link like:

http://youriphonelocationserver.com/locationService.php?ID=iphoneid&LATITUDE=laditudeNumber&LONGITUDE=longitudeNumber&ALTITUDE=number&RANDOM=12312

We will create this PHP file in an upcoming tutorial.

NSTimer *timer =  [[NSTimer
					timerWithTimeInterval:1800.0
					target:self
					selector:@selector(startItAgain:)
					userInfo:nil
					repeats:NO
					] retain];
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];

The DLocationDelegate timer is set for 1800.0 seconds, which is 30 minutes. I found that 30 minutes does not drain the battery too much, but still has a good amount of readings just incase your iPhone is lost.

While this will get your DLocationDelegate class setup, we still have one more issue, and that is that the iPhone will go into a hard sleep after a minute of no use. We will fix this in an upcoming tutorial.


分享到:
评论

相关推荐

    Daemon Tools Lite 4.30.3简体中文V4.05版

    Daemon Tools Lite 4.30.3是一款广受好评的虚拟光驱软件,尤其对于中文用户来说,其简体中文版本极大地便利了操作和理解。该版本为4.05版,表明它已经过多次更新和优化,以适应用户的需求和系统兼容性。 虚拟光驱是...

    Daemon_Tools Daemon_Tools

    3. **快速加载**:Daemon Tools能够快速加载和卸载镜像,提供无缝的用户体验,无需等待物理光驱的读取时间。 4. **集成到资源管理器**:用户可以直接在Windows资源管理器中右键点击镜像文件,选择“装载”或“卸载...

    daemon.rar daemon.rar

    "daemon.rar" 是一个包含"daemon.exe"的压缩包,该文件通常指的是Daemon Tools,这是一个非常流行的虚拟光驱软件,尤其适用于Windows操作系统。 Daemon Tools允许用户加载ISO、BIN等光盘映像文件,模拟出一个或多个...

    虚拟光驱 daemon_tools_347cn_eric

    虚拟光驱 daemon_tools_347cn_eric虚拟光驱 daemon_tools_347cn_eric虚拟光驱 daemon_tools_347cn_eric虚拟光驱 daemon_tools_347cn_eric虚拟光驱 daemon_tools_347cn_eric虚拟光驱 daemon_tools_347cn_eric虚拟光驱...

    Daemon Tools 3.47 英文版

    Daemon Tools是一款广受欢迎的虚拟光驱软件,尤其在游戏爱好者和系统管理员中有着广泛的使用。它的3.47版本是一个经典的老版本,以其小巧的体积(不到500KB)和出色的性能赢得了用户的好评。这个版本可能在很多地方...

    daemontools 3.4.7简体中文

    3. **光盘保护**: Daemon Tools支持多种光盘保护机制,如SafeDisc、SecuROM、ARMed等,使得用户可以在受保护的游戏中使用虚拟光驱。 4. **快速访问**: 虚拟光驱的读取速度远超物理光驱,尤其对于大容量的游戏或应用...

    Daemon Tools 3.46 简体中文版.rar

    Daemon Tools是一款广受欢迎的虚拟光驱软件,尤其在3.46版本时,它已经拥有相当成熟的稳定性和功能。这个版本特别为中国用户提供了简体中文界面,使得国内用户能够更加方便地操作和理解软件的各项功能。 Daemon ...

    Daemon Tools v4.0.9

    Daemon Tools是一款著名的虚拟光驱软件,它在IT领域中被广泛使用,特别是在游戏爱好者和系统管理员之间。Daemon Tools v4.0.9是该软件的一个版本,它允许用户创建虚拟CD和DVD驱动器,以便无需物理光盘即可运行软件、...

    Daemon 虚拟光驱清除工具

    3. 检查“启动”文件夹(C:\Users\用户名\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup)和“系统启动”文件夹(C:\Windows\System32\config\systemprofile\Desktop),看是否有Daemon Tools的...

    DaemonTools4.09 虚拟光驱

    DaemonTools4.09是一款经典的虚拟光驱软件,它允许用户在计算机上模拟CD/DVD驱动器,无需物理光盘即可运行光盘映像文件。这个版本被特别提及为适用于Windows Server 2008操作系统,表明它兼容该服务器平台,但可能不...

    虚拟光驱daemon3.47版

    标题中的"daemon3.47版"指的是Daemon Tools这一知名虚拟光驱软件的特定版本,即3.47版。Daemon Tools是历史悠久且广受好评的虚拟光驱软件之一,它提供多种操作系统平台的支持,包括Windows系统。 Daemon Tools的...

    DAEMON5.2.0.0

    3. **多平台支持**:除了Windows操作系统,DAEMON Tools还可能对其他平台有所支持,尽管这里未明确提及,但通常这样的专业软件会考虑跨平台兼容性。 4. **易用界面**:DAEMON Tools以用户友好著称,它的界面设计...

    daemontool4.11中文完美版daemontool4.11中文完美版

    Daemontools是一款功能强大的系统工具,主要用于管理和监控持续运行的服务。版本4.11是其一个重要的更新,特别地,这里的"中文完美版"意味着它已经进行了汉化处理,适合中国用户使用,无需担心语言障碍。这个版本的...

    Daemon317 344 346 347

    - `daemon-tools-3-17.exe`:这是Daemon Tools 3.17的安装程序文件。 6. **使用方法**:下载并解压压缩包后,用户需要找到对应版本的安装程序(通常是.exe文件),双击运行进行安装。安装过程中按照提示步骤操作,...

    daemon329.exe

    daemon329.exe虚拟光驱

    虚拟光驱-DAEMON Tools V3.46 简体中文版

    《虚拟光驱技术详解与DAEMON Tools V3.46简体中文版介绍》 在数字化时代,虚拟光驱作为一种高效、便捷的工具,已经逐渐成为计算机用户的重要辅助软件。虚拟光驱允许用户在没有物理光驱的情况下,通过创建虚拟光盘...

    精灵虚拟光驱(Daemon Tools Lite) v4.47.1 官方中文版最详细的安装图解

    3. 轻松安装:Daemon Tools Lite 的安装过程非常简单,用户只需按照安装向导的提示进行操作。 4. 易于使用:Daemon Tools Lite 的界面非常友好,用户可以轻松地管理虚拟磁盘和文件。 5. 免费使用:Daemon Tools Lite...

    daemon-0.8.tar.gz

    "daemon-0.8.tar.gz" 是一个在IT领域常见的压缩文件格式,它包含了名为 "daemon-0.8" 的项目或软件的源代码或资源。这个文件的命名方式表明它是一个版本号为0.8的守护进程(daemon)相关的程序。在Linux和Unix系统中...

    DAEMON Tools 3.47 中文版

    DAEMON Tools Lite 可以制作简单光盘映像文件和模拟 CD/DVD 光驱的最流行软件产品 功能 加载 *.mdx, *.mds/*.mdf, *.iso, *.b5t, *.b6t, *.bwt, *.ccd, *.cdi, *.bin/*.cue, *.ape/*.cue, *.flac/*.cue, *.nrg, ...

    Daemon Tools v4.03 V4.03HE 汉化版

    Daemon Tools v4.03 V4.03HE 汉化版,相信经常使用虚拟光驱的人都应该记得,这是当年极其经典的一个版本。 现在的虚拟光驱越做越大,功能也越来越多,最新的版本Daemon Tools 安装后要占用硬盘50多M,然尔对一般的...

Global site tag (gtag.js) - Google Analytics