ios - 在 Xcode 5 中工作后,Xcode 中的 GPS 跟踪在 Xcode 6 中停止工作

标签 ios iphone mapkit user-tracking

我正在浏览 youtube 上的一个教程,该教程教您如何在 map View 上跟踪和追踪用户的位置。本教程附带一份代码副本,因此我下载了代码文件并在 Xcode 中打开它们。我第一次在 Xcode 中打开代码时使用的是最新的 Xcode 5。它运行得很好,可以找到并跟踪位置。大约一天后,Xcode 6 发布了,所以我将 Xcode 更新为 Xcode 6。在 Xcode 6 中打开代码文件时,应用程序无法正确执行。我收到一条错误消息...

2014-09-28 17:24:34.468 GPSTrack[1644:130866] Trying to start MapKit location updates without prompting for location authorization. Must call -[CLLocationManager requestWhenInUseAuthorization] or -[CLLocationManager requestAlwaysAuthorization] first.

在头文件GPSTrackerViewController.h中

//
//  GPSTrackViewController.h
//  GPSTrack
//
//  Created by Nick Barrowclough on 4/21/14.
//  Copyright (c) 2014 iSoftware Developers. All rights reserved.
//

#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h> //import the mapkit framework

@interface GPSTrackViewController : UIViewController <CLLocationManagerDelegate, MKMapViewDelegate, MKOverlay> {

    CLLocationManager *lm; //core lcoation manager instance

    NSMutableArray *trackPointArray; //Array to store location points

    //instaces from mapkit to draw trail on map
    MKMapRect routeRect;
    MKPolylineView* routeLineView;
    MKPolyline* routeLine;
}
- (IBAction)startTracking:(id)sender;
- (IBAction)stopTracking:(id)sender;
- (IBAction)clearTrack:(id)sender;

@property (weak, nonatomic) IBOutlet MKMapView *mapview;

@end

GPSTrackViewController.m

//
//  GPSTrackViewController.m
//  GPSTrack
//
//  Created by Nick Barrowclough on 4/21/14.
//  Copyright (c) 2014 iSoftware Developers. All rights reserved.
//

#import "GPSTrackViewController.h"

@interface GPSTrackViewController ()

@end

@implementation GPSTrackViewController

@synthesize mapview;

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    mapview.mapType = MKMapTypeHybrid;
}

- (void)viewWillAppear:(BOOL)animated {
    trackPointArray = [[NSMutableArray alloc] init];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)startTracking:(id)sender {
    //start location manager
    lm = [[CLLocationManager alloc] init];
    lm.delegate = self;
    lm.desiredAccuracy = kCLLocationAccuracyBest;
    lm.distanceFilter = kCLDistanceFilterNone;
    [lm startUpdatingLocation];

    mapview.delegate = self;
    mapview.showsUserLocation = YES;
}

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

    //get the latest location
    CLLocation *currentLocation = [locations lastObject];

    //store latest location in stored track array;
    [trackPointArray addObject:currentLocation];

    //get latest location coordinates
    CLLocationDegrees Latitude = currentLocation.coordinate.latitude;
    CLLocationDegrees Longitude = currentLocation.coordinate.longitude;
    CLLocationCoordinate2D locationCoordinates = CLLocationCoordinate2DMake(Latitude, Longitude);

    //zoom map to show users location
    MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(locationCoordinates, 1000, 1000);
    MKCoordinateRegion adjustedRegion = [mapview regionThatFits:viewRegion]; [mapview setRegion:adjustedRegion animated:YES];

    NSInteger numberOfSteps = trackPointArray.count;

    CLLocationCoordinate2D coordinates[numberOfSteps];
    for (NSInteger index = 0; index < numberOfSteps; index++) {
        CLLocation *location = [trackPointArray objectAtIndex:index];
        CLLocationCoordinate2D coordinate2 = location.coordinate;

        coordinates[index] = coordinate2;
    }

    MKPolyline *polyLine = [MKPolyline polylineWithCoordinates:coordinates count:numberOfSteps];
    [mapview addOverlay:polyLine];

    //NSLog(@"%@", trackPointArray);
}

- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay
{
    MKPolylineView *polylineView = [[MKPolylineView alloc] initWithPolyline:overlay];
    polylineView.strokeColor = [UIColor redColor];
    polylineView.lineWidth = 4.0;

    return polylineView;
}

- (IBAction)stopTracking:(id)sender {
    //reset location manager and turn off GPS
    lm = [[CLLocationManager alloc] init];
    [lm stopUpdatingLocation];
    lm = nil;

    //stop shwing user location
    mapview.showsUserLocation = NO;

    //reset array fo tracks
    trackPointArray = nil;
    trackPointArray = [[NSMutableArray alloc] init];
}

- (IBAction)clearTrack:(id)sender {
    //remove overlay on mapview
    [mapview removeOverlays: mapview.overlays];
}


@end

谁能帮我理解为什么应用程序不再运行,并给我一些建议,让我知道我需要做什么才能让它重新启动和运行。

最佳答案

在 base sdk 8 下(这是 xcode 6 使用的,我删除了 xcode 标签,因为它不是特定于 IDE 的)你必须首先请求授权并且必须有一个 plist key (一个说明你为什么需要的字符串使用 GPS)

plist key取决于你的需求是EITHER

  • NSLocationWhenInUseUsageDescription
  • NSLocationAlwaysUsageDescription

这个 SO 问题看起来不错:

IOS 8 CLLocationManager Issue (Authorization Not Working)

有关详细说明,我可以推荐(略读):

http://nevan.net/2014/09/core-location-manager-changes-in-ios-8/

关于ios - 在 Xcode 5 中工作后,Xcode 中的 GPS 跟踪在 Xcode 6 中停止工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26090350/

相关文章:

ios - 使用 NSFetchedResultsController 理解 transient 属性

iphone - AVAudioRecorder 标准化音量

iphone - 如何在 iOS 中减少 "Live Bytes"?

ios - 缩小以显示用户位置/注释

ios - 第二次显示 MapKit 失败并显示 EXC_BAD_ACCESS

android - Windows Phone 的 native iOS/android 应用程序

ios - 通过数据创建 UIImage 时崩溃

iphone - 命令/bin/sh 失败,退出代码为 1 三 20-

swift - 如何在 Swift 中的 map 上显示两个位置之间的方向?

ios - 我没有编辑的 UITableViewCell(不可见的部分)也被编辑了。