IOS 8 CLLocationManager 问题(授权不起作用)

2024-05-13

#import "MyLocationViewController.h"
#define NSLog(FORMAT, ...) printf("%s\n", [[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String]);

@interface MyLocationViewController ()

@end

@implementation MyLocationViewController

{
    CLLocationManager *locationManager;
}

- (void)requestAlwaysAuthorization
{
    [locationManager requestAlwaysAuthorization];
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.
    self.mapView.showsUserLocation = YES;
    self.mapView.delegate = self;
    locationManager = [[CLLocationManager alloc] init];

}

- (IBAction)unwindToMap:(UIStoryboardSegue *)segue
{

}

- (IBAction)getCurrentLocation:(id)sender {
    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    [locationManager startUpdatingLocation];
}

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
    MKCoordinateRegion mapRegion;
    mapRegion.center = mapView.userLocation.coordinate;
    mapRegion.span.latitudeDelta = 0.001;
    mapRegion.span.longitudeDelta = 0.001;




    CLLocationCoordinate2D location = userLocation.coordinate;
    float lat = location.latitude;
    float lng = location.longitude;

    NSDictionary *locationDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                        [NSNumber numberWithFloat:lat] , @"Latitude",
                                        [NSNumber numberWithFloat:lng], @"Longitude", nil];

    NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                locationDictionary, @"Location_A",
                                nil];
    NSError *error;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dictionary options:NSJSONWritingPrettyPrinted error:&error];

    NSString *str = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    NSLog(@"%@",str);



    if (mapView.userLocation != nil)
    {
        _longitudeLabel.text = [NSString stringWithFormat:@"%.8f", mapView.userLocation.coordinate.longitude];
        _latitudeLabel.text = [NSString stringWithFormat:@"%.8f", mapView.userLocation.coordinate.latitude];
    }

    [mapView setRegion:mapRegion animated: YES];

}


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

@end

这是我的代码。

现在,我知道我需要使用 NSLocationAlwaysUsageDescription 键编辑 info.plist 文件(我这样做了),并且我添加了一个描述字符串。

但是,我在实现授权部分时遇到问题,因为错误仍然如下所示:

Trying to start MapKit location updates without prompting for location authorization. Must call -[CLLocationManager requestWhenInUseAuthorization] or -[CLLocationManager requestAlwaysAuthorization] first.

我已经阅读了 CLLocationManager 的 Apple IOS 8 文档,但不知何故我的代码无法工作。

有人可以帮助我,通过查看我的代码中需要修改的位置以使其正常工作,从而消除上述错误消息吗?

Thanks!


你不应该打开MKMapView's showsUserLocation在用户授权您的应用程序使用位置服务之前。

