SlideShare una empresa de Scribd logo
1 de 34
Descargar para leer sin conexión
Delegation 和 Core Location




范圣刚,princetoad@gmail.com,www.tfan.org
• delegation
• Core Location framework
• Xcode debugger
• Whereami
 • 找到设备当前的位置
 • 在⼀一个可交互的地图上显⽰示
 • 允许⽤用户使⽤用⼤大头针和标题来标记当前位置
项⺫⽬目,⺫⽬目标和框架
Project
• ⼀一个⽂文件,包含⼀一个到其他⽂文件的引⽤用的列表
 • 源代码⽂文件
 • 资源
 • 框架
 • 类库
• 配置
• 扩展名 = xcodeproj
Target
• ⼀一个 project 总是具有⾄至少⼀一个 target
• target 使⽤用位于 project 中的⽂文件来构建某⼀一特定
 产品
• Build and run 的是target,⽽而不是 project
• target 构建出来的产出⼀一般是⼀一个应⽤用,虽然也
 可以是⼀一个编译库或者⼀一个单元测试 bundle。
• 创建⼀一个新的项⺫⽬目并且选择模板后,Xcode ⾃自动
 ⽣生成⼀一个 iOS application target 并且命名为
 Whereami
Build Phases
Frameworks
• 可以添加到 Target 的相关类的集合
• Cocoa Touch 是框架的集合
• 使⽤用 frameworks 组织 Cocoa Touch 的好处
• 已经链接到 target 的 frameworks:
 • UIKit - iOS ⽤用户界⾯面
 • Foundation - NSString, NSArray ...
 • Core Graphics - 图形库
添加 Core Location 框架
Core Location
• 包含使应⽤用程序可以⽤用来确定设备的地理位置的
 类
• 针对所有类型的 iOS 设备,我们写的 Core
 Location 代码是⼀一致的
• 除了导⼊入 Core Location framework 之外,我们还
 需要引⼊入 framework 的头⽂文件
• 框架的头⽂文件名称总是 框架名 + .h
WhereamiViewController.h
• 导⼊入 CoreLocation.h
• 增加⼀一个 CLLocationManager 实例变量
#import <UIKit/UIKit.h>
// 导⼊入 Core Location framework 的头⽂文件
#import <CoreLocation/CoreLocation.h>

@interface WhereamiViewController : UIViewController
{
    CLLocationManager *locationManager;
}
@end
CLLocationManager
• 和设备上的位置硬件交互的类
• 具有⼀一些指定其⾏行为的属性
desiredAccuracy
• 告诉 location manager 位置发现的精度应该是多
 少
• 位置精度,所需的时间量和电池续航时间之间的
 权衡
• 精度最终依赖于
 • 设备类型
 • 基站发射塔和卫星的有效性
 • 已知的⽆无线接⼊入点的有效性
实例化并启动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];
    }
}
从 CLLocationManager 接收更新
• location manager 已经开始⼯工作,程序如何从
 location manager 获取数据?
• locationManager:didUpdateLocations: -> delegate
• 什么对象是 location manager 的 delegate?
• 每个 CLLocationManager 都有 delegate 属性,我
 们可以设置这个属性指向我们希望⽤用于接
 收“location found” 消息的对象
• 对于 Whereami, 我们使⽤用
 WhereamiViewController
Whereami 对象图
CLLocation
实现两个⽅方法
• 因为 CLLocationManager 发送
 locationManager:didUpdateLocations: 消息给它
 的 delegate,因此我们必须在
 WhereamiViewController.m 中实现这个⽅方法
• 同时我们也需要知道 CLLocationManager 是否查
 找位置失败,以及为什么失败。
• 失败的时候,CLLocationManager 会发送
 locationManager:didFailWithError: 消息,我们同
 样要实现这个⽅方法。
模拟⼀一个位置
委托
• 委托是针对回调的⾯面向对象⽅方法
• 回调是提前提供给事件的⼀一个函数,并且在每次
事件发⽣生时都会被调⽤用
• 有些对象需要进⾏行多次回调,但是没有内置的⽅方
法可以让两个或多个回调函数协作和共享信息
• 提供⼀一个单独的 delegate 来接收⼀一个特定对象的
所有事件消息,这个 delegate 对象就可以存储,
操作和传递相关消息
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
协议(Protocols)
• 对于每⼀一个可以具有 delegate 的对象,都有⼀一个
 对应的protocol,protocol 声明了对象可以发送给
 它的 delegate 的所有消息
