Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
200 views
in Technique[技术] by (71.8m points)

objective c - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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];
  }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...