Skip to content

Commit

Permalink
fix #35
Browse files Browse the repository at this point in the history
  • Loading branch information
yixiang committed Mar 22, 2019
1 parent fd7cea2 commit 172c3dd
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 25 deletions.
Expand Up @@ -25,7 +25,8 @@ - (void)doraemon_swizzleLocationDelegate:(id)delegate {
SEL sel = methods[i].name;
if ([delegate respondsToSelector:sel]) {
if (![[DoraemonGPSMocker shareInstance] respondsToSelector:sel]) {
NSAssert(NO, @"你在Delegate %@ 中所使用的SEL %@,暂不支持,请联系DoraemonKit开发者",delegate,sel);
NSAssert(NO, @"Delegate : %@ not implementation SEL : %@",delegate,NSStringFromSelector(sel));

}
}
}
Expand Down
117 changes: 95 additions & 22 deletions iOS/DoraemonKit/Src/GPS/Function/DoraemonGPSMocker.m
Expand Up @@ -100,6 +100,28 @@ -(void)enumDelegate:(CLLocationManager*)manager block:(void (^)(id<CLLocationMan
}

#pragma mark - CLLocationManagerDelegate
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
[self enumDelegate:manager block:^(id<CLLocationManagerDelegate> delegate) {
if ([delegate respondsToSelector:@selector(locationManager:didUpdateToLocation:fromLocation:)]) {
[delegate locationManager:manager didUpdateToLocation:newLocation fromLocation:oldLocation];
}
}];
}

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
if (!self.isMocking) {
[self dispatchLocationUpdate:manager locations:locations];
}
}

- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading {
[self enumDelegate:manager block:^(id<CLLocationManagerDelegate> delegate) {
if ([delegate respondsToSelector:@selector(locationManager:didUpdateHeading:)]) {
[delegate locationManager:manager didUpdateHeading:newHeading];
}
}];
}