• 当⼀一个类实现了来⾃自⼀一个 protocol 的⽅方法,就被
 称作 conform to(遵守) 这个 protocol
@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
protocol 的声明
• 使⽤用 @protocol 指令声明,紧跟名称
 CLLocationManagerDelegate
• 在尖括号内的 NSObject 引⽤用 NSObject 协议,告
 诉我们 CLLocationManagerDelegate 包括在
 NSObject protocol 中的所有⽅方法
• CLLocationManagerDelegate 中具体的⽅方法在其后
 声明
• protocol 以 @end 指令关闭
protocol 和 class
• protocol 不是类,只是⼀一个简单的⽅方法列表
• 不能实例化⼀一个 protocol,也不能具有实例变量
• 这些⽅方法也没有在 protocol 中的任何地⽅方实现,
 相反的,这些实现留给了满⾜足这个 protocol 的每
 个类
delegate protocol
• 我们把给 delegation 使⽤用的 protocol 称作 delegate
 protocol
• delegate protocol 的命名约定:delgating class 的
 名字 加上单词 “Delegate”
• 不是所有的 protocols 都是 delegate protocol
protocol ⽅方法
• 在 CLLocationManagerDelegate protocol 中,我们可
 以看到有两类⽅方法
 • 处理信息更新的⽅方法
 • 处理输⼊入请求的⽅方法
• 如果我们想要从 location manager 获知设备已经进⼊入
 了⼀一个特定的区域,那么 location manager 的
 delegate 就要实现 locationManager:didEnterRegion: ⽅方
 法
• locationManagerShouldDisplayHeadingCalibration 是请
 求输⼊入的⽅方法,询问是否应该显⽰示⼀一个航向校正。⽅方
 法返回⼀一个 Boolean 值,作为 delegate 的答案
required 或 optional
• 声明在 protocol 中的⽅方法可以是必须的
 (required)或者可选的(optional)
• 默认情况下,protocol ⽅方法都是必须的
 (required)
• 可选的⽅方法在前⾯面加上 @optional 指令
• CLLocationManagerDelegation protocol 中的⽅方法
 都是 optional
respondsToSelector:
• 发送⼀一个 optional message 之前,对象会⾸首先通
 过发送 respondsToSelector: 消息询问它的
 delegate 是否可以发送这个消息
• 在运⾏行时检查⼀一个对象是否实现了给定⽅方法
• 可以使⽤用 @selector() 指令把⼀一个 method selector
 转化成可以作为参数传递的值
protocol 的显式申明
 • 如果在 protocol 中的⽅方法是必须的,消息将会被发
   送⽽而不⾸首先检查,这意味着如果 delegate 没有实现
   这个⽅方法,⼀一个⽆无法识别的 selector 异常将会抛出,
   并且应⽤用将会崩溃
 • 为了防⽌止这种情况出现,编译器会坚持要求类实现
   了 protocol 中必须的⽅方法
 • 为了让编译器知道检查 protocol 必要⽅方法的实现,
   类必须显式申明它满⾜足⼀一个 protocol
 • 在类的头⽂文件中,把满⾜足的 protocols 添加到
   interface 声明中紧跟超类的 尖括号内以逗号分割的
   列表中
@interface WhereamiViewController : UIViewController <CLLocationManagerDelegate>
委托,控制器和内存管理
• 从 MVC 的⾓角度来看,WhereamiViewController 是
 ⼀一个控制器对象
• delegate ⼀一般都是控制器对象,⽽而且⼀一个控制器
 对象⼀一般拥有(own)委托给它的对象
• WhereamiViewController 拥有 CLLocationManager,
 并且 CLLocationManager 的 delegate 是
 WhereamiViewController
重写 dealloc
• retain cycle
• __unsafe_unretained
使⽤用调试器
• 当应⽤用从 Xcode 启动的时候,debugger 就被附加
到上⾯面
• debugger 监视应⽤用的当前状态
 • 当前正在执⾏行的⽅方法
 • ⽅方法当前访问的变量的值
使⽤用断点
诊断崩溃和异常

Más contenido relacionado

La actualidad más candente

基于OpenResty的百万级长连接推送
基于OpenResty的百万级长连接推送基于OpenResty的百万级长连接推送
基于OpenResty的百万级长连接推送OpenRestyCon
 
CH16:整合資料庫
CH16:整合資料庫CH16:整合資料庫
CH16:整合資料庫Justin Lin
 
02 Objective-C
02 Objective-C02 Objective-C
02 Objective-CTom Fan
 
