XMPP-use CoreLocation to obtain the geographical location and why does CLLocationManager not call the proxy? cllocationmanager

Source: Internet
Author: User

XMPP-use CoreLocation to obtain the geographical location and why does CLLocationManager not call the proxy? cllocationmanager

XMPP-Based Instant Messaging was recently launched. Let's summarize the relevant content and take a look!

1. Use CoreLocation to obtain the geographical location

To use CoreLocation, you must add "CoreLocation. framework" to frameworks and import <CoreLocation/CoreLocation. h>

1. Define member variables

#import "LocationHelper.h"@interface LocationHelper ()<CLLocationManagerDelegate>{    CLLocationManager *_locationManager;}@property(nonatomic,copy)GetLocationCompledBlock getLocationCompledBlock;

2. Implement Singleton

# Pragma mark-shareLocationHelper Singleton + (id) shareLocationHelper {static LocationHelper * instance = nil; static dispatch_once_t onceToken; dispatch_once (& onceToken, ^ {instance = [[LocationHelper alloc] init];}); return instance ;}

3. initialize the CLLocationManager Location manager.

- (void)setup {    _locationManager = [[CLLocationManager alloc] init];    _locationManager.delegate = self;    _locationManager.desiredAccuracy = kCLLocationAccuracyBest;    _locationManager.distanceFilter = 5.0;    [_locationManager requestAlwaysAuthorization];}

DesiredAccuracy is set to the positioning precision. It can be set to the optimum, and the device will automatically locate it in the most accurate way.

DistanceFilter is a distance filter. To reduce the number of polling times on the positioning device, the location change will not notify the delegate every time, but will only notify the delegate program when enough distance is moved, its unit is meters. Here it is set to move 5 and then notify the delegate to process updates.

4. implement external requests to obtain locations

- (void)getCurrentGeolocationsCompled:(GetLocationCompledBlock)compled{    self.getLocationCompledBlock = compled;    [_locationManager startUpdatingLocation];}

You can use startUpdatingLocation to start location management. In general, you 'd better disable it when you do not need to update the location, and use stopUpdatingLocation to save power.

5. Implement the CLLocationManagerDelegate proxy method to obtain location update information

# Pragma mark-CLLocationManager Delegate // proxy method implementation-(void) locationManager :( CLLocationManager *) manager didUpdateToLocation :( CLLocation *) newLocation fromLocation :( CLLocation *) oldLocation {CLGeocoder * geocoder = [[CLGeocoder alloc] init]; [geocoder reverseGeocodeLocation: newLocation completionHandler: ^ (NSArray * placemarks, NSError * error) {if (self. getLocationCompledBlock) {self. getLocationCompledBlock (placemarks) ;}}]; [manager stopUpdatingLocation];}-(void) locationManager :( CLLocationManager *) manager didFailWithError :( NSError *) error {[manager stopUpdatingLocation];} -(void) locationManager :( CLLocationManager *) manager didChangeAuthorizationStatus :( clthorizationstatus) status {switch (status) {condition: if ([_ locationManager respondsToSelector: @ selector (requestalsauwaythorization)]) {[_ locationManager requestAlwaysAuthorization];} break; default: break ;}}

6. Call the method in the Controller to obtain the location information and add the TAG to send it.

[Self. locationHelper Syntax: ^ (NSArray * placemarksA) {CLPlacemark * placemark = [placemarksA lastObject]; if (placemark) {NSDictionary * addressDictionary = placemark. addressDictionary; NSArray * formattedAddressLines = [addressDictionary valueForKey: @ "FormattedAddressLines"]; NSString * geoLocations = [formattedAddressLines lastObject]; if (geoLocations) {// geoLocations = China ***** [weakSelf didSendGeolocationsMessageWithGeolocaltions: geoLocations] ;}}];

 

Ii. Why does CLLocationManager not call a proxy?

The code method has been written happily, but there is always no message when sending the geographic location message, depressed, the breakpoint found that the agent did not go in, and finally the analysis to find information to solve

After the preceding encoding is complete, add the NSLocationAlwaysUsageDescription field to the Plist file.

 

Related Article

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.