ユーザーの現在の位置が 5 分ごとにサーバーに送信される機能を構築したいと考えています。Apple が好んでいるようには思えません。これは社内アプリになりますが (ユーザーは自分の場所が使用されていることを知っています)、ルールはそれほど厳密ではありませんか? 誰でもこれについて経験がありますか?
前もって感謝します!
非常に単純なケースのようです。
PLIST ファイルでバックグラウンド位置情報サービスの要件を有効にし、バックグラウンドで GPS を継続的に使用するとバッテリーが大幅に消耗するという免責事項をアプリの説明に記載し、コードで 5 分ごとに GPS 位置情報をアップロードするようにします。
バックグラウンドでも動作します:)
ユーザーが運転しているときにユーザーのルートをライブで記録するアプリをアプリストアに持っていますが、サーバーには送信されず、ユーザー自身の位置を常に追跡し、ユーザーが完了したらGPSを停止できます追跡。
ユーザーの位置を追跡することは一行でできることではありませんが、学習ルートを提案することはできます。
まず、問題には 2 つの部分があります。
a) ユーザーの位置を追跡する b) ユーザーの GPS 座標をサーバーに送信する
ユーザーの位置を追跡するには、2 つの方法があります。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');
}
}
}
}