Fixed MapView crash on iOS 8 and earlier

Summary: public

[MKPinAnnotationView redPinColor] is only supported on iOS 9 and later. This caused React Native to crash on iOS 8 and earlier.

This fixes the crash by providing a forked implementation for different OS versions.

Reviewed By: tadeuzagallo, javache

Differential Revision: D2702737

fb-gh-sync-id: cd8984f1f3d42989001f3c571e325f1b4ba09ac8
This commit is contained in:
Nick Lockwood 2015-11-30 05:07:43 -08:00 committed by facebook-github-bot-7
parent c9dd4015f1
commit c63de5e2a5
2 changed files with 60 additions and 6 deletions

View File

@ -219,6 +219,10 @@ var MapView = React.createClass({
* The pin color. This can be any valid color string, or you can use one * The pin color. This can be any valid color string, or you can use one
* of the predefined PinColors constants. Applies to both standard pins * of the predefined PinColors constants. Applies to both standard pins
* and custom pin images. * and custom pin images.
*
* Note that on iOS 8 and earlier, only the standard PinColor constants
* are supported for regualr pins. For custom pin images, any tintColor
* value is supported on all iOS versions.
* @platform ios * @platform ios
*/ */
tintColor: React.PropTypes.string, tintColor: React.PropTypes.string,
@ -373,8 +377,10 @@ var MapView = React.createClass({
/** /**
* Standard iOS MapView pin color constants, to be used with the * Standard iOS MapView pin color constants, to be used with the
* `annotation.tintColor` property. You are not obliged to use these, * `annotation.tintColor` property. On iOS 8 and earlier these are the
* but they are useful for matching the standard iOS look and feel. * only supported values when using regular pins. On iOS 9 and later
* you are not obliged to use these, but they are useful for matching
* the standard iOS look and feel.
*/ */
let PinColors = RCTMapConstants && RCTMapConstants.PinColors; let PinColors = RCTMapConstants && RCTMapConstants.PinColors;
MapView.PinColors = PinColors && { MapView.PinColors = PinColors && {

View File

@ -23,6 +23,24 @@
static NSString *const RCTMapViewKey = @"MapView"; static NSString *const RCTMapViewKey = @"MapView";
#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_9_0
static NSString *const RCTMapPinRed = @"#ff3b30";
static NSString *const RCTMapPinGreen = @"#4cd964";
static NSString *const RCTMapPinPurple = @"#c969e0";
@implementation RCTConvert (MKPinAnnotationColor)
RCT_ENUM_CONVERTER(MKPinAnnotationColor, (@{
RCTMapPinRed: @(MKPinAnnotationColorRed),
RCTMapPinGreen: @(MKPinAnnotationColorGreen),
RCTMapPinPurple: @(MKPinAnnotationColorPurple)
}), MKPinAnnotationColorRed, unsignedIntegerValue)
@end
#endif
@interface RCTMapManager() <MKMapViewDelegate> @interface RCTMapManager() <MKMapViewDelegate>
@end @end
@ -60,11 +78,29 @@ RCT_CUSTOM_VIEW_PROPERTY(region, MKCoordinateRegion, RCTMap)
- (NSDictionary<NSString *, id> *)constantsToExport - (NSDictionary<NSString *, id> *)constantsToExport
{ {
NSString *red, *green, *purple;
#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_9_0
if (![MKPinAnnotationView respondsToSelector:@selector(redPinColor)]) {
red = RCTMapPinRed;
green = RCTMapPinGreen;
purple = RCTMapPinPurple;
} else
#endif
{
red = RCTColorToHexString([MKPinAnnotationView redPinColor].CGColor);
green = RCTColorToHexString([MKPinAnnotationView greenPinColor].CGColor);
purple = RCTColorToHexString([MKPinAnnotationView purplePinColor].CGColor);
}
return @{ return @{
@"PinColors": @{ @"PinColors": @{
@"RED": RCTColorToHexString([MKPinAnnotationView redPinColor].CGColor), @"RED": red,
@"GREEN": RCTColorToHexString([MKPinAnnotationView greenPinColor].CGColor), @"GREEN": green,
@"PURPLE": RCTColorToHexString([MKPinAnnotationView purplePinColor].CGColor), @"PURPLE": purple,
} }
}; };
} }
@ -127,7 +163,19 @@ RCT_CUSTOM_VIEW_PROPERTY(region, MKCoordinateRegion, RCTMap)
NSString *reuseIdentifier = NSStringFromClass([MKPinAnnotationView class]); NSString *reuseIdentifier = NSStringFromClass([MKPinAnnotationView class]);
annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:reuseIdentifier] ?: [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseIdentifier]; annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:reuseIdentifier] ?: [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
((MKPinAnnotationView *)annotationView).animatesDrop = annotation.animateDrop; ((MKPinAnnotationView *)annotationView).animatesDrop = annotation.animateDrop;
((MKPinAnnotationView *)annotationView).pinTintColor = annotation.tintColor ?: [MKPinAnnotationView redPinColor];
#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_9_0
if (![annotationView respondsToSelector:@selector(pinTintColor)]) {
NSString *hexColor = annotation.tintColor ? RCTColorToHexString(annotation.tintColor.CGColor) : RCTMapPinRed;
((MKPinAnnotationView *)annotationView).pinColor = [RCTConvert MKPinAnnotationColor:hexColor];
} else
#endif
{
((MKPinAnnotationView *)annotationView).pinTintColor = annotation.tintColor ?: [MKPinAnnotationView redPinColor];
}
} }
annotationView.canShowCallout = true; annotationView.canShowCallout = true;