Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.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
Ios 使用位置管理器_Ios_Objective C_Ios6 - Fatal编程技术网

Ios 使用位置管理器

Ios 使用位置管理器,ios,objective-c,ios6,Ios,Objective C,Ios6,我想做以下几件事 跟踪应用程序代理文件中后台的位置 跟踪某人在视图控制器中的位置 我是这样做的: 在APPDelegate中: 在ApplicationIDFinishLaunching中: if ([CLLocationManager locationServicesEnabled]) { self.locationManager = [[CLLocationManager alloc] init]; self.locationManager.delegate = self

我想做以下几件事

  • 跟踪应用程序代理文件中后台的位置
  • 跟踪某人在视图控制器中的位置
我是这样做的:

在APPDelegate中:

在ApplicationIDFinishLaunching中:

if ([CLLocationManager locationServicesEnabled])
{
    self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.delegate = self;
    _startLocation = nil;
    self.locationManager.desiredAccuracy=kCLLocationAccuracyBest;
    self.locationManager.distanceFilter=500;
}


- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
    // Set Property
    self.myLocation = newLocation;

    BOOL isInBackground = NO;
    if ([UIApplication sharedApplication].applicationState == UIApplicationStateBackground)
    {
        isInBackground = YES;
    }

    // Handle location updates as normal, code omitted for brevity.
    // The omitted code should determine whether to reject the location update for being too
    // old, too close to the previous one, too inaccurate and so forth according to your own
    // application design.

    if (isInBackground)
    {
        [self sendBackgroundLocationToServer:newLocation];
    }

    if(newLocation.horizontalAccuracy <= 100.0f)
    {
        [self.locationManager stopMonitoringSignificantLocationChanges];
    }
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    [self.locationManager stopUpdatingLocation];
    [self.locationManager startMonitoringSignificantLocationChanges];

    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    if(self.locationManager == nil)
    {
        self.locationManager = [[CLLocationManager alloc] init];
    }
    [self.locationManager stopMonitoringSignificantLocationChanges];
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application
{
    self.locationManager = nil;
    [self.locationManager stopUpdatingLocation];
    [self.locationManager stopMonitoringSignificantLocationChanges];
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
if([CLLocationManager LocationServiceEnabled])
{
self.locationManager=[[CLLocationManager alloc]init];
self.locationManager.delegate=self;
_零位=零;
self.locationManager.desiredAccuracy=KCallocationAccuracyBest;
self.locationManager.distanceFilter=500;
}
-(void)locationManager:(CLLocationManager*)manager didUpdateToLocation:(CLLocation*)newLocation fromLocation:(CLLocation*)oldLocation{
//集合属性
self.myLocation=新位置;
BOOL-isInBackground=否;
if([UIApplication sharedApplication].applicationState==UIApplicationStateBackground)
{
isInBackground=是;
}
//按正常方式处理位置更新,为简洁起见省略代码。
//省略的代码应该决定是否拒绝位置更新
//旧的,太接近前一个,太不准确等根据您自己的
//应用程序设计。
如果(isInBackground)
{
[self-sendBackgroundLocationToServer:newLocation];
}

如果(newLocation.horizontalAccuracy从设计角度来看,我发现最好将位置管理器包装在一个单例类()中。您在这里做的是:

if ([CLLocationManager locationServicesEnabled])
{
    PLAppDelegate *appDelegate = (PLAppDelegate *) [[UIApplication sharedApplication] delegate];

    if(appDelegate.locationManager == nil)
    {
        self.locationManager = [[CLLocationManager alloc] init];
    }
    else {
        self.locationManager = appDelegate.locationManager;
    }

    self.locationManager.delegate = self;
    [self.locationManager startUpdatingLocation];
}
…非常混乱:您可能正在复制location manager对象或只是引用它(在没有看到头文件的情况下,代码不清楚那里发生了什么),通常不应该切换location manager的委托

使用singleton类将为您提供一个单独的位置来处理与位置相关的事情,并且您的视图控制器和其他类可以随时请求这些数据。或者,您可以使用委托模式存储对每个位置感知类(例如视图控制器、应用程序委托)的引用在单例中,并在收到回调时广播到这些类,例如
locationManager:didUpdateToLocation


我希望这会有所帮助-看看我添加的链接,看看我的意思:在这里使用singleton模式是最完美的,因为您在任何时候都只希望使用一个位置数据源。

从设计角度来看,我发现最好将位置管理器包装在singleton类中().你在这里做什么:

if ([CLLocationManager locationServicesEnabled])
{
    PLAppDelegate *appDelegate = (PLAppDelegate *) [[UIApplication sharedApplication] delegate];

    if(appDelegate.locationManager == nil)
    {
        self.locationManager = [[CLLocationManager alloc] init];
    }
    else {
        self.locationManager = appDelegate.locationManager;
    }

    self.locationManager.delegate = self;
    [self.locationManager startUpdatingLocation];
}
…非常混乱:您可能正在复制location manager对象或只是引用它(在没有看到头文件的情况下,代码不清楚那里发生了什么),通常不应该切换location manager的委托

使用singleton类将为您提供一个单独的位置来处理与位置相关的事情,并且您的视图控制器和其他类可以随时请求这些数据。或者,您可以使用委托模式存储对每个位置感知类(例如视图控制器、应用程序委托)的引用在单例中,并在收到回调时广播到这些类,例如
locationManager:didUpdateToLocation


我希望这能有所帮助-看看我添加的链接,看看我的意思:在这里使用单例模式是最完美的,因为你在任何时候都只需要使用一个位置数据源。

如果你在iphone上按中心按钮关闭应用程序,那么这个方法就会被调用

- (void)applicationWillEnterForeground:(UIApplication *)application

如果你在iphone中按下中间按钮关闭应用程序,那么这个方法就会被调用

- (void)applicationWillEnterForeground:(UIApplication *)application

您的代码看起来不错,但是如果您注册了位置更改,它将在后台保持活动状态。如果您想暂停位置更改,有几种方法,我通常在我的
AppDelegate
中停止它们

- (void)applicationDidEnterBackground:(UIApplication *)application
{
   NSLog(@"Application entering background");
   CLLocationManager *locMan = [[CLLocationManager alloc] init];
   [locMan stopMonitoringSignificantLocationChanges];
   [locMan stopUpdatingLocation];
}


- (void)applicationDidBecomeActive:(UIApplication *)application
{
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    CLLocationManager *locMan = [[CLLocationManager alloc] init];
    [locMan startMonitoringSignificantLocationChanges];
}

您的代码看起来不错,但是如果您注册了位置更改,它将在后台保持活动状态。如果您想暂停位置更改,有几种方法,我通常在我的
AppDelegate
中停止它们

- (void)applicationDidEnterBackground:(UIApplication *)application
{
   NSLog(@"Application entering background");
   CLLocationManager *locMan = [[CLLocationManager alloc] init];
   [locMan stopMonitoringSignificantLocationChanges];
   [locMan stopUpdatingLocation];
}


- (void)applicationDidBecomeActive:(UIApplication *)application
{
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    CLLocationManager *locMan = [[CLLocationManager alloc] init];
    [locMan startMonitoringSignificantLocationChanges];
}

此外,您还可以在应用程序委派中检测应用程序何时进入后台,然后停止监视位置更新。请参阅此处有关应用程序状态更改的信息:您可以使用应用程序委派方法
willResignActive
等来检测应用程序何时进入后台。您可以阅读更多关于此处的应用程序状态更改:我正在关注您的教程:您将[self.locationManager startUpdatingLocation]放在哪里?您可以将其放在您知道要开始接收位置更新的任何位置。例如,您可以在
[[self alloc]init]之后将其放在教程中的
sharedInstance
方法中
,这样它在第一次请求单例时就开始接收更新。此外,您可以在应用程序委托中检测应用程序何时进入后台,然后停止监视位置更新。请参阅此处有关应用程序状态更改的信息:您可以使用应用程序委托方法
willResignActive
等。您可以在此处阅读有关应用程序状态更改的更多信息:我在这里学习您的教程:您将[self.locationManager startUpdatingLocation]放在哪里您可以将它放在任何您希望开始接收位置更新的地方。例如,您可以将它放在教程中
[[self alloc]init]之后的
sharedInstance
方法中
这样它在第一次请求singleton时就开始接收更新。你是说ApplicationWilenterBackground吗?我想你的方法是相反的。ApplicationWilenterForeground在应用程序从后台返回之前就被调用了。你是说ApplicationWilenterBackground吗?我想你的方法是相反的.applicationWillEnter