-(BOOL)locationManagerShouldDisplayHeadingCalibration:(CLLocationManager *)manager{
__block BOOL ret = NO;
[self enumDelegate:manager block:^(id<CLLocationManagerDelegate> delegate) {
Expand All @@ -111,18 +133,47 @@ -(BOOL)locationManagerShouldDisplayHeadingCalibration:(CLLocationManager *)manag
return ret;
}

-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
- (void)locationManager:(CLLocationManager *)manager
didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region {
[self enumDelegate:manager block:^(id<CLLocationManagerDelegate> delegate) {
if ([delegate respondsToSelector:@selector(locationManager:didUpdateToLocation:fromLocation:)]) {
[delegate locationManager:manager didUpdateToLocation:newLocation fromLocation:oldLocation];
if ([delegate respondsToSelector:@selector(locationManager:didDetermineState:forRegion:)]) {
[delegate locationManager:manager didDetermineState:state forRegion:region];
}
}];
}

-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{
- (void)locationManager:(CLLocationManager *)manager
didRangeBeacons:(NSArray<CLBeacon *> *)beacons inRegion:(CLBeaconRegion *)region{
[self enumDelegate:manager block:^(id<CLLocationManagerDelegate> delegate) {
if ([delegate respondsToSelector:@selector(locationManager:didChangeAuthorizationStatus:)]) {
[delegate locationManager:manager didChangeAuthorizationStatus:status];
if ([delegate respondsToSelector:@selector(locationManager:didRangeBeacons:inRegion:)]) {
[delegate locationManager:manager didRangeBeacons:beacons inRegion:region];
}
}];
}

- (void)locationManager:(CLLocationManager *)manager rangingBeaconsDidFailForRegion:(CLBeaconRegion *)region
withError:(NSError *)error{
[self enumDelegate:manager block:^(id<CLLocationManagerDelegate> delegate) {
if ([delegate respondsToSelector:@selector(locationManager:rangingBeaconsDidFailForRegion:withError:)]) {
[delegate locationManager:manager rangingBeaconsDidFailForRegion:region withError:error];
}
}];
}

- (void)locationManager:(CLLocationManager *)manager
didEnterRegion:(CLRegion *)region{
[self enumDelegate:manager block:^(id<CLLocationManagerDelegate> delegate) {
if ([delegate respondsToSelector:@selector(locationManager:didEnterRegion:)]) {
[delegate locationManager:manager didEnterRegion:region];
}
}];
}

- (void)locationManager:(CLLocationManager *)manager
didExitRegion:(CLRegion *)region{
[self enumDelegate:manager block:^(id<CLLocationManagerDelegate> delegate) {
if ([delegate respondsToSelector:@selector(locationManager:didExitRegion:)]) {
[delegate locationManager:manager didExitRegion:region];
}
}];
}
Expand All @@ -135,27 +186,28 @@ -(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)
}];
}

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
if (!self.isMocking) {
[self dispatchLocationUpdate:manager locations:locations];
}
- (void)locationManager:(CLLocationManager *)manager
monitoringDidFailForRegion:(nullable CLRegion *)region
withError:(NSError *)error{
[self enumDelegate:manager block:^(id<CLLocationManagerDelegate> delegate) {
if ([delegate respondsToSelector:@selector(locationManager:monitoringDidFailForRegion:withError:)]) {
[delegate locationManager:manager monitoringDidFailForRegion:region withError:error];
}
}];
}

-(void)dispatchLocationUpdate:(CLLocationManager *)manager locations:(NSArray*)locations{
NSString *key = [NSString stringWithFormat:@"%p_delegate",manager];
id<CLLocationManagerDelegate> delegate = [_locationMonitor objectForKey:key];
if ([delegate respondsToSelector:@selector(locationManager:didUpdateLocations:)]) {
[delegate locationManager:manager didUpdateLocations:locations];
}else if ([delegate respondsToSelector:@selector(locationManager:didUpdateToLocation:fromLocation:)]){
[delegate locationManager:manager didUpdateToLocation:locations.firstObject fromLocation:self.oldLocation];
self.oldLocation = locations.firstObject;
}
-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{
[self enumDelegate:manager block:^(id<CLLocationManagerDelegate> delegate) {
if ([delegate respondsToSelector:@selector(locationManager:didChangeAuthorizationStatus:)]) {
[delegate locationManager:manager didChangeAuthorizationStatus:status];
}
}];
}

- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading {
- (void)locationManager:(CLLocationManager *)manager didStartMonitoringForRegion:(CLRegion *)region{
[self enumDelegate:manager block:^(id<CLLocationManagerDelegate> delegate) {
if ([delegate respondsToSelector:@selector(locationManager:didUpdateHeading:)]) {
[delegate locationManager:manager didUpdateHeading:newHeading];
if ([delegate respondsToSelector:@selector(locationManager:didStartMonitoringForRegion:)]) {
[delegate locationManager:manager didStartMonitoringForRegion:region];
}
}];
}
Expand All @@ -176,11 +228,32 @@ - (void)locationManagerDidResumeLocationUpdates:(CLLocationManager *)manager {
}];
}

- (void)locationManager:(CLLocationManager *)manager didFinishDeferredUpdatesWithError:(nullable NSError *)error{
[self enumDelegate:manager block:^(id<CLLocationManagerDelegate> delegate) {
if ([delegate respondsToSelector:@selector(locationManager:didFinishDeferredUpdatesWithError:)]) {
[delegate locationManager:manager didFinishDeferredUpdatesWithError:error];
}
}];
}

- (void)locationManager:(CLLocationManager *)manager didVisit:(CLVisit *)visit {
[self enumDelegate:manager block:^(id<CLLocationManagerDelegate> delegate) {
if ([delegate respondsToSelector:@selector(locationManager:didVisit:)]) {
[delegate locationManager:manager didVisit:visit];
}
}];
}



-(void)dispatchLocationUpdate:(CLLocationManager *)manager locations:(NSArray*)locations{
NSString *key = [NSString stringWithFormat:@"%p_delegate",manager];
id<CLLocationManagerDelegate> delegate = [_locationMonitor objectForKey:key];
if ([delegate respondsToSelector:@selector(locationManager:didUpdateLocations:)]) {
[delegate locationManager:manager didUpdateLocations:locations];
}else if ([delegate respondsToSelector:@selector(locationManager:didUpdateToLocation:fromLocation:)]){
[delegate locationManager:manager didUpdateToLocation:locations.firstObject fromLocation:self.oldLocation];
self.oldLocation = locations.firstObject;
}
}
@end
4 changes: 2 additions & 2 deletions iOS/DoraemonKitDemo/DoraemonKitDemo.xcodeproj/project.pbxproj
Expand Up @@ -572,7 +572,7 @@
GCC_PREFIX_HEADER = "DoraemonKitDemo/DoraemonKitDemo-PrefixHeader.pch";
INFOPLIST_FILE = DoraemonKitDemo/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = yixiang.DoraemonKitDemoX;
PRODUCT_BUNDLE_IDENTIFIER = yixiang.DoraemonKitDemoXXX;
PRODUCT_NAME = "$(TARGET_NAME)";
TARGETED_DEVICE_FAMILY = "1,2";
};
Expand All @@ -590,7 +590,7 @@
GCC_PREFIX_HEADER = "DoraemonKitDemo/DoraemonKitDemo-PrefixHeader.pch";
INFOPLIST_FILE = DoraemonKitDemo/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = yixiang.DoraemonKitDemoX;
PRODUCT_BUNDLE_IDENTIFIER = yixiang.DoraemonKitDemoXXX;
PRODUCT_NAME = "$(TARGET_NAME)";
TARGETED_DEVICE_FAMILY = "1,2";
};
Expand Down

0 comments on commit 172c3dd

Please sign in to comment.