2016-07-27 43 views
0

我正在Objective-C中爲iPhone 9.3.3開發Xcode 7.3中的應用程序。我遵循這個網站上的建議 - http://nevan.net/2014/09/core-location-manager-changes-in-ios-8/來獲取iphone的位置。我正在使用閃電電纜測試我目前使用的iPhone 9.3.3。核心位置在Xcode 7.3中不起作用

我有「NSLocationWhenInUseUsageDescription」添加到plist值爲「請求位置」。

我很困惑,爲什麼這不起作用。我沒有得到NSLog上的位置的任何字符串。

任何幫助,高度讚賞。


ViewController.h

#import <UIKit/UIKit.h> 

#import <CoreLocation/CoreLocation.h> 

#import <MapKit/MapKit.h> 


@interface ViewController : UIViewController<CLLocationManagerDelegate> 


@property (strong, nonatomic) CLLocationManager *locationManager; 

@property (weak, nonatomic) CLLocationManager *locations; 


// 

//@property (weak, nonatomic) IBOutlet UILabel *longitudelabel; 

//@property (weak, nonatomic) IBOutlet UILabel *latitudelabel; 


@end 

ViewController.m

#import <UIKit/UIKit.h> 

#import <Foundation/Foundation.h> 

#import "ViewController.h" 





@interface ViewController() 

{ 

    NSUserDefaults *defaults; 

} 



@end 



@implementation PollenViewController 



- (void)viewDidLoad 

{ 

    [super viewDidLoad]; 



    // ** Don't forget to add NSLocationWhenInUseUsageDescription in MyApp-Info.plist and give it a string 



    self.locationManager = [[CLLocationManager alloc] init]; 

    self.locationManager.delegate = self; 

    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest; 

    // Check for iOS 8. Without this guard the code will crash with "unknown selector" on iOS 7. 

    if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) { 

     [self.locationManager requestWhenInUseAuthorization]; 

    } 

    [self.locationManager startUpdatingLocation]; 



} 


-(void)viewWillAppear:(BOOL)animated 

{ 

    [super viewWillAppear:animated]; 


} 


// Location Manager Delegate Methods 

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations 

{ 

    NSLog(@"%@", [locations lastObject]); 

} 

@end 
+0

什麼是錯的我們的代碼?是地點零? –

+0

你將需要調試你的代碼。在調用requestWhenInUseAuthorization之前和之後檢查authorizationStatus的值。同時實現委託方法'locationManager:didChangeAuthorizationStatus:'來查看授權狀態的變化。 –

+0

@DuncanC我將我的.m更改爲以下內容 - [鏈接](http://i.imgur。com/m3quLKk.png),並用調試器遍歷它。當[self.locationManager requestWhenInUseAuthorization];被稱爲沒有發生在應用程序中。我也嘗試了[經理requestWhenInUseAuthorization]並沒有得到任何東西。有關如何解決這個問題的任何提示? – Aranelmalina

回答

0

與iOS 8,請求允許並開始使用定位服務現在是單獨的操作。由於

requestWhenInUseAuthorization 

requestAlwaysAuthorization 

方法異步運行,應用程序不能立即啓動定位服務。相反,您需要實施

locationManager:didChangeAuthorizationStatus 

委派方法如下,它隨時根據用戶輸入激發授權狀態。

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status { 

    if (status == kCLAuthorizationStatusAuthorizedAlways 
     || status == kCLAuthorizationStatusAuthorizedWhenInUse) { 
     [manager startUpdatingLocation]; 
    } 
} 

如果用戶以前已經給使用位置服務的權限,委託方法也將位置管理器初始化後調用並有其代表與相應的授權狀態設置如下

CLLocationManager *manager = [CLLocationManager new] 
Manager.delegate = self; 
if [CLLocationManager locationServicesEnabled] { 
    [manager startUpdatingLocation]; 
} 
0

我已經能夠獲取位置並使用以下ViewController.m代碼將其顯示在應用程序中。 @ DuncanC的評論很有幫助。這還包括反向地理編碼。

-Used NSLocationWhenInUseUsageDescription inInfo.plists String with「Need Location」。 - 確保設置 - >隱私 - >位置服務 - > AppName設置爲「While Using」而不是「Never」。

希望這對任何卡住的人都有幫助。

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

@interface ViewController() 
{ 
    NSUserDefaults *defaults; 

    NSString *locationfound; 


} 

@property (strong, nonatomic) CLLocationManager *locationManager; 

@end 

@implementation PollenViewController { 
    CLGeocoder *geocoder; 
    CLPlacemark *placemark; 
    } 


- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    //implement location finder 
    self.locationManager=[[CLLocationManager alloc] init]; 
    self.locationManager.delegate=self; 
    self.locationManager.desiredAccuracy=kCLLocationAccuracyBest; 

    //initialize geocoder for later 
    geocoder = [[CLGeocoder alloc] init]; 


} 

-(void)viewWillAppear:(BOOL)animated 
{ 
    [super viewWillAppear:animated]; 


} 
//CURRENTLY WORKING LOCATION MANAGER 
//roughly based on ---/*https://gist.github.com/paulw11/84b18044942e2ceea52a8*/ 

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus: 
(CLAuthorizationStatus)status { 
    defaults = [NSUserDefaults standardUserDefaults]; 

    NSLog(@"Callback"); 
    NSLog(@"status %i",status); 
    if (status == kCLAuthorizationStatusAuthorizedAlways || status == kCLAuthorizationStatusAuthorizedWhenInUse) { 
     NSLog(@"Authorized"); 
     [self.locationManager startUpdatingLocation]; 

    } else if (status == kCLAuthorizationStatusDenied || status == kCLAuthorizationStatusRestricted) { 
     NSLog(@"Denied"); 
     locationfound=[NSString stringWithFormat:@"No Location Found"];; 
     self.LocationLabel.text=locationfound; 

    }else{ 
     [self.locationManager requestWhenInUseAuthorization]; 
     [self.locationManager requestAlwaysAuthorization]; 
    } 
} 

// Location Manager Delegate Methods 
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations 
{ 
    CLLocation *newLocation = [locations lastObject]; 

    NSLog(@"Location new %@",newLocation) ; 

    // Reverse Geocoding 
    NSLog(@"Resolving the Address"); 
    [geocoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *placemarks, NSError *error) { 
     defaults = [NSUserDefaults standardUserDefaults]; 

//  NSLog(@"Found placemarks: %@, error: %@", placemarks, error); 

     if (error == nil && [placemarks count] > 0) { 
      placemark = [placemarks lastObject]; 
      locationfound= [NSString stringWithFormat:@"%@, %@", 
           placemark.locality, 
           placemark.administrativeArea]; 
      NSLog(@"%@",locationfound); 
      self.LocationLabel.text=locationfound; 
     } else { 
      NSLog(@"%@", error.debugDescription); 
      locationfound=[NSString stringWithFormat:@"No Location Found"];; 
      self.LocationLabel.text=locationfound; 
     } 

    } ]; 
} 

@end 
0

你可以嘗試

- (void)locationManager:(CLLocationManager*) manager didFailWithError:(NSError*)error 

要看到,如果更新失敗,爲什麼