你可以实施CLLocationManagerDelegate's locationManager:didChangeAuthorizationStatus:方法并开启showsUserLocation像这样:

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {  
    if (status == kCLAuthorizationStatusAuthorizedAlways || status == kCLAuthorizationStatusAuthorizedWhenInUse) {
        self.mapView.showsUserLocation = YES;
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

IOS 8 CLLocationManager 问题(授权不起作用) 的相关文章

  • 我可以在 Swift 3 项目中使用 Swift 2.3 框架吗?

    在我的项目中 我将所有私有 swift 2 3 文件迁移到 swift 3 我想使用用 swift 2 3 编写的遗留框架 直到它们有 swift 3 版本 我尝试添加 使用旧版 Swift 版本 是 清除 构建我的项目 但我仍然遇到一些麻
  • iOS模型层通知Controller对象

    https developer apple com library archive documentation General Conceptual DevPedia CocoaCore MVC html https developer a
  • 如何跟踪 SwiftUI 应用程序中的所有触摸

    我正在尝试在 SwiftUI 应用程序中实现锁屏 我需要跟踪每个事件才能重新启动锁定计时器 在 UIKit 应用程序中 我使用了这种方法 重写 UIApplication 它允许了解应用程序中的任何事件 override func send
  • 如何让 Chrome Cast 在 iOS 后台运行?

    我有一个简单的问题 当您进入 iPhone 的主屏幕并且不退出应用程序时 chrome Cast 设备会停止在屏幕上播放视频 当应用程序在后台运行时 我如何才能保持播放状态 如果您有一个视频应用程序并且它在投射设备中运行 您可能需要以下行为
  • 如何在UiView中添加UIImagePickerController

    如何在TabBarApplication中的UiView中添加UIImagePickerController 无论您是否在选项卡中 此代码都会进入您视图的 ViewController 类 当您需要时创建一个选择器 UIImagePicke
  • 将UIWebView显示的PDF保存到本地

    我有一个UIViewController与UIWebView显示一个 pdf 文件 具体取决于之前单击的行UITableView 现在我想添加一个按钮 供用户在本地保存此 pdf 文件以供离线使用 然后还有第二个UITableView它应该
  • 渐变方向从左到右

    我完全被这个问题难住了 它应该如此简单 以至于让我发疯 我正在使用这个苹果反射教程 苹果反射示例 http developer apple com library ios samplecode Reflection Listings MyV
  • iOS-将图像转为视频时,CVPixelBufferCreate内存无法正确释放

    我正在将图像制作成视频 但总是因为内存警告而崩溃 分配太多CVPixelBufferCreate 我不知道如何正确处理 我看过很多类似的主题 但没有一个能解决我的问题 这是我的代码 void writeImagesArray NSArray
  • SpriteKit - 对多个 SKNode 上运行的多个 SKAction 进行排序

    我非常了解 SKAction API 但在多个节点上运行顺序代码时我无法获得良好的代码 这是示例代码 简化 import SpriteKit class GameScene SKScene weak var node1 SKNode wea
  • UILongPressGestureRecognizer 不起作用,但将其替换为 UITapGestureRecognizer 效果很好。为什么?

    我有一个UIImageView with a UILongPressGestureRecognizer无论我如何配置手势识别器 似乎都不会检测到长按手势 但是 如果我把它换成UITapGestureRecognizer that工作得很好
  • “EXC_BAD_ACCESS”与“分段错误”。两者实际上相同吗?

    在我的前几个虚拟应用程序 用于学习时练习 中 我遇到了很多EXC BAD ACCESS 这以某种方式告诉我错误访问是 您正在触摸 访问一个不应该的对象 因为它尚未分配或释放 或者只是您无权访问它 查看此示例代码 该代码存在访问错误问题 因为
  • iOS - 在 UITabBar 上方获取所需的阴影

    我试图让我的标签栏阴影看起来像这张图片中看到的那样 这样做的最佳方法是什么 我正在使用 Objective C Thanks 您可以使用以下代码为任何 UI 对象提供阴影 tabBar layer shadowOffset CGSize w
  • iOS5 和 Facebook API

    我想将我的应用程序与 Facebook 集成并从 github 下载 Facebook ios sdk 但是 该 sdk 适用于 Xcode 4 0 代码库 我正在使用 Xcode 4 2 for iOS 5 beta 5 应用程序 是否有
  • UIView 周围的虚线边框

    如何在周围添加虚线边框UIView 像这样的东西 如果您喜欢子层 还有另一种方法 在您的自定义视图的 init 中 输入以下内容 border 是 ivar border CAShapeLayer layer border strokeCo
  • 视频中的图像/文本叠加 swift

    我正在使用 swift 在视频中使用图像叠加来实现水印效果 我正在使用AVFoundation为此 但不知何故我没有成功 以下是我的覆盖图像 文本的代码 let path NSBundle mainBundle pathForResourc
  • 如何在 iOS 6 中强制 UIViewController 为纵向

    As the ShouldAutorotateToInterfaceOrientation在 iOS 6 中已弃用 我用它来强制特定视图仅肖像 在 iOS 6 中执行此操作的正确方法是什么 这仅适用于我的应用程序的一个区域 所有其他视图都可
  • 在 Swift 中,如何为具有自动布局的 UIView 制作动画,就像页面滑入一样?

    我尝试创建一个 UIView 来表示一个大小与设备屏幕相同的页面 由于该应用程序支持方向 因此我使用 AutoLayout 来构建它 它工作正常 直到我尝试将页面动画化以从右侧滑入 经过一番研究后 我能想到的最好的办法是 myView UI
  • 如何在多个视图中显示相同的导航栏?

    我可以为一个视图重现以下导航栏 但是 一旦我单击按钮后移至下一个视图 我就会丢失最右边的两个图标 搜索 个人资料 据我所知 从故事板设置导航项目通常是按视图进行的 我可以为每个视图复制这些项目 但我想知道是否有更好的方法来完成一次 是否有教
  • 如何删除以前的 ViewController

    我是一名学生 对编程还很陌生 我正在尝试在业余时间学习 Objective C Swift 我使用 spriteKit 和 swift 制作了一个游戏 有多个菜单 场景 我正在尝试从一个视图控制器转换到另一个视图控制器 为此 我使用了以下代
  • Swift:UICollectionViewCell didSelectItemAtIndexPath 更改背景颜色

    我可以轻松更改单元格的背景颜色CellForItemAtIndexPath method func collectionView collectionView UICollectionView cellForItemAtIndexPath

随机推荐