Servlet & JSP 教學手冊第二版 - 課後練習解答
Servlet & JSP 教學手冊第二版 - 課後練習解答Servlet & JSP 教學手冊第二版 - 課後練習解答
Servlet & JSP 教學手冊第二版 - 課後練習解答Justin Lin
 
Reactive X 响应式编程
Reactive X 响应式编程Reactive X 响应式编程
Reactive X 响应式编程Jun Liu
 
Serverless Event Streaming with Pulsar Functions-xiaolong
Serverless Event Streaming with Pulsar Functions-xiaolongServerless Event Streaming with Pulsar Functions-xiaolong
Serverless Event Streaming with Pulsar Functions-xiaolongStreamNative
 
Using armeria to write your RPC
Using armeria to write your RPCUsing armeria to write your RPC
Using armeria to write your RPCkoji lin
 
Large-Scale Cluster Mangement & Kubernetes Under The Hood
Large-Scale Cluster Mangement & Kubernetes Under The HoodLarge-Scale Cluster Mangement & Kubernetes Under The Hood
Large-Scale Cluster Mangement & Kubernetes Under The HoodLei (Harry) Zhang
 
05 MapKit and Text Input
05 MapKit and Text Input05 MapKit and Text Input
05 MapKit and Text InputTom Fan
 
2. 從 REPL 到 IDE
2. 從 REPL 到 IDE2. 從 REPL 到 IDE
2. 從 REPL 到 IDEJustin Lin
 
Ch02 撰寫與設定 Servlet
Ch02 撰寫與設定 ServletCh02 撰寫與設定 Servlet
Ch02 撰寫與設定 ServletJustin Lin
 
Java script closures
Java script closuresJava script closures
Java script closuresskywalker1114
 
07 View Controllers
07 View Controllers07 View Controllers
07 View ControllersTom Fan
 
Servlet & JSP 教學手冊第二版 - 第 7 章:使用 JSTL
Servlet & JSP 教學手冊第二版 - 第 7 章:使用 JSTLServlet & JSP 教學手冊第二版 - 第 7 章:使用 JSTL
Servlet & JSP 教學手冊第二版 - 第 7 章:使用 JSTLJustin Lin
 
3 introduction to kubernetes
3 introduction to kubernetes3 introduction to kubernetes
3 introduction to kubernetesJiang Shang
 
Servlet & JSP 教學手冊第二版 - 第 6 章:使用 JSP
Servlet & JSP 教學手冊第二版 - 第 6 章:使用 JSPServlet & JSP 教學手冊第二版 - 第 6 章:使用 JSP
Servlet & JSP 教學手冊第二版 - 第 6 章:使用 JSPJustin Lin
 
Servlet & JSP 教學手冊第二版 - 第 1 章:簡介Web應用程式
Servlet & JSP 教學手冊第二版 - 第 1 章:簡介Web應用程式Servlet & JSP 教學手冊第二版 - 第 1 章:簡介Web應用程式
Servlet & JSP 教學手冊第二版 - 第 1 章:簡介Web應用程式Justin Lin
 

La actualidad más candente (20)

基于OpenResty的百万级长连接推送
基于OpenResty的百万级长连接推送基于OpenResty的百万级长连接推送
基于OpenResty的百万级长连接推送
 
CH16:整合資料庫
CH16:整合資料庫CH16:整合資料庫
CH16:整合資料庫
 
02 Objective-C
02 Objective-C02 Objective-C
02 Objective-C
 
Servlet & JSP 教學手冊第二版 - 課後練習解答
Servlet & JSP 教學手冊第二版 - 課後練習解答Servlet & JSP 教學手冊第二版 - 課後練習解答
Servlet & JSP 教學手冊第二版 - 課後練習解答
 
Reactive X 响应式编程
Reactive X 响应式编程Reactive X 响应式编程
Reactive X 响应式编程
 
Serverless Event Streaming with Pulsar Functions-xiaolong
Serverless Event Streaming with Pulsar Functions-xiaolongServerless Event Streaming with Pulsar Functions-xiaolong
Serverless Event Streaming with Pulsar Functions-xiaolong
 
Using armeria to write your RPC
Using armeria to write your RPCUsing armeria to write your RPC
Using armeria to write your RPC
 
Large-Scale Cluster Mangement & Kubernetes Under The Hood
Large-Scale Cluster Mangement & Kubernetes Under The HoodLarge-Scale Cluster Mangement & Kubernetes Under The Hood
Large-Scale Cluster Mangement & Kubernetes Under The Hood
 
