iOS 8.3 Xcode 6.3.1 中未调用核心位置委托方法

2023-11-23

我试图使用 Xcode 6.3.1 中的核心位置框架获取用户的当前位置,我做了以下事情:

  1. Added 核心位置框架 under Target-> General-> 链接的框架和库
  2. My 视图控制器.h文件如下图所示,

    #import <UIKit/UIKit.h>
    #import <CoreLocation/CoreLocation.h>
    
    @interface ViewController : UIViewController<CLLocationManagerDelegate>
    @property (weak, nonatomic) IBOutlet UILabel *lblLatitude;
    @property (weak, nonatomic) IBOutlet UILabel *lblLongitude;
    @property (weak, nonatomic) IBOutlet UILabel *lblAddress;
    @property (strong, nonatomic) CLLocationManager *locationManager;
    
    @end
    
  3. 我的ViewController.m文件如下所示,

    - (void)viewDidLoad
    {
    
    [super viewDidLoad];
    self.locationManager = [[CLLocationManager alloc] init];
    
    self.locationManager.delegate = self;
    if(IS_OS_8_OR_LATER){
        NSUInteger code = [CLLocationManager authorizationStatus];
        if (code == kCLAuthorizationStatusNotDetermined && ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)] || [self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)])) {
        if([[NSBundle mainBundle] objectForInfoDictionaryKey:@"NSLocationAlwaysUsageDescription"]){
            [self.locationManager requestAlwaysAuthorization];
        } else if([[NSBundle mainBundle] objectForInfoDictionaryKey:@"NSLocationWhenInUseUsageDescription"]) {
            [self.locationManager  requestWhenInUseAuthorization];
        } else {
            NSLog(@"Info.plist does not contain NSLocationAlwaysUsageDescription or NSLocationWhenInUseUsageDescription");
        }
    }
    }
    [self.locationManager startUpdatingLocation];
    }
    
    #pragma mark - CLLocationManagerDelegate
    
    - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
    {
        NSLog(@"didFailWithError: %@", error);
        UIAlertView *errorAlert = [[UIAlertView alloc]
                           initWithTitle:@"Error" message:@"Failed to Get Your Location" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [errorAlert show];
    }
    
    - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
    {
        NSLog(@"didUpdateToLocation: %@", newLocation);
        CLLocation *currentLocation = newLocation;
    
        if (currentLocation != nil) {
            lblLatitude.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude];
            lblLongitude.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude];
        }
    }
    @end
    

我还在 info.plist 文件中添加了以下键

  • NSLocationWhenInUseUsageDescription
  • NSLocationAlwaysUsageDescription

plist image

检查了所有给出的内容here, here, here, here,以及更多列表

那么,谁有这个问题的解决方案,请帮忙。我一整天都在寻找这个而失去了理智。


是的!找到了解决方案,这是我的整个代码和添加的内容以使其正常工作。特别感谢@MBarton 的大力帮助。还要感谢@Vinh Nguyen 投入宝贵的时间来解决我的问题。

Added 核心位置框架 under 目标 -> 常规 -> 链接的框架和库

添加到 .plist 文件中

NSLocationAlwaysUsageDescription

看截图:

plist screenshot

In my ViewController.h

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <MapKit/MKAnnotation.h>

// #define IS_OS_8_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)

@interface ViewController : UIViewController  <MKMapViewDelegate,  CLLocationManagerDelegate>
{
    __weak IBOutlet UINavigationItem *navigationItem;
}

 @property (weak, nonatomic) IBOutlet MKMapView *mapView;
 @property(nonatomic, retain) CLLocationManager *locationManager;

@end

Then in ViewController.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize mapView;

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

    [self setUpMap];
}

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

-(void)setUpMap
{
    mapView.delegate = self;
    self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.delegate = self;
#ifdef __IPHONE_8_0
   // if(IS_OS_8_OR_LATER) {
    if ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) { 
        // Use one or the other, not both. Depending on what you put in info.plist
        [self.locationManager requestAlwaysAuthorization];
    }
#endif
    [self.locationManager startUpdatingLocation];

    mapView.showsUserLocation = YES;
    [mapView setMapType:MKMapTypeStandard];
    [mapView setZoomEnabled:YES];
    [mapView setScrollEnabled:YES];
}

-(void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:YES];

    self.locationManager.distanceFilter = kCLDistanceFilterNone;
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    [self.locationManager startUpdatingLocation];
    NSLog(@"%@", [self deviceLocation]);

    //View Area
    MKCoordinateRegion region = { { 0.0, 0.0 }, { 0.0, 0.0 } };
    region.center.latitude = self.locationManager.location.coordinate.latitude;
    region.center.longitude = self.locationManager.location.coordinate.longitude;
    region.span.longitudeDelta = 0.005f;
    region.span.longitudeDelta = 0.005f;
    [mapView setRegion:region animated:YES];

}

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(userLocation.coordinate, 800, 800);
    [self.mapView setRegion:[self.mapView regionThatFits:region] animated:YES];
}
- (NSString *)deviceLocation {
    return [NSString stringWithFormat:@"latitude: %f longitude: %f", self.locationManager.location.coordinate.latitude, self.locationManager.location.coordinate.longitude];
}

噗……!过去 5 天以来,经过与许多代码的斗争,得到了解决方案......

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

iOS 8.3 Xcode 6.3.1 中未调用核心位置委托方法 的相关文章

  • iOS模型层通知Controller对象

    https developer apple com library archive documentation General Conceptual DevPedia CocoaCore MVC html https developer a
  • NSAttributedString 的 AutoLayout 行高计算错误

    我的应用程序从 API 中提取 HTML 将其转换为NSAttributedString 为了允许可点击的链接 并将其写入自动布局表中的一行 问题是 每当我调用这种类型的单元格时 高度都会被错误计算并且内容会被截断 我尝试了不同的行高计算实
  • 在 iOS 上,边距、边缘插入、内容插入、对齐矩形、布局边距、锚点...之间有什么区别?

    iOS 社区中似乎有几种不同的选项 术语 人们在布局方面使用 例如 UIEdgeInsets 是一种类型 但有时我听到 读到 设置插图 或布局边距与布局指南 我总是能够找到有效的选择 但我永远不确定我是否使用了正确的工具来完成这项工作 有人
  • 渐变方向从左到右

    我完全被这个问题难住了 它应该如此简单 以至于让我发疯 我正在使用这个苹果反射教程 苹果反射示例 http developer apple com library ios samplecode Reflection Listings MyV
  • Google Cardboard - Cardboard VR 套件有 iPhone / iOS 入门项目吗?

    我正在看Google Cardboard 套件 一种廉价的 VR 设置 https developers google com cardboard 使用 Android 设备玩 3d VR 游戏 我看到他们有一个 Android 演示项目
  • iOS-将图像转为视频时,CVPixelBufferCreate内存无法正确释放

    我正在将图像制作成视频 但总是因为内存警告而崩溃 分配太多CVPixelBufferCreate 我不知道如何正确处理 我看过很多类似的主题 但没有一个能解决我的问题 这是我的代码 void writeImagesArray NSArray
  • 有没有办法从 Instruments (Xcode) 的命令行实例中删除授权提示?

    我目前正在通过 bash 脚本使用 Instruments 来启动命令行界面以启动自动化插件的运行 在 4 2 中 这工作得很好 但是随着升级到 Xcode 4 3 我现在被提示需要授权用户 分析其他进程 即使授予了正确的凭据 也不会真正对
  • 如何在 UITextView 中禁用放大功能

    我想摆脱 UITextView 中的放大和文本选择 但我需要电话号码 链接和地址检测器 我在用 void addGestureRecognizer UIGestureRecognizer gestureRecognizer if gestu
  • 定位精度定义 - iOS

    iOS 上返回的 准确性 或 不确定性 的统计意图是什么 即使是近似值 例如 Android 文档对其返回的精度数字进行了解释 从这个意义上讲 它大约是一个标准差 我们将准确度定义为 68 置信度的半径 换句话说 如果 您以该位置的纬度和经
  • UIAlertAction 处理程序在延迟后运行

    我正在尝试将 UIAlertViews 更改为 UIAlertControllers 我为此设置了这个操作 UIAlertAction undoStopAction UIAlertAction actionWithTitle Undo St
  • 如何计算CLLocationDistance的中心坐标

    我想计算我的位置和一些注释之间的中心点 到目前为止我已经这样做了 CLLocation myLoc self locMgr location MKPointAnnotation middleAnnotation locationV anno
  • Objective C 宏附加到字符串

    我认为这是一件非常简单的事情 但由于我是 iOS 开发和 Objective C 的新手 所以我无法弄清楚 define RESTFUL PATH PREFIX https gogch com gch restful define LOGI
  • 在后台运行 URL 请求

    我想在一定的时间间隔内发出 url 请求 例如 每 10 分钟应用程序应该发出一次 url 调用并获取一些 json 数据 应用程序在后台运行时应该能够执行此操作 这可以做到吗 如果是这样 这是否违反 Apple 服务条款 有什么限制吗 i
  • 如何将 SCNPlane 颜色更改为透明颜色

    我正在开发一个 ARKit 项目 在水平面上点击时需要波纹动画效果 为此 我采用了 UIView 对象并将其作为 SCNPlane 对象材料的内容传递 我已将波纹动画添加到 UIView 对象 一切正常 但我无法将 SCNPlane 颜色更
  • UIView 周围的虚线边框

    如何在周围添加虚线边框UIView 像这样的东西 如果您喜欢子层 还有另一种方法 在您的自定义视图的 init 中 输入以下内容 border 是 ivar border CAShapeLayer layer border strokeCo
  • 带有图像的 UITableView 滚动非常慢[重复]

    这个问题在这里已经有答案了 可能的重复 带图像的表格视图 加载和滚动缓慢 https stackoverflow com questions 4071497 table view with images slow load and scro
  • UIImageWriteToSavedPhotosAlbum 选择器语法问题

    努力让 UIImageWriteToSavedPhotosAlbum 快速工作https developer apple com library ios documentation UIKit Reference UIKitFunction
  • 游戏中心玩家显示名称在沙盒中始终为“我”

    我使用用户的游戏中心显示名称和玩家 ID 来维护他们在我的服务器上的个人资料 当我进行测试时 一切似乎都正确执行 但我的沙箱帐户的用户显示名称显示为 Me 而不是附加到我的帐户的显示名称 Billybobbo 这应该在沙盒模式下发生吗 Co
  • 使用自定义格式将字符串转换为 NSDate [重复]

    这个问题在这里已经有答案了 可能的重复 NSString 到 NSDate https stackoverflow com questions 1353081 nsstring to nsdate iPhone 如何将 yyyyMMddTh
  • iOS 11 特定设置部分的 URL 方案停止工作

    我的应用程序使用 URL 方案将用户直接带到 设置 常规 关于 部分 以下 URL 在 10 3 x 中工作正常 应用程序首选项 根 常规 路径 关于 然而 这个 URL 方案在 iOS 11 GM 版本中不再有效 它仅启动 设置 应用程序

随机推荐

  • AngularJS ng-view 不工作

    所以我遵循了这个指南 http viralpatel net blogs angularjs routing and views tutorial with example 但是当我试图改变观点时什么也没有发生 有人知道我做错了什么吗 这是
  • Laravel 分组集合返回对象而不是数组

    我有以下查询 outings Outing all gt groupBy function item return Carbon parse item start gt format m d Y return response gt jso
  • std::thread 导致 DLLMain 死锁

    所以 这就是我要说的 std 很复杂 在VS2013中这个简单的程序会导致死锁 include
  • 更改 Flexdashboard 中各个文本部分的字体大小

    我正在使用 flexdashboard 创建报告 并且我只想更改页面一部分的字体大小 我觉得我可以通过添加 CSS 类来做到这一点 但我找不到如何在 R markdown 代码中指定类名 有任何想法吗 您可以将 CSS 直接添加到 Rmar
  • hash_map是STL的一部分吗?

    简单的问题 hash map 是 STL 的一部分吗 The STL has hash map 但是 C 标准库does not Due to 一个常见的误解 您可能会将 C 标准库视为 STL 或者将 C 标准库的工具链实现的部分视为 S
  • Win 7 DllImport C# 奇怪的错误,对内存位置的访问无效?

    我正在使用 DllImport 从 C 应用程序访问 C dll 中的某些函数 该代码在我的开发笔记本电脑 Windows 7 64 位 上运行良好 dll 本身是 32 位 因此我以 32 位运行托管 dll 的进程 并且运行良好 但是
  • 将 QSlider 移动到鼠标点击位置

    我有一个 QSlider 当用户按下鼠标左键时 我想将其移动到鼠标光标的位置 我一直在四处寻找 但找不到任何最近可以解决我的问题的东西 这是我的滑块 我希望能够单击使滑块跳转到鼠标单击的位置 我可以拖动滑块 但我希望能够单击 我测试了单击
  • 日语 ASCII 代码

    在哪里可以获得与日语汉字 平假名和片假名字符对应的 ASCII 代码列表 我正在做一个java函数和Javascript来确定它是否是日语字符 它的ASCII码范围是多少 ASCII代表美国信息交换标准代码 仅包含 128 个字符 并非所有
  • 在 Eclipse 中重命名访问器/修改器方法?

    当他们获取 设置的变量被重构 gt 重命名 Eclipse 3 4 时 有什么方法可以自动重命名访问器 修改器 1 当您对变量选择 重构 gt 重命名 时 Eclipse 会提示您在 内联 框中输入新名称 在它的正下方 有一条帮助消息 旁边
  • 使用 setCompoundDrawables 进行 EditText 时计算图像大小

    当我添加如下图标时 etComment EditText findViewById R id et comment Drawable img getResources getDrawable R drawable warning etCom
  • 如何在Play Framework中定义任意任务? (如红宝石耙子)

    如何在Play Framework中定义任意任务 我的意思是任务从命令行运行 类似于 ruby rake 我知道 ant 工具 但正在寻找更好的替代方案 对于 Play 2 您可以按照此处的文档使用 SBT 创建新任务 http www s
  • 我可以在不使用 HTTPS 连接的情况下使用 SSL 证书吗?

    我有点困惑 如果 SSL 证书有助于识别您已连接到受信任的服务器 那么为什么需要使用加密 HTTPS 连接呢 我可以使用 SSL 证书进行 HTTP 连接吗 这里有一个误解 证书不是 SSL 使用证书的是SSL 但证书是在SSL之前诞生的
  • 沿弧线对 UIView 进行动画处理

    我希望沿着图中所示的弧线对视图进行动画处理 从位置 1 到位置 2 实际发生的情况是 视图动画描述的是一个完整的圆而不是圆弧 我的问题是 我应该使用CGPath添加弧 or CGPath添加圆弧到点 我需要使用吗CGPath移动到点是否描述
  • RESTful删除策略

    假设我有一个资源 在调用删除时可以有两种不同的行为 资源被删除 资源被移至回收站 如何以符合 REST 的方式对其进行建模 我想到了以下解决方案 DELETE myresource 将资源移至回收站 默认行为 DELETE myresour
  • 检查 PowerShell 中每行的第一个字符是否有特定值

    我正在读取包含特定格式数字的文本文件 我想弄清楚该行的第一个字符是 6 还是 4 并将整行存储在数组中以供以后使用 因此 如果该行以 6 开头 则将整行添加到 SixArray 中 如果该行以 4 开头 则将整行添加到 fourArray
  • 核心运动错误102是什么意思?

    我使用 Core Motion 的传感器融合来获取北向运动更新 motionManager startDeviceMotionUpdatesUsingReferenceFrame CMAttitudeReferenceFrameXTrueN
  • 如何将 openssl 添加到 swift 项目

    我正在学习如何向我的 iOS OSX 项目添加应用内购买收据验证 有一个很好的概述hereWWDC14 有关于这个主题的精彩视频 示例代码很多 但每个人都跳过一步 如何导入 openSSL 标头 swift 编译器抱怨没有这样的模块 imp
  • 在一个 SQL 查询中合并两个表并使日期值唯一

    我有以下两个表 您也可以在 SQL fiddle 中找到它们here CREATE TABLE Inbound Inbound Date DATE Product TEXT InboundType TEXT Quantity VARCHAR
  • 对于每个 int x: x+1 > x .... 这总是正确的吗?

    我刚刚开始在学校学习 C 我正在努力掌握基本概念 我们的作业有一个问题 对于每一个int x x 1 gt x 判断正确与否 正确则给出推理 错误则给出反例 我很困惑 因为我们被告知 int 类型是 32 位 这基本上意味着整数是二进制格式
  • iOS 8.3 Xcode 6.3.1 中未调用核心位置委托方法

    我试图使用 Xcode 6 3 1 中的核心位置框架获取用户的当前位置 我做了以下事情 Added 核心位置框架 under Target gt General gt 链接的框架和库 My 视图控制器 h文件如下图所示 import