使用AFNetworking 3在后台以及正在运行的应用程序中将当前位置发送到服务器 [英] sending current location to server in background as well as in running app using AFNetworking 3

查看:78
本文介绍了使用AFNetworking 3在后台以及正在运行的应用程序中将当前位置发送到服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图对此进行大量搜索,但是我没有得到想要的确切答案.

I tried to search lot for this, but i didn't get exact answer what i want.

我的问题是

我可以在应用程序运行以及在后台运行时将用户的当前位置发送到服务器.

Can I send user's current location to server when app is running as well as in background.

例如:在我的应用中,我想每2分钟存储一次用户的当前位置,然后将其当前位置发送到服务器.如果用户在不到2分钟的时间内移动了50米, . 我怎样才能做到这一点 ?我可以在后台模式和前台模式下将位置更新发送到服务器吗?如果是,那怎么办?

For example: In my app i want to store the user's current location in every 2min and then send his current location to server.as well as if user move 50 meters within less than 2min then also. How can i do this ? Can i send location updates to the server in background mode as well as foreground mode? if yes then how?

我尝试过以下方式:

 self.locations = [[NSMutableArray alloc] init];
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.distanceFilter = 10;

self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager.delegate = self;
[self.locationManager requestAlwaysAuthorization];

[self.locationManager startUpdatingLocation];

...

- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
       fromLocation:(CLLocation *)oldLocation
{



 // Add another annotation to the map.
MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
annotation.coordinate = newLocation.coordinate;
[self.map addAnnotation:annotation];

// Also add to our map so we can remove old values later
[self.locations addObject:annotation];

// Remove values if the array is too big
while (self.locations.count > 1)
{
    annotation = [self.locations objectAtIndex:0];
    [self.locations removeObjectAtIndex:0];

    // Also remove from the map
    [self.map removeAnnotation:annotation];
}


if (UIApplication.sharedApplication.applicationState == UIApplicationStateActive)
{
    // determine the region the points span so we can update our map's zoom.
    double maxLat = -91;
    double minLat =  91;
    double maxLon = -181;
    double minLon =  181;

    for (MKPointAnnotation *annotation in self.locations)
    {
        CLLocationCoordinate2D coordinate = annotation.coordinate;

        if (coordinate.latitude > maxLat)
            maxLat = coordinate.latitude;
        if (coordinate.latitude < minLat)
            minLat = coordinate.latitude;

        if (coordinate.longitude > maxLon)
            maxLon = coordinate.longitude;
        if (coordinate.longitude < minLon)
            minLon = coordinate.longitude;
    }

    MKCoordinateRegion region;
    region.span.latitudeDelta  = (maxLat +  90) - (minLat +  90);
    region.span.longitudeDelta = (maxLon + 180) - (minLon + 180);

    _latitude1=[NSString stringWithFormat:@"Latitude:%f",newLocation.coordinate.latitude];
    _longitude1=[NSString stringWithFormat:@"Longitude:%f",newLocation.coordinate.longitude];


    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    appDelegate.fetchtokenString1 = [NSString stringWithString:_latitude1];
    appDelegate.fetchedriveridString1 = [NSString stringWithString:_longitude1];





    NSLog(@"%@",_latitude1);
    NSLog(@"%@",_longitude1);


    // the center point is the average of the max and mins
    region.center.latitude  = minLat + region.span.latitudeDelta / 2;
    region.center.longitude = minLon + region.span.longitudeDelta / 2;

    // Set the region of the map.
    [self.map setRegion:region animated:YES];
}
else
{
    NSLog(@"App is backgrounded. New location is %@", newLocation);
}

先谢谢了.

推荐答案

当您的应用程序在前台运行时,请选中此选项.

Check this when your app is running in foreground.

如何在iOS

选中此选项可在您的应用程序在后台运行时发送位置信息.

Check this to send location when your app is in background.

​​在以下情况下向服务器发送纬度和经度应用程序在后台

获取经度和纬度后,使用AFNetworking框架进行发送.

after getting latitude and longitude, send by using AFNetworking framework.

如果您不知道如何获取位置从头开始,请按照以下步骤获取经度和纬度.

If you need from scratch if you dont know how to get location then follow the below steps to get latitude and longitude.

在您的info.pList的两个字符串类型下面添加此

Add this below two string types in your info.pList

然后在您的项目中添加CoreLocation Framework.

Then add CoreLocation Framework in your project.

将其导入到您的ViewController.h

Import it to your ViewController.h

 #import <UIKit/UIKit.h>
 #import <CoreLocation/CoreLocation.h>

 @interface ViewController : UIViewController<CLLocationManagerDelegate> 
 @property(nonatomic, retain) CLLocationManager *locationManager;

 @end

然后在ViewController中执行以下操作.m

Then do below one in your ViewController.m

 #define IS_OS_8_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)

 #import "ViewController.h"

 @interface ViewController ()

 @end

 @implementation ViewController

 - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
#ifdef __IPHONE_8_0
if(IS_OS_8_OR_LATER) {
    // Use one or the other, not both. Depending on what you put in info.plist
    [self.locationManager requestWhenInUseAuthorization];
    [self.locationManager requestAlwaysAuthorization];
}
#endif
[self.locationManager startUpdatingLocation];


}

-(void)viewWillAppear:(BOOL)animated {

NSLog(@"latitude: %f, longitude: %f",self.locationManager.location.coordinate.latitude,self.locationManager.location.coordinate.longitude);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

日志将显示纬度和经度.

Log will show the latitude and longitude.

这篇关于使用AFNetworking 3在后台以及正在运行的应用程序中将当前位置发送到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