05 MapKit and Text Input
05 MapKit and Text Input05 MapKit and Text Input
05 MapKit and Text Input
 
2. 從 REPL 到 IDE
2. 從 REPL 到 IDE2. 從 REPL 到 IDE
2. 從 REPL 到 IDE
 
Ch02 撰寫與設定 Servlet
Ch02 撰寫與設定 ServletCh02 撰寫與設定 Servlet
Ch02 撰寫與設定 Servlet
 
Docker實務
Docker實務Docker實務
Docker實務
 
Java script closures
Java script closuresJava script closures
Java script closures
 
07 View Controllers
07 View Controllers07 View Controllers
07 View Controllers
 
Servlet & JSP 教學手冊第二版 - 第 7 章:使用 JSTL
Servlet & JSP 教學手冊第二版 - 第 7 章:使用 JSTLServlet & JSP 教學手冊第二版 - 第 7 章:使用 JSTL
Servlet & JSP 教學手冊第二版 - 第 7 章:使用 JSTL
 
RESTful
RESTfulRESTful
RESTful
 
3 introduction to kubernetes
3 introduction to kubernetes3 introduction to kubernetes
3 introduction to kubernetes
 
Servlet & JSP 教學手冊第二版 - 第 6 章:使用 JSP
Servlet & JSP 教學手冊第二版 - 第 6 章:使用 JSPServlet & JSP 教學手冊第二版 - 第 6 章:使用 JSP
Servlet & JSP 教學手冊第二版 - 第 6 章:使用 JSP
 
Docker基礎
Docker基礎Docker基礎
Docker基礎
 
Servlet & JSP 教學手冊第二版 - 第 1 章:簡介Web應用程式
Servlet & JSP 教學手冊第二版 - 第 1 章:簡介Web應用程式Servlet & JSP 教學手冊第二版 - 第 1 章:簡介Web應用程式
Servlet & JSP 教學手冊第二版 - 第 1 章:簡介Web應用程式
 

Destacado

15 Subclassing UITableViewCell
15 Subclassing UITableViewCell15 Subclassing UITableViewCell
15 Subclassing UITableViewCellTom Fan
 
01 A Simple iOS Application
01 A Simple iOS Application01 A Simple iOS Application
01 A Simple iOS ApplicationTom Fan
 
iOS: Implementing a Custom View
iOS: Implementing a Custom ViewiOS: Implementing a Custom View
iOS: Implementing a Custom ViewJussi Pohjolainen
 
08 Notification and Rotation
08 Notification and Rotation08 Notification and Rotation
08 Notification and RotationTom Fan
 
06 Subclassing UIView and UIScrollView
06 Subclassing UIView and UIScrollView06 Subclassing UIView and UIScrollView
06 Subclassing UIView and UIScrollViewTom Fan
 
PhoneGap 通信原理和插件系统
PhoneGap 通信原理和插件系统PhoneGap 通信原理和插件系统
PhoneGap 通信原理和插件系统Tom Fan
 

Destacado (6)

15 Subclassing UITableViewCell
15 Subclassing UITableViewCell15 Subclassing UITableViewCell
15 Subclassing UITableViewCell
 
01 A Simple iOS Application
01 A Simple iOS Application01 A Simple iOS Application
01 A Simple iOS Application
 
iOS: Implementing a Custom View
iOS: Implementing a Custom ViewiOS: Implementing a Custom View
iOS: Implementing a Custom View
 
08 Notification and Rotation
08 Notification and Rotation08 Notification and Rotation
08 Notification and Rotation
 
06 Subclassing UIView and UIScrollView
06 Subclassing UIView and UIScrollView06 Subclassing UIView and UIScrollView
06 Subclassing UIView and UIScrollView
 
PhoneGap 通信原理和插件系统
PhoneGap 通信原理和插件系统PhoneGap 通信原理和插件系统
PhoneGap 通信原理和插件系统
 

Similar a 04 Delegation and Core Location

IOS入门分享
IOS入门分享IOS入门分享
IOS入门分享zenyuhao
 
給 iOS 工程師的 Flutter 開發
給 iOS 工程師的 Flutter 開發給 iOS 工程師的 Flutter 開發
給 iOS 工程師的 Flutter 開發Weizhong Yang
 
[2020 .NET Conf] 企業Azure DevOps Service 實際應用架構與秘辛
[2020 .NET Conf] 企業Azure DevOps Service 實際應用架構與秘辛[2020 .NET Conf] 企業Azure DevOps Service 實際應用架構與秘辛
[2020 .NET Conf] 企業Azure DevOps Service 實際應用架構與秘辛Edward Kuo
 
