我一直在關注本教程http://www.devfright.com/ios-6-core-location-tutorial/將位置服務添加到我的ios應用程序。我已經導入Corelocation在我TestFileViewController.h文件像這樣:CLLocation Manager startUpdatingLocation不啓動
#import <UIKit/UIKit.h>
#import <FacebookSDK/FacebookSDK.h>
#import <CoreLocation/CoreLocation.h>
@interface TestFileViewController : UIViewController <UITableViewDelegate,UITableViewDataSource, NSStreamDelegate>
@property (nonatomic, strong) CLLocationManager *locationManager;
@end
然後在testFileViewController.m viewDidLoad中我加了(記住要實現CLLocationManagerDelegate):
//start checking for location from CoreLocation Framework (currently not working)
if ([CLLocationManager locationServicesEnabled])
{
NSLog(@"location services enabled");
_locationManager = [[CLLocationManager alloc] init];
_locationManager.desiredAccuracy = kCLLocationAccuracyBest;
_locationManager.delegate = self;
[_locationManager startUpdatingLocation];
};
}
(有條件CLLocationManager執行真實NSLogs),然後在下面的權利viewDidLoad中:
//fail to find location, handle error
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(@"didFailWithError: %@", error);
UIAlertView *errorAlert = [[UIAlertView alloc]
initWithTitle:@"Error" message:@"Failed to Get Your Location" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[errorAlert show];
}
//show found location, return NSLOG for proof
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation: (CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSLog(@"Made it to location Manager");
CLLocation *location = [locationManager location];
CLLocationCoordinate2D coordinate = [location coordinate];
NSLog(@"%f",coordinate.latitude);
}
我加入了NSLogs只是作爲標記從CON確定無論函數是否被調用,但是既不調用didFailWithError也不調用doUpdateToLocation。
是的我知道didUpdateToLocation已棄用/過時,我用didUpdateLocations替換它,但它仍然無法正常工作,因此我將其更改回來。
如何讓我的locationManager開始更新當前位置並以稍後在我的應用中使用的字符串形式返回座標值?謝謝。
我已經添加了你的建議,它似乎沒有改變發生了什麼。 – canaanmckenzie
對不起,澄清,我讓我的類委託,並設置locationManager從testFileViewController.h類調用,我也刪除了locationManager下方的視圖加載並從選擇器調用它的方法。它仍然返回它已啓用位置服務,但它實際上並不開始更新位置。 – canaanmckenzie
你有沒有在[_locationManager startUpdatingLocation]中放置一個斷點?它會被叫嗎?當它發生時,你應該看到蘋果提示。你看到了嗎?如果不是,您是否有可能更早看到它並拒絕了許可?如果你去設置和隱私你看到你的應用程序名稱和安全性設置爲YES?幫助您在聊天窗口中提供幫助。 lmk – hackerinheels