我想构建一个功能,用户的当前位置将每隔5分钟发送到服务器。这听起来并不像Apple会喜欢的东西。 这将是一个内部应用程序(并且用户知道他们的位置被使用),规则不那么严格吗?有人有这方面的经验吗?
提前致谢!
答案 0 :(得分:7)
似乎是一个非常直截了当的案例。
在您的PLIST文件中启用后台位置服务要求,在您的应用说明中放置免责声明,说明在后台连续使用GPS会大大耗尽电池,然后让您的代码每5分钟上传一次GPS位置。
即使在后台也能正常工作:)
我在应用程序商店中有一个应用程序,当用户开车时记录用户的路线,虽然它不会发送到服务器,但它会不断跟踪用户自己的位置,当用户完成时,他们可以停止GPS跟踪。
跟踪用户的位置并不是一个单行的事情,但我可以建议一条学习路线,这不是太多的压力。
首先,问题分为两部分:
a)跟踪用户的位置 b)将用户的GPS坐标发送到服务器
跟踪用户的位置可以通过两种方式完成。您可以使用CLLocationManager跟踪用户的位置,或者如果您想要快速和脏的方法,可以使用MKMapView的委托方法:
// --------------------------------------------------------------
// Example .m files implementation
// --------------------------------------------------------------
-(void)viewDidLoad
{
...
myMapView.delegate = self;
...
}
// --------------------------------------------------------------
// this MapView delegate method gets called every time your
// user location is updated, you can send your GPS location
// to your sever here
// --------------------------------------------------------------
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
// pseudo-code
double latitude = userLocation.coordinate.latitude;
double longitude = userLocation.coordinate.longitude;
// you need to implement this method yourself
[self sendGPSToServerWithLatitude:latitude AndLongitude:longitude];
}
// sends the GPS coordinate to your server
-(void)sendGPSToServerWithLatitude:(double)paramLatitude AndLongitude:(double)paramLongitude
{
// ------------------------------------------------------
// There are other libraries you can use like
// AFNetworking, but when I last tested AFNetworking
// a few weeks ago, I had issues with it sending
// email addresses or multiple word POST values
// ------------------------------------------------------
// here I am using ASIHttpRequest library and it's ASIFormDataRequest.h class
// to make a POST value to a server. You need to build the server web service
// part to receive the latitude and longitude
NSURL *url = [NSURL urlWithString:@"http://www.youserver.com/api"];
__block ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:[NSNumber numberWithDouble:paramLatitude] forKey:@"latitude"];
[request setPostValue:[NSNumber numberWithDouble:paramLongitude] forKey:@"longitude"];
[request setPostValue:userNameString forKey:@"username"];
[request setCompletionBlock:^{
NSDictionary *data = [request responseString];
NSLog(@"Server response = %@", data);
}];
[request setFailedBlock:^{
NSLog(@"Server error: %@", [[request error] localizedDescription]);
}];
[request startAsynchronous];
}
PHP服务器端代码
// --------------------------------------------------------------
// This is an example server implementation using PHP and Symfony
// web framework.
//
// You don't have to use PHP and Symfony, you can use .NET C# too
// or any other server languages you like to build the web service
// --------------------------------------------------------------
class DefaultController
{
...
// -------------------------------------
// expects and latitude and longitude
// coordinate pair from the client
// either using POST or GET
// -------------------------------------
public function recordGPSLocationAction()
{
// checks to see if the user accessing the
// web service is authorized to do so
if($this->authorize())
{
return new Response('Not authorized');
}
else // assume user is authorized from this point on
{
// check to see if user has passed in latitude and longitude
if(!isset($_REQUEST['latitude']) || !isset($_REQUEST['longitude']
|| !isset($_REQUEST['username'])
{
throw $this->createNotFoundException('Username, Latitude or Longitude was not received');
}
else
{
// write your latitude and longitude for the specified username to database here
....
return new Response('User GPS location saved');
}
}
}
}