14 Saving Loading and Application States
14 Saving Loading and Application States14 Saving Loading and Application States
14 Saving Loading and Application StatesTom Fan
 
Migrations 與 Schema 操作
Migrations 與 Schema 操作Migrations 與 Schema 操作
Migrations 與 Schema 操作Shengyou Fan
 
docker intro
docker introdocker intro
docker introkoji lin
 
Dev-Ops与Docker的最佳实践 QCon2016 北京站演讲
Dev-Ops与Docker的最佳实践 QCon2016 北京站演讲Dev-Ops与Docker的最佳实践 QCon2016 北京站演讲
Dev-Ops与Docker的最佳实践 QCon2016 北京站演讲ChinaNetCloud
 
Clipper@datacon.2019.tw
Clipper@datacon.2019.twClipper@datacon.2019.tw
Clipper@datacon.2019.twWei-Yu Chen
 
4, OCP - oracle networking
4, OCP - oracle networking4, OCP - oracle networking
4, OCP - oracle networkingted-xu
 
Docker 最佳实践
Docker 最佳实践Docker 最佳实践
Docker 最佳实践YuLing Liu
 
2, OCP - installing and creating a database
2, OCP - installing and creating a database2, OCP - installing and creating a database
2, OCP - installing and creating a databaseted-xu
 
Java SE 8 技術手冊第 5 章 - 物件封裝
Java SE 8 技術手冊第 5 章 - 物件封裝Java SE 8 技術手冊第 5 章 - 物件封裝
Java SE 8 技術手冊第 5 章 - 物件封裝Justin Lin
 
OpenEJB - 另一個選擇
OpenEJB - 另一個選擇OpenEJB - 另一個選擇
OpenEJB - 另一個選擇Justin Lin
 
Oh K8s Is Swag - Kubernetes Basics
Oh K8s Is Swag - Kubernetes BasicsOh K8s Is Swag - Kubernetes Basics
Oh K8s Is Swag - Kubernetes BasicsOkis Chuang
 
Migrations 與 Schema操作
Migrations 與 Schema操作Migrations 與 Schema操作
Migrations 與 Schema操作Shengyou Fan
 
Azure Container Service 使用 DC / OS 管理 docker 容器
Azure Container Service 使用 DC / OS 管理 docker 容器Azure Container Service 使用 DC / OS 管理 docker 容器
Azure Container Service 使用 DC / OS 管理 docker 容器Ch Rick
 
Docker一期培训
Docker一期培训Docker一期培训
Docker一期培训青帅 常
 

Similar a 04 Delegation and Core Location (20)

IOS入门分享
IOS入门分享IOS入门分享
IOS入门分享
 
給 iOS 工程師的 Flutter 開發
給 iOS 工程師的 Flutter 開發給 iOS 工程師的 Flutter 開發
給 iOS 工程師的 Flutter 開發
 
[2020 .NET Conf] 企業Azure DevOps Service 實際應用架構與秘辛
[2020 .NET Conf] 企業Azure DevOps Service 實際應用架構與秘辛[2020 .NET Conf] 企業Azure DevOps Service 實際應用架構與秘辛
[2020 .NET Conf] 企業Azure DevOps Service 實際應用架構與秘辛
 
14 Saving Loading and Application States
14 Saving Loading and Application States14 Saving Loading and Application States
14 Saving Loading and Application States
 
Migrations 與 Schema 操作
Migrations 與 Schema 操作Migrations 與 Schema 操作
Migrations 與 Schema 操作
 
docker intro
docker introdocker intro
docker intro
 
Javascript 闭包
Javascript 闭包Javascript 闭包
Javascript 闭包
 
Dev-Ops与Docker的最佳实践 QCon2016 北京站演讲
Dev-Ops与Docker的最佳实践 QCon2016 北京站演讲Dev-Ops与Docker的最佳实践 QCon2016 北京站演讲
Dev-Ops与Docker的最佳实践 QCon2016 北京站演讲
 
Clipper@datacon.2019.tw
Clipper@datacon.2019.twClipper@datacon.2019.tw
Clipper@datacon.2019.tw
 
4, OCP - oracle networking
4, OCP - oracle networking4, OCP - oracle networking
4, OCP - oracle networking
 
Docker 最佳实践
Docker 最佳实践Docker 最佳实践
Docker 最佳实践
 
