简体   繁体   中英

Location Services in IOS

All,

I have this code :

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeSound | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert)];
    self.locationmanager = [CLLocationManager new];
    [self.locationmanager setDesiredAccuracy:kCLLocationAccuracyBest];
    [self.locationmanager setDistanceFilter:kCLDistanceFilterNone];
    CLAuthorizationStatus status = [CLLocationManager authorizationStatus];
    if (status == kCLAuthorizationStatusRestricted || status == kCLAuthorizationStatusDenied)
    {
        // location not avaialble create an alert.
        NSLog(@"User has not allowed location to be tracked");

    }

    if (status == kCLAuthorizationStatusAuthorized)
    {

in the AppDelegate didfinishLauncingWithOptions, and I need to it to check / allow the location lat/long to be retrieved, the ViewdidLoad opens before the IF statement to see if it authorised. So the ViewDidLoad doesn't get the correct information. When the if(authorised) is executed then it goes to a db with the lat and long and then the view did load should get the data. but the view did load is blank as the if statement has not be ran. Within the IF statement it executes a block (NFNetworking) and it does get there. If i re-run it from XCODE it does.

In your Appdelegate.h

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

 @interface AppDelegate : UIResponder <UIApplicationDelegate,CLLocationManagerDelegate>
   {

    CLLocationCoordinate2D homeLocation;
    CLLocationCoordinate2D phoneLocation;
    CLLocationManager *locationManager;
    CLLocation *currentLocation;
    NSDictionary *cstreampostData;
   }

In Appdeleate.m

     -(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
  {



   if ([CLLocationManager locationServicesEnabled]) {
       self.locationManager = [[CLLocationManager alloc]init];
       self.locationManager.delegate=self;
       self.locationManager.desiredAccuracy=kCLLocationAccuracyKilometer;


     }else{
       UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Location Services Not Available!" message:@"" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles: nil];
    [alert show];

    }



. 
. 
. 

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self.window makeKeyAndVisible];

 return YES;

 }

   #pragma mark- Current Location Delegates


 -(void)setUpLocationManager:(id)sender{
   //location tracking
     [self.locationManager startUpdatingLocation];
    }

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

    self.phoneLocation=CLLocationCoordinate2DMake(newLocation.coordinate.latitude, newLocation.coordinate.longitude);
    self.currentLocation=newLocation;

  if (newLocation.horizontalAccuracy <=1000.0f) {
    [self.locationManager stopUpdatingLocation];
    NSLog(@"%g %g",newLocation.coordinate.longitude, newLocation.coordinate.latitude);

      self.cstreampostData=[NSDictionary dictionaryWithObjectsAndKeys:

                          [NSString stringWithFormat:@"%g",newLocation.coordinate.latitude],@"latitude",
                          [NSString stringWithFormat:@"%g",newLocation.coordinate.longitude],@"longitude", nil];
   }



   NSLog(@"current location latitude is======%f",phoneLocation.latitude);
   NSLog(@"current location longitude is======%f",phoneLocation.longitude);



     }

-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
    if (error.code == kCLErrorDenied) {
    [self.locationManager stopUpdatingLocation];
   }else if(error.code == kCLErrorLocationUnknown){
    //retry
   }else{

   }


     NSLog(@"%@",error);
     UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Location Services Not Available!" message:@"" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles: nil];
     [alert show];

       }

in your ViewController.m

-(void)viewDidLoad
{
[super viewDidLoad];

 AppDelegate *appDelegate=(AppDelegate *)[UIApplication sharedApplication].delegate;
 [appDelegate setUpLocationManager:sender];
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM