04 delegation and core location

34
Delegation Core Location 范圣刚,[email protected] www.tfan.org

Upload: tom-fan

Post on 28-Jun-2015

266 views

Category:

Technology


0 download

DESCRIPTION

delegation 和 Core Location

TRANSCRIPT

Page 2: 04 Delegation and Core Location

• delegation

• Core Location framework

• Xcode debugger

•Whereami

•找到设备当前的位置•在⼀一个可交互的地图上显⽰示•允许⽤用户使⽤用⼤大头针和标题来标记当前位置

Page 3: 04 Delegation and Core Location

项⺫⽬目,⺫⽬目标和框架

Page 4: 04 Delegation and Core Location

Project

•⼀一个⽂文件,包含⼀一个到其他⽂文件的引⽤用的列表•源代码⽂文件•资源•框架•类库• 配置

•扩展名 = xcodeproj

Page 5: 04 Delegation and Core Location

Target

•⼀一个 project 总是具有⾄至少⼀一个 target

• target 使⽤用位于 project 中的⽂文件来构建某⼀一特定产品

• Build and run 的是target,⽽而不是 project

• target 构建出来的产出⼀一般是⼀一个应⽤用,虽然也可以是⼀一个编译库或者⼀一个单元测试 bundle。

•创建⼀一个新的项⺫⽬目并且选择模板后,Xcode ⾃自动⽣生成⼀一个 iOS application target 并且命名为 Whereami

Page 6: 04 Delegation and Core Location

Build Phases

Page 7: 04 Delegation and Core Location

Frameworks

•可以添加到 Target 的相关类的集合

• Cocoa Touch 是框架的集合

•使⽤用 frameworks 组织 Cocoa Touch 的好处

•已经链接到 target 的 frameworks:• UIKit - iOS ⽤用户界⾯面

• Foundation - NSString, NSArray ...

• Core Graphics - 图形库

Page 8: 04 Delegation and Core Location

添加 Core Location 框架

Page 9: 04 Delegation and Core Location

Core Location

•包含使应⽤用程序可以⽤用来确定设备的地理位置的类

•针对所有类型的 iOS 设备,我们写的 Core Location 代码是⼀一致的

•除了导⼊入 Core Location framework 之外,我们还需要引⼊入 framework 的头⽂文件

•框架的头⽂文件名称总是 框架名 + .h

Page 10: 04 Delegation and Core Location

WhereamiViewController.h

•导⼊入 CoreLocation.h

•增加⼀一个 CLLocationManager 实例变量

#import <UIKit/UIKit.h>// 导⼊入 Core Location framework 的头⽂文件#import <CoreLocation/CoreLocation.h>

@interface WhereamiViewController : UIViewController{ CLLocationManager *locationManager;}@end

Page 11: 04 Delegation and Core Location

CLLocationManager

•和设备上的位置硬件交互的类•具有⼀一些指定其⾏行为的属性

Page 12: 04 Delegation and Core Location

desiredAccuracy

•告诉 location manager 位置发现的精度应该是多少

•位置精度,所需的时间量和电池续航时间之间的权衡

•精度最终依赖于•设备类型•基站发射塔和卫星的有效性•已知的⽆无线接⼊入点的有效性

Page 13: 04 Delegation and Core Location

实例化并启动locationManager

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{ self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // 创建 location manager 对象 locationManager = [[[CLLocationManager] alloc] init]; // 设置最⾼高精度(不管所需时间和电池电量消耗) [locationManager setDesiredAccuracy:kCLLocationAccuracyBest]; // ⽴立即开始查找位置 [locationManager startUpdatingLocation]; }}

Page 14: 04 Delegation and Core Location

从 CLLocationManager 接收更新• location manager 已经开始⼯工作,程序如何从

location manager 获取数据?• locationManager:didUpdateLocations: -> delegate

•什么对象是 location manager 的 delegate?

•每个 CLLocationManager 都有 delegate 属性,我们可以设置这个属性指向我们希望⽤用于接收“location found” 消息的对象

•对于 Whereami, 我们使⽤用WhereamiViewController

Page 15: 04 Delegation and Core Location

Whereami 对象图

Page 16: 04 Delegation and Core Location

CLLocation

Page 17: 04 Delegation and Core Location

实现两个⽅方法•因为 CLLocationManager 发送 locationManager:didUpdateLocations: 消息给它的 delegate,因此我们必须在 WhereamiViewController.m 中实现这个⽅方法

•同时我们也需要知道 CLLocationManager 是否查找位置失败,以及为什么失败。

•失败的时候,CLLocationManager 会发送 locationManager:didFailWithError: 消息,我们同样要实现这个⽅方法。

Page 18: 04 Delegation and Core Location

模拟⼀一个位置

Page 19: 04 Delegation and Core Location

委托•委托是针对回调的⾯面向对象⽅方法•回调是提前提供给事件的⼀一个函数,并且在每次事件发⽣生时都会被调⽤用

•有些对象需要进⾏行多次回调,但是没有内置的⽅方法可以让两个或多个回调函数协作和共享信息

•提供⼀一个单独的 delegate 来接收⼀一个特定对象的所有事件消息,这个 delegate 对象就可以存储,操作和传递相关消息

Page 20: 04 Delegation and Core Location

delegate 和 target-action

• target-action

• 当特定事件发⽣生时发送⼀一个 action message 给 target object

• 对于每⼀一个不同的事件,我们必须创建⼀一个新的 target-action paire(像tap, double tap, long press)

• delegate

• 只要设置 delegate ⼀一次,就可以为很多不同的事件给它发送消息

• 在 delegate 中实现对应的⽅方法

•在 target-action pair ⾥里⾯面,action message 可以是任意消息;delegation 的对象只能发送位于protocol 中的delegate essage

Page 21: 04 Delegation and Core Location

协议(Protocols)•对于每⼀一个可以具有 delegate 的对象,都有⼀一个对应的protocol,protocol 声明了对象可以发送给它的 delegate 的所有消息

•当⼀一个类实现了来⾃自⼀一个 protocol 的⽅方法,就被称作 conform to(遵守) 这个 protocol

Page 22: 04 Delegation and Core Location

@protocol CLLocationManagerDelegate<NSObject>

@optional- (void)locationManager:(CLLocationManager *)manager! didUpdateToLocation:(CLLocation *)newLocation! ! fromLocation:(CLLocation *)oldLocation __OSX_AVAILABLE_BUT_DEPRECATED(__MAC_10_6, __MAC_NA, __IPHONE_2_0, __IPHONE_6_0);

- (void)locationManager:(CLLocationManager *)manager! didUpdateLocations:(NSArray *)locations __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_6_0);

- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_0);

- (void)locationManager:(CLLocationManager *)manager! didEnterRegion:(CLRegion *)region __OSX_AVAILABLE_STARTING(__MAC_10_7,__IPHONE_4_0);

- (void)locationManager:(CLLocationManager *)manager! didExitRegion:(CLRegion *)region __OSX_AVAILABLE_STARTING(__MAC_10_7,__IPHONE_4_0);

- (void)locationManager:(CLLocationManager *)manager! didFailWithError:(NSError *)error;

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status __OSX_AVAILABLE_STARTING(__MAC_10_7,__IPHONE_4_2);

@end

Page 23: 04 Delegation and Core Location

protocol 的声明•使⽤用 @protocol 指令声明,紧跟名称

CLLocationManagerDelegate

•在尖括号内的 NSObject 引⽤用 NSObject 协议,告诉我们 CLLocationManagerDelegate 包括在 NSObject protocol 中的所有⽅方法

• CLLocationManagerDelegate 中具体的⽅方法在其后声明

• protocol 以 @end 指令关闭

Page 24: 04 Delegation and Core Location

protocol 和 class

• protocol 不是类,只是⼀一个简单的⽅方法列表

•不能实例化⼀一个 protocol,也不能具有实例变量

•这些⽅方法也没有在 protocol 中的任何地⽅方实现,相反的,这些实现留给了满⾜足这个 protocol 的每个类

Page 25: 04 Delegation and Core Location

delegate protocol

•我们把给 delegation 使⽤用的 protocol 称作 delegate protocol

• delegate protocol 的命名约定:delgating class 的名字 加上单词 “Delegate”

•不是所有的 protocols 都是 delegate protocol

Page 26: 04 Delegation and Core Location

protocol ⽅方法•在 CLLocationManagerDelegate protocol 中,我们可以看到有两类⽅方法• 处理信息更新的⽅方法• 处理输⼊入请求的⽅方法

•如果我们想要从 location manager 获知设备已经进⼊入了⼀一个特定的区域,那么 location manager 的 delegate 就要实现 locationManager:didEnterRegion: ⽅方法

• locationManagerShouldDisplayHeadingCalibration 是请求输⼊入的⽅方法,询问是否应该显⽰示⼀一个航向校正。⽅方法返回⼀一个 Boolean 值,作为 delegate 的答案

Page 27: 04 Delegation and Core Location

required 或 optional

•声明在 protocol 中的⽅方法可以是必须的(required)或者可选的(optional)

•默认情况下,protocol ⽅方法都是必须的(required)

•可选的⽅方法在前⾯面加上 @optional 指令

• CLLocationManagerDelegation protocol 中的⽅方法都是 optional

Page 28: 04 Delegation and Core Location

respondsToSelector:

•发送⼀一个 optional message 之前,对象会⾸首先通过发送 respondsToSelector: 消息询问它的 delegate 是否可以发送这个消息

•在运⾏行时检查⼀一个对象是否实现了给定⽅方法•可以使⽤用 @selector() 指令把⼀一个 method selector 转化成可以作为参数传递的值

Page 29: 04 Delegation and Core Location

protocol 的显式申明•如果在 protocol 中的⽅方法是必须的,消息将会被发送⽽而不⾸首先检查,这意味着如果 delegate 没有实现这个⽅方法,⼀一个⽆无法识别的 selector 异常将会抛出,并且应⽤用将会崩溃

•为了防⽌止这种情况出现,编译器会坚持要求类实现了 protocol 中必须的⽅方法

•为了让编译器知道检查 protocol 必要⽅方法的实现,类必须显式申明它满⾜足⼀一个 protocol

•在类的头⽂文件中,把满⾜足的 protocols 添加到 interface 声明中紧跟超类的 尖括号内以逗号分割的列表中

@interface WhereamiViewController : UIViewController <CLLocationManagerDelegate>

Page 30: 04 Delegation and Core Location

委托,控制器和内存管理•从 MVC 的⾓角度来看,WhereamiViewController 是⼀一个控制器对象

• delegate ⼀一般都是控制器对象,⽽而且⼀一个控制器对象⼀一般拥有(own)委托给它的对象

•WhereamiViewController 拥有 CLLocationManager, 并且 CLLocationManager 的 delegate 是 WhereamiViewController

Page 31: 04 Delegation and Core Location

重写 dealloc

• retain cycle

• __unsafe_unretained

Page 32: 04 Delegation and Core Location

使⽤用调试器•当应⽤用从 Xcode 启动的时候,debugger 就被附加到上⾯面

• debugger 监视应⽤用的当前状态•当前正在执⾏行的⽅方法•⽅方法当前访问的变量的值

Page 33: 04 Delegation and Core Location

使⽤用断点

Page 34: 04 Delegation and Core Location

诊断崩溃和异常