Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/112.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone GPS在iOS 4上工作,但在iOS 5上不工作_Iphone_Ios - Fatal编程技术网

Iphone GPS在iOS 4上工作,但在iOS 5上不工作

Iphone GPS在iOS 4上工作,但在iOS 5上不工作,iphone,ios,Iphone,Ios,在我的AppDelegate中,我有一个属性: GPSTracking* _GPSTracker; 它负责启动位置更新。它使用singletonCLLocationManagerDelegate并如下使用: @implementation GPSTracking -(id) initWithDelegate:(MSpyAppDelegate *) _del { if (self = [super init]) { self.mainDele

在我的AppDelegate中,我有一个属性:

GPSTracking*        _GPSTracker;
它负责启动位置更新。它使用singleton
CLLocationManagerDelegate
并如下使用:

@implementation GPSTracking

-(id) initWithDelegate:(MSpyAppDelegate *) _del
{
    if (self = [super init])
    {   
        self.mainDelegate = _del;
        [[InternalGPSManager sharedInstance] setMainDelegate: self.mainDelegate];
    }
    return self;
}

//Start getting locations
- (void)startUpdatingByManager
{
    [[InternalGPSManager sharedInstance] startLocationUpdateInSeparateThread];
    self.gpsTimer = [NSTimer scheduledTimerWithTimeInterval:(gpsUpdateInterval * 60)    target:self selector:@selector(startUpdatingByManager) userInfo:nil repeats:NO] 
}
实际位置经理代表:

InternalGPSManager.h:

@interface InternalGPSManager : NSObject <CLLocationManagerDelegate>
   {
      CLLocationManager *locationManager;
   }
@property (nonatomic, retain) CLLocationManager *locationManager;
它适用于iOS 4,但不适用于iOS 5。从未调用位置委托回调。
有什么想法吗?

请幽默地告诉我,并尝试将您的代码更改为:

- (void)startLocationUpdateInSeparateThread
{
   // Not a separate thread!
   NSLog(@"Starting updates using delegate: %@", self.locationManager.delegate);
   [self.locationManager startUpdatingLocation];
}

PS:您为什么不使用ARC???

问题是您正在后台线程中启动位置更新,当您告诉位置更新开始时,该线程就会被清除

detachNewThreadSelector:toTarget:withObject:
上:

在执行过程中,将保留目标和辩论对象 拆下的螺纹,然后释放。已退出分离的线程 (使用exit类方法)一旦完成目标 执行aSelector方法

因此,只要
startLocationUpdate
完成,该线程就会被清理

然后,在location manager代表上:

委托对象的方法是从中的线程调用的 您启动了相应的位置服务。那根线 自身必须有一个活动的运行循环,如在 应用程序的主线程

因此,
CLLocationManager
希望在您启动位置更新的同一个线程(称为
startUpdatingLocation
的线程)上给您回电话,但该线程可能会在位置修复可用时消失。我还相信,你可以在一个设备/操作系统上使用代码,而不是在另一个设备/操作系统上

因此,要么:

  • 在主线程上启动位置更新。启动位置更新是一个异步操作,因此,在主(UI)线程上启动位置更新并不意味着在位置修复可用之前整个UI都会被阻止。这只意味着当位置可用时,您将在UI线程上被调用
或者

  • 让你的后台线程挂起,只要你想继续接收位置更新
要做第一件事,就这么做

[self.locationManager startUpdatingLocation];
而不是调用
startLocationUpdateInSeparateThread
。要执行第二步,请添加以下代码:

-(无效)startLocationUpdate
{ 
NSAutoreleasePool*池=[[NSAutoreleasePool alloc]init];
[self.locationManager startUpdatingLocation];
[[nsrunlop currentRunLoop]run];//防止清理此后台线程
[池释放];
} 

如果您的
CLLocationManagerDelegate
回调(用于接收位置更新)没有进行任何繁重的处理,那么我建议您完全避免使用后台线程

ARC与iOS 4.0+配合使用。现在没有理由不使用它。看,运行测试只需要几分钟,然后再报告。谢谢,我正在测试不同的解决方案。还是不走运。我想我错过了这么详细的答案!我尝试删除线程创建,但未触发委托回调。我也尝试了第二种选择,但仍然没有成功。关键的一点是,最初的解决方案可以在iOS 4上运行。我想我只是错过了一些东西…@也许,好吧,有什么悬而未决的问题,你有时会很幸运,有些东西没有尽快清理干净。操作系统版本的更改可能会破坏这一点,这并不让我感到震惊,但从一开始我就觉得这是错误的。无论如何,如果您修改了代码,但仍然无法工作,我需要看看您的代码现在是什么样子,以提供帮助。如果有点不同,我会在你问题的底部贴一个“更新”。如果有很大不同,我会提出一个新问题。祝你好运
- (void)startLocationUpdateInSeparateThread
{
   // Not a separate thread!
   NSLog(@"Starting updates using delegate: %@", self.locationManager.delegate);
   [self.locationManager startUpdatingLocation];
}