2, OCP - installing and creating a database
2, OCP - installing and creating a database2, OCP - installing and creating a database
2, OCP - installing and creating a database
 
Java SE 8 技術手冊第 5 章 - 物件封裝
Java SE 8 技術手冊第 5 章 - 物件封裝Java SE 8 技術手冊第 5 章 - 物件封裝
Java SE 8 技術手冊第 5 章 - 物件封裝
 
OpenEJB - 另一個選擇
OpenEJB - 另一個選擇OpenEJB - 另一個選擇
OpenEJB - 另一個選擇
 
Oh K8s Is Swag - Kubernetes Basics
Oh K8s Is Swag - Kubernetes BasicsOh K8s Is Swag - Kubernetes Basics
Oh K8s Is Swag - Kubernetes Basics
 
Migrations 與 Schema操作
Migrations 與 Schema操作Migrations 與 Schema操作
Migrations 與 Schema操作
 
Azure Container Service 使用 DC / OS 管理 docker 容器
Azure Container Service 使用 DC / OS 管理 docker 容器Azure Container Service 使用 DC / OS 管理 docker 容器
Azure Container Service 使用 DC / OS 管理 docker 容器
 
Javascript
JavascriptJavascript
Javascript
 
Docker基礎
Docker基礎Docker基礎
Docker基礎
 
Docker一期培训
Docker一期培训Docker一期培训
Docker一期培训
 

Más de Tom Fan

HTML5 Web workers
HTML5 Web workersHTML5 Web workers
HTML5 Web workersTom Fan
 
Web sockets
Web socketsWeb sockets
Web socketsTom Fan
 
Semantics
SemanticsSemantics
SemanticsTom Fan
 
Multimedia
MultimediaMultimedia
MultimediaTom Fan
 
Intro to-html5
Intro to-html5Intro to-html5
Intro to-html5Tom Fan
 
Html5 history
Html5 historyHtml5 history
Html5 historyTom Fan
 
Geolocation
GeolocationGeolocation
GeolocationTom Fan
 
File api
File apiFile api
File apiTom Fan
 
Deviceaccess
DeviceaccessDeviceaccess
DeviceaccessTom Fan
 
Webstorage
WebstorageWebstorage
WebstorageTom Fan
 
Html5 最重要的部分
Html5 最重要的部分Html5 最重要的部分
Html5 最重要的部分Tom Fan
 
AT&T 的 HTML5 策略和应用现状
AT&T 的 HTML5 策略和应用现状AT&T 的 HTML5 策略和应用现状
AT&T 的 HTML5 策略和应用现状Tom Fan
 
PhoneGap 2.0 开发
PhoneGap 2.0 开发PhoneGap 2.0 开发
PhoneGap 2.0 开发Tom Fan
 
Android 平台 HTML5 应用开发
Android 平台 HTML5 应用开发Android 平台 HTML5 应用开发
Android 平台 HTML5 应用开发Tom Fan
 
HTML5 生态系统和应用架构模型
HTML5 生态系统和应用架构模型HTML5 生态系统和应用架构模型
HTML5 生态系统和应用架构模型Tom Fan
 
18 NSUserDefaults
18 NSUserDefaults18 NSUserDefaults
18 NSUserDefaultsTom Fan
 
17 Localization
17 Localization17 Localization
17 LocalizationTom Fan
 
16 CoreData
16 CoreData16 CoreData
16 CoreDataTom Fan
 

Más de Tom Fan (20)

HTML5 Web workers
HTML5 Web workersHTML5 Web workers
HTML5 Web workers
 
Web sockets
Web socketsWeb sockets
Web sockets
 
Storage
StorageStorage
Storage
 
Semantics
SemanticsSemantics
Semantics
 
Multimedia
MultimediaMultimedia
Multimedia
 
Intro to-html5
Intro to-html5Intro to-html5
Intro to-html5
 
Html5 history
Html5 historyHtml5 history
Html5 history
 
Geolocation
GeolocationGeolocation
Geolocation
 
File api
File apiFile api
File api
 
Deviceaccess
DeviceaccessDeviceaccess
Deviceaccess
 
Css3
Css3Css3
Css3
 
Webstorage
WebstorageWebstorage
Webstorage
 
Html5 最重要的部分
Html5 最重要的部分Html5 最重要的部分
Html5 最重要的部分
 
AT&T 的 HTML5 策略和应用现状
AT&T 的 HTML5 策略和应用现状AT&T 的 HTML5 策略和应用现状
AT&T 的 HTML5 策略和应用现状
 
