なぜCllocationManager DelegateがiPhone SDK 4.0で呼ばれていないのですか?
-
27-09-2019 - |
質問
これは私が私のappdelegateクラスに持っているコードです
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
CLLocationManager *locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.distanceFilter = 1000; // 1 Km
locationManager.desiredAccuracy = kCLLocationAccuracyKilometer;
[locationManager startUpdatingLocation];
}
そして、これは私が私のappdelegateクラスに持っている代表的な方法です
//This is the delegate method for CoreLocation
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
//printf("AppDelegate latitude %+.6f, longitude %+.6f\n", newLocation.coordinate.latitude, newLocation.coordinate.longitude);
}
3.0、3.1、3.1.3で動作しますが、4.0シミュレーターとデバイスの両方で動作していません。
理由は何ですか ?
解決
同様のエラーがあり、今解決されています。問題は、LocationManager変数をローカルで宣言しています。代わりにクラス変数として宣言し、直接またはプロパティ保持(またはARCを使用する場合は強い)を介して保持されていることを確認してください。それは問題を解決します!問題はDe LocationManager変数がリリースされ、デリゲートを更新することはありませんでした。ここにコード:
.hファイル:
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
@interface MapViewController : UIViewController <MKMapViewDelegate, CLLocationManagerDelegate> {
@private
MKMapView *_mapView;
CLLocationManager *_locationManager;
}
@property(nonatomic, strong) CLLocationManager *locationManager;
@end
.mファイル:
self.locationManager = [[CLLocationManager alloc] init];
_locationManager.delegate = self;
_locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[_locationManager startUpdatingLocation];
私はそれが役立つことを願っています。
他のヒント
します -locationManager:didFailWithError:
あなたの代表で呼ばれたことがありますか?ある時点でロケーションデータへのアクセスを拒否したのかもしれませんが、今では迅速になりませんが、アクセスは拒否されます。
CllocationManagerを使用するいくつかのクラスの1つだけが電話をかけられない理由を理解しようとして、私は夢中になりました どれか CllocationManagerDelegateメソッドの。
結局のところ、そのクラスはメイン以外のスレッドから初期化されたため、そのcllocationManagerは非メインスレッドで作成されました...一時的なスレッドでさえあるかもしれません。とにかく、以下でクラスを初期化するための呼び出しを包むだけで、CllocationManagerの委任方法を呼び出すのに必要なことだけでした。
dispatch_sync(dispatch_get_main_queue(),
^{
// create your class that will create a CLLocationManager here.
});
したがって、結論:メインスレッド以外にCllocationManagerを作成しないでください。または、デリゲートメソッドと呼ばれることはありません。私はかつてこれを知っていました...ちょうど長い時間でした...ため息。これを再び理解するために1日無駄になりました!すべてのcllocationmanager soは私が読んだことがないので、これを育てたことがないので、ここに投稿しています。それが誰かを助けることを願っています!
どこかでロケーションマネージャーを扱っていないのですか?あなたが持っているコードは、それがDidfinishlaunchingで割り当てられていることを示しています...方法は忘れられます(まだ保持されているので、それでも機能するはずです)。
ユーザーの場所を取得しても大丈夫かどうかを尋ねる最初のポップアップさえわかりますか?
コードを「NavigationControllerにプッシュする」ViewControllerに入れます。
私のコードはです。
someviewcontroller.hで
#import <MapKit/MapKit.h>
@interface SomeViewController : UIViewController<CLLocationManagerDelegate>{
}
@property (nonatomic, strong) CLLocationManager *locationManager;
@end
someviewcontroller.mで
#import "SomeViewController.h"
@implementation SomeViewController
-(void)findLocation{
if ([CLLocationManager locationServicesEnabled]) {
if (!self.locationManager) {
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationManager.distanceFilter = kCLDistanceFilterNone;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[self.locationManager startUpdatingLocation];
}
}
}
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
NSLog(@"didUpdateLocations");
CLLocation *lo = [locations objectAtIndex:0];
NSLog(@"latitude = %f, longitude = %f", lo.coordinate.latitude, lo.coordinate.longitude);
[manager stopUpdatingHeading];
}
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
NSLog(@"didUpdateToLocation");}
-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
NSLog(@"didFailWithError");
[manager stopUpdatingLocation];
}
私が思う問題は、ARCのために、LocationManagerがリリースされることです。