iphone - 当应用程序在后台时,每 5 分钟通过 iPhone 应用程序向服务器发送位置更新

标签 iphone ios

我想构建一个功能,用户的当前位置将每 5 分钟发送到服务器。听起来不像 Apple 会喜欢的东西。
虽然这将是一个内部应用程序(并且用户知道他们的位置已被使用),但规则是否不那么严格?有人有这方面的经验吗?

提前致谢!

最佳答案

似乎是一个非常直接的案例。

在您的 PLIST 文件中启用后台定位服务要求,在您的应用说明中添加免责声明,说明在后台持续使用 GPS 会大大耗尽电池电量,然后让您的代码每 5 分钟上传一次 GPS 位置。

它甚至可以在后台工作:)

我在应用商店有一个应用程序,可以在用户开车时实时记录用户的路线,虽然它不会发送到服务器,但它会不断跟踪用户自己的位置,当用户完成后,他们可以停止 GPS追踪。

一些代码建议

跟踪用户的位置并不是一件简单的事情,但我可以建议一条学习路线,这不会太让人不知所措。

首先,您的问题有两个部分:

a) 跟踪用户的位置
b) 将用户的 GPS 坐标发送到服务器

跟踪用户的位置

可以通过两种方式跟踪用户的位置。您可以使用 CLLocationManager 来跟踪用户的位置,或者如果您想要快速而肮脏的方法,您可以使用 MKMapView 的委托(delegate)方法:

// --------------------------------------------------------------
// 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');
            }
        }
    }
}

关于iphone - 当应用程序在后台时,每 5 分钟通过 iPhone 应用程序向服务器发送位置更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13177464/

相关文章:

iphone - iPhone 5/5th Gen iPod Touch 的加速度计/陀螺仪限制的变化?

iphone - 在类中启用 NSCopying 协议(protocol)

ios - iPhone 上的 Safari 浏览器 : Can I close the oauth browser tab?

ios - 为什么我收到字符串变量错误?

android - "Unresolved reference: NativeSqliteDriver"for sqldelight 添加到 ios 源集

ios - UIView center 属性给出了意想不到的结果

iphone - UIScrollView ImageView 顶部有针脚

iphone - 透明的 UIPopover

ios - 我应该在关闭 View 之前关闭键盘吗?

ios - 从移动浏览器(Safari、iOS 上的 Chrome)中的 Flutter Web 应用重定向到移动应用