PhoneGap 2.0 开发
PhoneGap 2.0 开发PhoneGap 2.0 开发
PhoneGap 2.0 开发
 
Android 平台 HTML5 应用开发
Android 平台 HTML5 应用开发Android 平台 HTML5 应用开发
Android 平台 HTML5 应用开发
 
HTML5 生态系统和应用架构模型
HTML5 生态系统和应用架构模型HTML5 生态系统和应用架构模型
HTML5 生态系统和应用架构模型
 
18 NSUserDefaults
18 NSUserDefaults18 NSUserDefaults
18 NSUserDefaults
 
17 Localization
17 Localization17 Localization
17 Localization
 
16 CoreData
16 CoreData16 CoreData
16 CoreData
 

04 Delegation and Core Location

  • 1. Delegation 和 Core Location 范圣刚,princetoad@gmail.com,www.tfan.org
  • 2. • delegation • Core Location framework • Xcode debugger • Whereami • 找到设备当前的位置 • 在⼀一个可交互的地图上显⽰示 • 允许⽤用户使⽤用⼤大头针和标题来标记当前位置
  • 4. Project • ⼀一个⽂文件,包含⼀一个到其他⽂文件的引⽤用的列表 • 源代码⽂文件 • 资源 • 框架 • 类库 • 配置 • 扩展名 = xcodeproj
  • 5. Target • ⼀一个 project 总是具有⾄至少⼀一个 target • target 使⽤用位于 project 中的⽂文件来构建某⼀一特定 产品 • Build and run 的是target,⽽而不是 project • target 构建出来的产出⼀一般是⼀一个应⽤用,虽然也 可以是⼀一个编译库或者⼀一个单元测试 bundle。 • 创建⼀一个新的项⺫⽬目并且选择模板后,Xcode ⾃自动 ⽣生成⼀一个 iOS application target 并且命名为 Whereami
  • 7. Frameworks • 可以添加到 Target 的相关类的集合 • Cocoa Touch 是框架的集合 • 使⽤用 frameworks 组织 Cocoa Touch 的好处 • 已经链接到 target 的 frameworks: • UIKit - iOS ⽤用户界⾯面 • Foundation - NSString, NSArray ... • Core Graphics - 图形库
  • 9. Core Location • 包含使应⽤用程序可以⽤用来确定设备的地理位置的 类 • 针对所有类型的 iOS 设备,我们写的 Core Location 代码是⼀一致的 • 除了导⼊入 Core Location framework 之外,我们还 需要引⼊入 framework 的头⽂文件 • 框架的头⽂文件名称总是 框架名 + .h
  • 10. WhereamiViewController.h • 导⼊入 CoreLocation.h • 增加⼀一个 CLLocationManager 实例变量 #import <UIKit/UIKit.h> // 导⼊入 Core Location framework 的头⽂文件 #import <CoreLocation/CoreLocation.h> @interface WhereamiViewController : UIViewController { CLLocationManager *locationManager; } @end
  • 12. desiredAccuracy • 告诉 location manager 位置发现的精度应该是多 少 • 位置精度,所需的时间量和电池续航时间之间的 权衡 • 精度最终依赖于 • 设备类型 • 基站发射塔和卫星的有效性 • 已知的⽆无线接⼊入点的有效性
  • 13. 实例化并启动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]; } }
  • 14. 从 CLLocationManager 接收更新 • location manager 已经开始⼯工作,程序如何从 location manager 获取数据? • locationManager:didUpdateLocations: -> delegate • 什么对象是 location manager 的 delegate? • 每个 CLLocationManager 都有 delegate 属性,我 们可以设置这个属性指向我们希望⽤用于接 收“location found” 消息的对象 • 对于 Whereami, 我们使⽤用 WhereamiViewController
  • 17. 实现两个⽅方法 • 因为 CLLocationManager 发送 locationManager:didUpdateLocations: 消息给它 的 delegate,因此我们必须在 WhereamiViewController.m 中实现这个⽅方法 • 同时我们也需要知道 CLLocationManager 是否查 找位置失败,以及为什么失败。 • 失败的时候,CLLocationManager 会发送 locationManager:didFailWithError: 消息,我们同 样要实现这个⽅方法。
  • 19. 委托 • 委托是针对回调的⾯面向对象⽅方法 • 回调是提前提供给事件的⼀一个函数,并且在每次 事件发⽣生时都会被调⽤用 • 有些对象需要进⾏行多次回调,但是没有内置的⽅方 法可以让两个或多个回调函数协作和共享信息 • 提供⼀一个单独的 delegate 来接收⼀一个特定对象的 所有事件消息,这个 delegate 对象就可以存储, 操作和传递相关消息
  • 20. 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
  • 21. 协议(Protocols) • 对于每⼀一个可以具有 delegate 的对象,都有⼀一个 对应的protocol,protocol 声明了对象可以发送给 它的 delegate 的所有消息 • 当⼀一个类实现了来⾃自⼀一个 protocol 的⽅方法,就被 称作 conform to(遵守) 这个 protocol
  • 22. @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
  • 23. protocol 的声明 • 使⽤用 @protocol 指令声明,紧跟名称 CLLocationManagerDelegate • 在尖括号内的 NSObject 引⽤用 NSObject 协议,告 诉我们 CLLocationManagerDelegate 包括在 NSObject protocol 中的所有⽅方法 • CLLocationManagerDelegate 中具体的⽅方法在其后 声明 • protocol 以 @end 指令关闭
  • 24. protocol 和 class • protocol 不是类,只是⼀一个简单的⽅方法列表 • 不能实例化⼀一个 protocol,也不能具有实例变量 • 这些⽅方法也没有在 protocol 中的任何地⽅方实现, 相反的,这些实现留给了满⾜足这个 protocol 的每 个类
  • 25. delegate protocol • 我们把给 delegation 使⽤用的 protocol 称作 delegate protocol • delegate protocol 的命名约定:delgating class 的 名字 加上单词 “Delegate” • 不是所有的 protocols 都是 delegate protocol
  • 26. protocol ⽅方法 • 在 CLLocationManagerDelegate protocol 中,我们可 以看到有两类⽅方法 • 处理信息更新的⽅方法 • 处理输⼊入请求的⽅方法 • 如果我们想要从 location manager 获知设备已经进⼊入 了⼀一个特定的区域,那么 location manager 的 delegate 就要实现 locationManager:didEnterRegion: ⽅方 法 • locationManagerShouldDisplayHeadingCalibration 是请 求输⼊入的⽅方法,询问是否应该显⽰示⼀一个航向校正。⽅方 法返回⼀一个 Boolean 值,作为 delegate 的答案
  • 27. required 或 optional • 声明在 protocol 中的⽅方法可以是必须的 (required)或者可选的(optional) • 默认情况下,protocol ⽅方法都是必须的 (required) • 可选的⽅方法在前⾯面加上 @optional 指令 • CLLocationManagerDelegation protocol 中的⽅方法 都是 optional
  • 28. respondsToSelector: • 发送⼀一个 optional message 之前,对象会⾸首先通 过发送 respondsToSelector: 消息询问它的 delegate 是否可以发送这个消息 • 在运⾏行时检查⼀一个对象是否实现了给定⽅方法 • 可以使⽤用 @selector() 指令把⼀一个 method selector 转化成可以作为参数传递的值
  • 29. protocol 的显式申明 • 如果在 protocol 中的⽅方法是必须的,消息将会被发 送⽽而不⾸首先检查,这意味着如果 delegate 没有实现 这个⽅方法,⼀一个⽆无法识别的 selector 异常将会抛出, 并且应⽤用将会崩溃 • 为了防⽌止这种情况出现,编译器会坚持要求类实现 了 protocol 中必须的⽅方法 • 为了让编译器知道检查 protocol 必要⽅方法的实现, 类必须显式申明它满⾜足⼀一个 protocol • 在类的头⽂文件中,把满⾜足的 protocols 添加到 interface 声明中紧跟超类的 尖括号内以逗号分割的 列表中 @interface WhereamiViewController : UIViewController <CLLocationManagerDelegate>
  • 30. 委托,控制器和内存管理 • 从 MVC 的⾓角度来看,WhereamiViewController 是 ⼀一个控制器对象 • delegate ⼀一般都是控制器对象,⽽而且⼀一个控制器 对象⼀一般拥有(own)委托给它的对象 • WhereamiViewController 拥有 CLLocationManager, 并且 CLLocationManager 的 delegate 是 WhereamiViewController
  • 31. 重写 dealloc • retain cycle • __unsafe_unretained
  • 32. 使⽤用调试器 • 当应⽤用从 Xcode 启动的时候,debugger 就被附加 到上⾯面 • debugger 监视应⽤用的当前状态 • 当前正在执⾏行的⽅方法 • ⽅方法当前访问的变量的值