IOS Background Continuous positioning detailed (support ISO9.0 above)

Source: Internet
Author: User

IOS Background Continuous positioning detailed (support ISO9.0 above)

#import <CoreLocation/CoreLocation.h> and implement Cllocationmanagerdelegate agent, the complete code of. h file is as follows:

[OBJC]View Plain Copy
    1. #import <UIKit/UIKit.h>
    2. #import <CoreLocation/CoreLocation.h>
    3. @interface viewcontroller:uiviewcontroller<cllocationmanagerdelegate>
    4. @end

2.info.list file:

Right-click Add Row, the added key is nslocationalwaysusagedescription, the other values are default, examples are as follows:


3. Add Background Location permissions

4.VIEWCONTROLLER.M file:

(1) Define a private Cllocationmanager object

(2) Initialize and set the parameters (Initlocation method), where

Locationmanager.desiredaccuracy set positioning accuracy, with six values selectable, decreasing precision in turn

Kcllocationaccuracybestfornavigation

Kcllocationaccuracybest

Kcllocationaccuracynearesttenmeters

Kcllocationaccuracyhundredmeters

Kcllocationaccuracykilometer

Kcllocationaccuracythreekilometers

Locationmanager.pauseslocationupdatesautomatically set whether to allow the system to automatically pause the positioning, here to set to No, just start I did not set, background location for about 20 minutes to stop!

(3) Implement the Proxy method for Cllocationmanagerdelegate, which is called after each successful location:

-(void) Locationmanager: (Cllocationmanager *) Manager didupdatelocations: (nsarray*) locations;

* You can also do this by implementing the following methods:

-(void) Locationmanager: (Cllocationmanager *) Manager

Didupdatetolocation: (cllocation *) newlocation

Fromlocation: (cllocation *) oldlocation

(4) Implement the Proxy method of Cllocationmanagerdelegate, which is called after locating an error:

-(void) Locationmanager: (Cllocationmanager *) Manager didfailwitherror: (nserror *) error

The complete code for the. m file is as follows:[OBJC]View Plain Copy
  1. #import "ViewController.h"
  2. @interface Viewcontroller () {
  3. Cllocationmanager *locationmanager;
  4. cllocation *newlocation;
  5. Cllocationcoordinate2D coordinate;
  6. }
  7. @end
  8. @implementation Viewcontroller
  9. -(void) Viewdidload {
  10. [Super Viewdidload];
  11. [self initlocation];
  12. }
  13. #pragma mark Initialize the location
  14. -(void) initlocation {
  15. Locationmanager=[[cllocationmanager alloc] init];
  16. Locationmanager. Delegate = self ;
  17. Locationmanager. desiredaccuracy = kcllocationaccuracybest; Set positioning accuracy
  18. if ([[[[ Uidevice Currentdevice] systemversion] Floatvalue] >= 8. 0) {
  19. [Locationmanager requestalwaysauthorization];
  20. }
  21. 9.0 After this must be added is not able to achieve the background of continuous positioning

    if ([[[[Uidevice Currentdevice] systemversion] floatvalue] >= 9.0) {

    Locationmanager.allowsbackgroundlocationupdates = YES;

    }

  22. if (![ Cllocationmanager locationservicesenabled]) {
  23. NSLog (@ "Please turn on location: Settings > Privacy > Locations > Location Services");
  24. }
  25. Locationmanager. pauseslocationupdatesautomatically = NO;
  26. [Locationmanager startupdatinglocation];
  27. //[locationmanager Startmonitoringsignificantlocationchanges];
  28. }
  29. #pragma Mark successfully positioned
  30. -(void) Locationmanager: (cllocationmanager *) Manager didupdatelocations: (nsarray *) locations{
  31. NewLocation = [Locations Lastobject];
  32. double lat = newlocation. Coordinate. latitude;
  33. Double lon = newlocation. Coordinate. Longitude;
  34. NSLog (@ "lat:%f,lon:%f", Lat,lon);
  35. }
  36. #pragma mark failed to locate
  37. -(void) Locationmanager: (cllocationmanager *) Manager Didfailwitherror: (nserror *) error{
  38. NSLog (@ "error:%@", error);
  39. }
  40. -(void) didreceivememorywarning {
  41. [Super didreceivememorywarning];
  42. //Dispose of any resources, can be recreated.
  43. }
  44. @end

IOS Background Continuous positioning detailed (support ISO9.0 above)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.