使用Core Location
来源:互联网 发布:罂粟种子淘宝上叫什么 编辑:程序博客网 时间:2024/05/24 22:44
file into files that need to know about Core Location classes
#import <CoreLocation/CoreLocation.h>
2. CLLocationManager
定义: comform the delegate
@interface WhereamiViewController : UIViewController <CLLocationManagerDelegate>
{
CLLocationManager *locationManager;
}
init:
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Create location manager object
locationManager = [[CLLocationManager alloc] init];
// And we want it to be as accurate as possible// regardless of how much time/power it takes
[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
// Tell our manager to start looking for its location immediately
[locationManager startUpdatingLocation];
}
return self;
}
3.set deletegate at :initWithNibName
// There will be a warning from this line of code; ignore it for now
[locationManager setDelegate:self]
4.implement delegate method:
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
NSLog(@"%@", newLocation);
}
- (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error
{
NSLog(@"Could not find location: %@", error);
}
If you are running Whereami on the simulator, you’ll have to simulate its location once Whereami is running. When
you run an application from Xcode, a bar appears at the bottom of the editor area with several icons. Click the
icon in this bar and then select a location from the drop-down list
Compare Target-action and Deletgate:
Let’s compare delegation with another object-oriented approach to callbacks: target-action pairs. You used target action
pairs with the UIButtons in your Quiz application from Chapter 1. In a target-action pair, you have a target
object that you send an action message when a particular event occurs (like a button tap). A new target-action pair
must be created for each distinct event (like a tap, a double tap, or a long press). With delegation, you set the delegate
once and then can send it messages for many different events. The delegate will implement the methods that
correspond to the events it wants to hear about
Before sending an optional message, the object first asks its delegate if it is okay to send that message by sending
another message, respondsToSelector:. Every object implements this method, which checks at runtime whether an
object implements a given method. You can turn a method selector into a value that you can pass as an argument with
the @selector() directive. For example, CLLocationManager could implement a method that looks like this:
- (void)finishedFindingLocation:(CLLocation *)newLocation
{
// locationManager:didUpdateToLocation:fromLocation:
// is an optional method, so we check first.
SEL updateMethod = @selector(locationManager:didUpdateToLocation:fromLocation:);
if ([[self delegate] respondsToSelector:updateMethod]) {
// If the method is implemented, then we send the message.
[[self delegate] locationManager:self
didUpdateToLocation:newLocation
fromLocation:oldLocation];
}
}
为防止互相的Strong reference 导致的对象release 问题, CLLocationManager’s delegate property is not a
strong reference. But it is not a weak reference, either. To maintain backwards-compatibility with non-ARC versions
of iOS, delegate properties are __unsafe_unretained. 既然不是Strong ,也不是weak, 而是为了兼容性,设置成__unsafe_unretained, 所以需要
在dealloc 中设置为nil.
5.
Because delegate is unsafe unretained instead of weak, it is not automatically set to nil when the object it points to
is destroyed. You have to do that yourself in the delegate object’s dealloc method.
In WhereamiViewController.m, override dealloc.
- (void)dealloc
{
// Tell the location manager to stop sending us messages
[locationManager setDelegate:nil];
}
It is a little deceptive on our part to have you implement dealloc for WhereamiViewController. Why? Because the
WhereamiViewController will never be destroyed in this application – the dealloc method you just implemented
will never be called. The Whereami application needs the WhereamiViewController from beginning to end, so the
WhereamiViewController always has an owner.
However, many controller objects in your applications will get destroyed, and some of these will be delegates of
other objects. As you go through this book, you’ll start to see which controllers in an application get destroyed and
which stick around for the life of the application. For now, it is best to be safe, learn the ropes, and implement
dealloc for WhereamiViewController to set the locationManager’s delegate to nil.
In the next chapter, we’ll give Whereami a user interface. Right now, let’s take a look at how to work with the
debugger that comes with Xcode.
Diagnosing crashes and exceptions
While you can set breakpoints yourself to break on a particular line, it would be nice if the debugger would
automatically set a breakpoint on any line that causes your application to crash or that causes an exception to be
thrown.
To get the debugger to do this, we need to add a breakpoint for all exceptions. Select the debug navigator. Then, at
the bottom of the navigator area, click the + icon and select Add Exception Breakpoint.... Then, click the Done
button on the panel that pops up (Figure 4.13)
Preprocessing
The Compile Sources build phase can be broken into two steps: preprocessing and compiling. The goal of the
preprocessing phase is to create an intermediate file for each implementation file (.m). The intermediate file is still
Objective-C code like the implementation file, but, as we will see, the intermediate file can get very large.
To create an intermediate file, the preprocessor resolves all the preprocessor directives in the implementation file.
Preprocessor directives are statements prefixed with the pound symbol (#), like #import. The resolution of a
#import statement replaces the import statement with the contents
Compiling
Once the preprocessor has finished, the generated intermediate files are compiled. Compiling an intermediate file
takes the Objective-C code and turns it into machine code. This machine code is stored in an object file, one for each
intermediate file.
The compiling phase – the transition to machine code – is where we see most of our errors as programmers. When
the compiler doesn’t understand our code, it generates an error. We call errors generated during this phase compiletime
errors or syntax errors. Compile-time errors are typically misplaced semicolons, unbalanced square brackets ([])
or curly ones ({}), spelling or capitalization errors.
Linking
An object file contains the machine code for the methods implemented in the implementation file. However, within
an implementation file, you use code from other implementation files. For example, WhereamiViewController.m
uses the startUpdatingLocation method, and the machine code for that method is in the object file generated from
CLLocationManager.m.
Instead of copying the code for this method into the object file for WhereamiViewController.m, the compiler leaves
a link to the object file for CLLocationManager.m. The Link Binary With Libraries phase is where these links are
resolved. For short, we just call it the linking phase.
- 使用Core Location
- Core Location的使用
- ios-使用Core Location定位
- Core Location
- Core Location定位和MapKit视图使用
- ios-使用Core Location框架定位
- iOS定位服务Core Location使用详解
- IOS学习之Core Location定位和MapKit视图使用
- Core Location和MapKit的一些简单使用
- iOS8之在Today Extension中使用Core Location
- Core Location 简述
- IOS Core Location
- Core Location之指南针
- Core Location Framework学习
- Core Location 简述
- Core Location定位
- iOS8 Core Location
- Core Location初心者
- 简单工具-较为自动地添加电话号码
- 第四章 Controller接口控制器详解(1)
- No Dialect mapping for JDBC type解决方法
- C# 通过服务启动窗体(把窗体添加到服务里)实现用户交互的windows服务
- SSH建立信任关系实现免输密码登陆
- 使用Core Location
- [测试优化] Unity 3D 阴影锯齿优化
- ClistCtrl,不能显示数据
- 键盘记录工具(支持中文)
- T-SQL中执行sql语句的两种写法
- php非常简单的文件上传,没有文件类型、大小限制
- Windows Sockets 2.0:使用完成端口高性能,可扩展性Winsock服务程序
- unsigned int的最大值(用十进制表示)
- Windows下mysql客户端访问Linux下mysql-cluster的SQL节点服务器