iOS开发之ReactiveCocoa框架(RAC)第六篇程序定位

使用RAC方式与传统方式做定位功能的对比

首先,因为使用定位功能,所以要在info.plist文件里面做请求描述设置

这里写图片描述

添加下面两个字段:
Privacy - Location Always Usage Description
Privacy - Location When In Use Usage Description

然后是代码部分:

.h文件

#import "ViewController.h"

@import ReactiveCocoa;

@interface ViewController ()<CLLocationManagerDelegate>

@property (nonatomic,strong)CLLocationManager * manager;//位置管理器
@property (nonatomic) CLGeocoder * geocoder;//返地理位置编码
@property (weak, nonatomic) IBOutlet UILabel *placeLabel;

@end

@implementation ViewController

-(CLLocationManager * )manager{

    if (!_manager) {

        _manager = [[CLLocationManager alloc]init];
        _manager.delegate = self;
    }
    return _manager;
}

-(CLGeocoder *)geocoder{

    if (!_geocoder) {

        _geocoder = [[CLGeocoder alloc]init];
    }
    return _geocoder;
}

-(RACSignal *)authorizedSignal{

    if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined) {

        [self.manager requestAlwaysAuthorization];

        return [[self rac_signalForSelector:@selector(locationManager:didChangeAuthorizationStatus:) fromProtocol:@protocol(CLLocationManagerDelegate)] map:^id(id value) {

            return @([value[1] integerValue] == kCLAuthorizationStatusAuthorizedAlways);

        }];
    }
    return [RACSignal return:@([CLLocationManager authorizationStatus]== kCLAuthorizationStatusAuthorizedAlways)];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    //    RACSequence * s1 = [[[@[@(1),@(2),@(3)] rac_sequence] map:^id(id value) {
    //
    //        return @([value integerValue]+2);
    //    }] filter:^BOOL(id value) {
    //
    //        return [value integerValue] <5;
    //    }];
    //
    //    NSLog(@"%@",[s1 array]);
    //    //3,4

    //    RACSequence * s1 = [@[@(1),@(2),@(3)] rac_sequence];
    //    RACSequence * s2 = [@[@(1),@(3),@(9)] rac_sequence];
    //
    //    RACSequence * s3 =[[@[s1,s2] rac_sequence] flattenMap:^RACStream *(id value) {
    //
    //        return [value filter:^BOOL(id value) {
    //
    //            return [value integerValue] % 3 ==0;
    //        }];
    //    }];
    //    NSLog(@"%@",[s3 array]);
    //    //3,3,9

    //知识点:获取某个对象的属性时,生成信号量
    //传统OC 使用KVO技术

    //    RAC  RACObserve观察者(对象,属性)
    //    RACSignal * signal = RACObserve(self, title);
    //
    //    [signal subscribeNext:^(id x) {
    //
    //
    //    }];

    //    常见的使用方式,进行绑定,将我们一个已知的属性绑定到另外一个对象的属性上
    //    RAC() = RACObserve(<#TARGET#>, <#KEYPATH#>)

    [[[[[self authorizedSignal] filter:^BOOL(id value) {

        return [value boolValue];
    }] flattenMap:^RACStream *(id value){

        return [[[[[[self rac_signalForSelector:@selector(locationManager:didUpdateLocations:) fromProtocol:@protocol(CLLocationManagerDelegate)] map:^id(id value) {

            return value[1];
        }] merge:[[self rac_signalForSelector:@selector(locationManager:didFailWithError:) fromProtocol:@protocol(CLLocationManagerDelegate)]map:^id(id value) {

            return [RACSignal error:value[1]];
        }]] take:1] initially:^{

            [self.manager startUpdatingLocation];
        }] finally:^{

            [self.manager stopUpdatingLocation];
        }];
    }] flattenMap:^RACStream *(id value) {

        CLLocation * location =[value firstObject];
        return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {

            [self.geocoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {

                if (error) {

                    [subscriber sendError:error];
                }else{

                    [subscriber sendNext:[placemarks firstObject]];
                    [subscriber sendCompleted];
                }
            }];
            return [RACDisposable disposableWithBlock:^{


            }];
        }];
    }] subscribeNext:^(id x) {

        NSLog(@"%@",x);
        self.placeLabel.text =[x addressDictionary][@"Name"];
        NSLog(@"地址%@",self.placeLabel.text);
    }];
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值