现在的位置: 首页 > 综合 > 正文

技术分享- IOS后台获取地理位置并且上传到服务器

2018年07月26日 ⁄ 综合 ⁄ 共 6346字 ⁄ 字号 评论关闭

有时候需要获取手机的地理位置,当程序关闭的时候,也可以获取地理位置并且上传到服务器端。苹果在后台可以运行的程序,只有三种,一个是声音,一个是获取地理位置。下面主要说一下在后台获取地理位置,并且定时发送到服务器端。
实现非常简单,首先在info.plist中添加属性:


通过下面的代码获取地理位置:
#import “MyCLController.h”

@implementation MyCLController

@synthesize locationManager;
@synthesize delegate;

- (id) init {
self = [super init];
if (self != nil) {
self.locationManager = [[[CLLocationManager alloc] init] autorelease];
self.locationManager.delegate = self; // send loc updates to myself
}
return self;
}

- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
NSLog(@”dsdsds===>>>%f”,newLocation.coordinate.latitude);
[self.delegate locationUpdate:newLocation];
}


- (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error
{
[self.delegate locationError:error];
}

- (void)dealloc {
[self.locationManager release];
[super dealloc];
}
@end




在添加一个定时器,就可以定时发送数据。

[NSTimer scheduledTimerWithTimeInterval:(1.0) target:self selector:@selector(onTimer) userInfo:nil repeats:YES];


下面是程序关闭之后运行的日志:
2012-05-13 16:58:51.826 HelloThere[5339:707] connectionDidFinishLoading
2012-05-13 16:58:51.828 HelloThere[5339:707] receivedData length—->8345
2012-05-13 16:58:51.979 HelloThere[5339:707] dsdsds===>>>40.009185
2012-05-13 16:58:51.980 HelloThere[5339:707] sss==2012-05-13 08:58:51 +0000
2012-05-13 16:58:52.753 HelloThere[5339:707] xxxxxx21
2012-05-13 16:58:52.827 HelloThere[5339:707] connectionDidFinishLoading
2012-05-13 16:58:52.829 HelloThere[5339:707] receivedData length—->8345
2012-05-13 16:58:52.981 HelloThere[5339:707] dsdsds===>>>40.009185
2012-05-13 16:58:52.982 HelloThere[5339:707] sss==2012-05-13 08:58:52 +0000
2012-05-13 16:58:53.753 HelloThere[5339:707] xxxxxx22
2012-05-13 16:58:53.826 HelloThere[5339:707] connectionDidFinishLoading
2012-05-13 16:58:53.828 HelloThere[5339:707] receivedData length—->8345
2012-05-13 16:58:53.991 HelloThere[5339:707] dsdsds===>>>40.009185
2012-05-13 16:58:53.992 HelloThere[5339:707] sss==2012-05-13 08:58:53 +0000
2012-05-13 16:58:54.753 HelloThere[5339:707] xxxxxx23
2012-05-13 16:58:54.824 HelloThere[5339:707] connectionDidFinishLoading
2012-05-13 16:58:54.826 HelloThere[5339:707] receivedData length—->8345
2012-05-13 16:58:55.004 HelloThere[5339:707] dsdsds===>>>40.009185
2012-05-13 16:58:55.005 HelloThere[5339:707] sss==2012-05-13 08:58:55 +0000
2012-05-13 16:58:55.753 HelloThere[5339:707] xxxxxx24
2012-05-13 16:58:55.827 HelloThere[5339:707] connectionDidFinishLoading
2012-05-13 16:58:55.829 HelloThere[5339:707] receivedData length—->8345
2012-05-13 16:58:56.027 HelloThere[5339:707] dsdsds===>>>40.009185
2012-05-13 16:58:56.029 HelloThere[5339:707] sss==2012-05-13 08:58:56 +0000
2012-05-13 16:58:56.753 HelloThere[5339:707] xxxxxx25
2012-05-13 16:58:56.828 HelloThere[5339:707] connectionDidFinishLoading
2012-05-13 16:58:56.830 HelloThere[5339:707] receivedData length—->8345
2012-05-13 16:58:57.055 HelloThere[5339:707] dsdsds===>>>40.009185
2012-05-13 16:58:57.057 HelloThere[5339:707] sss==2012-05-13 08:58:57 +0000
2012-05-13 16:58:57.753 HelloThere[5339:707] xxxxxx26
2012-05-13 16:58:57.825 HelloThere[5339:707] connectionDidFinishLoading
2012-05-13 16:58:57.827 HelloThere[5339:707] receivedData length—->8345
2012-05-13 16:58:57.979 HelloThere[5339:707] dsdsds===>>>40.009185
2012-05-13 16:58:57.981 HelloThere[5339:707] sss==2012-05-13 08:58:57 +0000
2012-05-13 16:58:58.753 HelloThere[5339:707] xxxxxx27
2012-05-13 16:58:58.827 HelloThere[5339:707] connectionDidFinishLoading
2012-05-13 16:58:58.829 HelloThere[5339:707] receivedData length—->8345
2012-05-13 16:58:59.154 HelloThere[5339:707] dsdsds===>>>40.009185
2012-05-13 16:58:59.155 HelloThere[5339:707] sss==2012-05-13 08:58:59 +0000
2012-05-13 16:58:59.753 HelloThere[5339:707] xxxxxx282012-05-13 16:58:59.826 HelloThere[5339:707] connectionDidFinishLoading

2012-05-13 16:58:59.827 HelloThere[5339:707] receivedData length—->8345
2012-05-13 16:58:59.977 HelloThere[5339:707] dsdsds===>>>40.009185
2012-05-13 16:58:59.979 HelloThere[5339:707] sss==2012-05-13 08:58:59 +0000
2012-05-13 16:59:00.753 HelloThere[5339:707] xxxxxx29
2012-05-13 16:59:00.828 HelloThere[5339:707] connectionDidFinishLoading
2012-05-13 16:59:00.829 HelloThere[5339:707] receivedData length—->8345
2012-05-13 16:59:01.001 HelloThere[5339:707] dsdsds===>>>40.009185
2012-05-13 16:59:01.003 HelloThere[5339:707] sss==2012-05-13 08:59:00 +0000
2012-05-13 16:59:01.753 HelloThere[5339:707] xxxxxx30
2012-05-13 16:59:01.827 HelloThere[5339:707] connectionDidFinishLoading
2012-05-13 16:59:01.828 HelloThere[5339:707] receivedData length—->8345
2012-05-13 16:59:02.019 HelloThere[5339:707] dsdsds===>>>40.009185
2012-05-13 16:59:02.021 HelloThere[5339:707] sss==2012-05-13 08:59:02 +0000
2012-05-13 16:59:02.754 HelloThere[5339:707] xxxxxx31
2012-05-13 16:59:02.826 HelloThere[5339:707] connectionDidFinishLoading
2012-05-13 16:59:02.828 HelloThere[5339:707] receivedData length—->8345
2012-05-13 16:59:03.103 HelloThere[5339:707] dsdsds===>>>40.009185
2012-05-13 16:59:03.105 HelloThere[5339:707] sss==2012-05-13 08:59:03 +0000
2012-05-13 16:59:03.754 HelloThere[5339:707] xxxxxx32
2012-05-13 16:59:03.830 HelloThere[5339:707] connectionDidFinishLoading
2012-05-13 16:59:03.833 HelloThere[5339:707] receivedData length—->8345
2012-05-13 16:59:03.995 HelloThere[5339:707] dsdsds===>>>40.009230
2012-05-13 16:59:03.997 HelloThere[5339:707] sss==2012-05-13 08:59:03 +0000
2012-05-13 16:59:04.753 HelloThere[5339:707] xxxxxx33
2012-05-13 16:59:04.790 HelloThere[5339:707] connectionDidFinishLoading
2012-05-13 16:59:04.792 HelloThere[5339:707] receivedData length—->8345
2012-05-13 16:59:05.009 HelloThere[5339:707] dsdsds===>>>40.009230
2012-05-13 16:59:05.011 HelloThere[5339:707] sss==2012-05-13 08:59:05 +0000
2012-05-13 16:59:05.753 HelloThere[5339:707] xxxxxx34
2012-05-13 16:59:05.825 HelloThere[5339:707] connectionDidFinishLoading
2012-05-13 16:59:05.828 HelloThere[5339:707] receivedData length—->8345
2012-05-13 16:59:06.021 HelloThere[5339:707] dsdsds===>>>40.009230
2012-05-13 16:59:06.023 HelloThere[5339:707] sss==2012-05-13 08:59:06 +0000
2012-05-13 16:59:06.753 HelloThere[5339:707] xxxxxx35
2012-05-13 16:59:06.823 HelloThere[5339:707] connectionDidFinishLoading
2012-05-13 16:59:06.824 HelloThere[5339:707] receivedData length—->8345
2012-05-13 16:59:07.153 HelloThere[5339:707] dsdsds===>>>40.009230
2012-05-13 16:59:07.155 HelloThere[5339:707] sss==2012-05-13 08:59:07 +0000
2012-05-13 16:59:07.753 HelloThere[5339:707] xxxxxx36
2012-05-13 16:59:07.826 HelloThere[5339:707] connectionDidFinishLoading

2012-05-13 16:59:07.828 HelloThere[5339:707] receivedData length—->8345

转载:http://peixun.eol.cn/company/company_article_detail.php?articleid=22692

源代码:http://easymorse-iphone.googlecode.com/svn/trunk/HelloThere/

抱歉!评论已关闭.