最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

给Your Second iOS App的BirdWatching添加Location Services

iOS crifan 2792浏览 0评论

之前已经给Your Second iOS App的BirdWatching添加自定义日期了,现在继续根据:

Your Second iOS App – Next Steps

中的建议:

Ask the user to enable Location Services so that the app can suggest the user’s current location as the bird sighting location.

去给其添加Location Services。


去网上找了找,啥是iOS的Location Services,然后找到官网教程:

iOS 5: Understanding Location Services

然后才得知:

【iOS的Location Services功能介绍】

1.原来Location Services就是为一堆程序提供位置服务。

2. 这些程序是属于Location-dependent的程序,比如Maps,Camera,Safari,。。。

3. 而位置信息的提供是通过:cellular,Wi-Fi,GPS,的网络所得到的位置信息。

4. 用途举例:

(1)(基于你当前的位置)帮你找到你附近的咖啡店

(2)(根据你所在的位置)自动帮你设置时区

5.获得对应的位置信息,所通过的网络,可能是上述所说多种网络的组合。

比如你如果当前不在GPS所提供的信号范围内的话,则会通过密集的WiFi信号和基站(cell)来决定你的位置。

6.然后就去看官方资料中,相关的类的说明:

CLLocationManager Class Reference

CLLocationManagerDelegate Protocol Reference

看完后,再去参考别人的例子:

CLLocationManager用法示例

然后一点点自己去实现自己的代码。

7.先创建一个类,基于NSObject的:

based on nsobject create new locationmanager

8.创建好类之后,然后去添加对应所需的CoreLocation.framework。

结果虽然也参考了:

An Example iOS 5 iPhone Location Application

iOS利用CoreLocation获取地理位置以及如何在模拟器进行调试

结果愣是找了半天没找到。

最后还是自己一点点摸索出来的:

【Xcode中,如何给当前项目添加一个新的framework,比如CoreLocation.framework】

图示:

how add framework

found corelocation

文字版:

点击当前项目的名称->TARGETS->Build Phases->Link Binary With Libraries->点击 “+”,即可添加所需的library。

9.不过后来又删除了上面单独建立的类,而是把相关代码加入到原来的文件里面了。

然后在AddSightingViewController.h中添加了:

@property (nonatomic) CLLocationManager *locationManager;
@property (nonatomic) CLLocation *curLocation;

在AddSightingViewController.m中的viewDidLoad中添加了:

    self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    
    self.locationManager.delegate = self;
    
    self.curLocation = nil;
    
    [self.locationManager startUpdatingLocation];

然后去试试,果然出现了所期望的提示,问该是否允许该程序使用Location Services:

show hit to allow use location

点击OK后,iPhone模拟器的右上角也显示了对应的使用位置服务的箭头符号:

location sign

 

10.然后又去添加代码:

#pragma mark -
#pragma mark CLLocationManagerDelegate
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
    //NSString *locationDesciption = [[NSString alloc] initWithString:newLocation.description];
    self.locationInput.text = [[NSString alloc] initWithString:newLocation.description];

}

最后是可以实现所要的效果的,即对应的location可以获得位置信息:

location desc str

11.不过上述缺点是,位置信息会实时更新,导致locationInput.text会一直被更新,所以,要去优化一下,仅初始化赋值一次,即可释放此LocationManager对象了。

后来试了,把delegate设置为nil:

#pragma mark -
#pragma mark CLLocationManagerDelegate
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
    //NSString *locationDesciption = [[NSString alloc] initWithString:newLocation.description];
    self.locationInput.text = [[NSString alloc] initWithString:newLocation.description];

    self.locationManager.delegate = nil;
}

就可以取消更新了,对应的,locationInput.text位置的内容,也可以再继续手动更新了。

而不会出现之前始终被更新,而导致手动几乎无法修改了。

 

【总结】

添加相应的库CoreLocation.framework,新增一个CLLocationManager,然后实现其delegate,即CLLocationManagerDelegate,即可使用Location Services,实现自己要做的事情了。

转载请注明:在路上 » 给Your Second iOS App的BirdWatching添加Location Services

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
90 queries in 0.167 seconds, using 22.16MB memory