【问题标题】:Sending location coordinates to server in background from iOS从 iOS 在后台向服务器发送位置坐标
【发布时间】:2014-04-06 08:38:03
【问题描述】:

我正在开发一个需要将用户位置发送到服务器的应用程序。我必须每秒更新用户位置并将其从后台发送到服务器。

我可以在后台获取位置坐标,但是如何每秒将其发送到服务器? 请帮帮我。

我发现这是在一个应用程序中工作 - UBER

【问题讨论】:

    标签: ios objective-c background core-location uibackgroundtask


    【解决方案1】:

    这取决于您的服务器期望发送什么。但这里是一个使用 AFNetworking 2.0 的示例:

    - (void)sendLocation:(CLLocationCoordinate2D)coordinate
    {
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
        NSDictionary *parameters = @{
                                     @"latitude": [NSNumber numberWithDouble:coordinate.latitude],
                                     @"longitude": [NSNumber numberWithDouble:coordinate.longitude]
                                     };
    
        [manager POST:@"http://example.com/yourendpoint"
           parameters:parameters
              success:^(AFHTTPRequestOperation *operation, id responseObject)
        {
            NSLog(@"JSON: %@", responseObject);
        }
              failure:^(AFHTTPRequestOperation *operation, NSError *error)
        {
            NSLog(@"Error: %@", error);
        }];
    }
    

    每秒发送用户位置不是一个好主意。您应该在它发生变化时或在更长的时间间隔内发送它。

    希望对你有帮助。

    【讨论】:

    • 谢谢 reecon..我会试试这个
    • 如果我们将位置发送到服务器的时间间隔更长,那么我们就无法像 uber 和 OLA 应用程序那样显示继续跟踪。如果您有任何解决方案,请告诉我。谢谢。
    猜你喜欢
    • 2022-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多