我想构建一个功能,每 5 分钟将用户的当前位置发送到服务器。听起来苹果并不喜欢这样的功能。 不过,这将是一个内部应用程序(并且用户知道他们的位置被使用),对此规则是否不那么严格?大家有这方面的经验吗
提前致谢!
看起来是一个非常简单的案例。
在您的 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');
}
}
}
}
参加聚会太晚了,但仍然值得在这里分享..!
CLLocationUpdate 是 iOS 17 中提供的游戏规则改变者。
无需再担心应用程序状态。
简单的两行代码即可为您提供全天的位置更新。
let updates = CLLocationUpdate.liveUpdates()
for try await update in updates { }
打破循环以停止位置更新。太简单了不是吗?
确保您已在后台模式下启用位置。
仅供参考:基于时间间隔的位置更新仍然是一个缺失的功能。希望我们尽快收到此更新!
参考: