Skip to content
This repository was archived by the owner on Aug 8, 2023. It is now read-only.

Commit 9b37141

Browse files
committed
[ios] MGLUserLocation, MGLUserLocationAnnotationView and MGLAnnotationImage now conforms to NSSecureCoding
1 parent f045c04 commit 9b37141

File tree

3 files changed

+81
-2
lines changed

3 files changed

+81
-2
lines changed

platform/darwin/test/MGLCodingTests.m

+42-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
#import <Mapbox/Mapbox.h>
22
#import <XCTest/XCTest.h>
33

4+
#if TARGET_OS_IPHONE
5+
#import "MGLUserLocation_Private.h"
6+
#endif
47

58
@interface MGLCodingTests : XCTestCase
69
@end
@@ -423,6 +426,44 @@ - (void)testAnnotationView {
423426
XCTAssertEqualObjects(NSStringFromCGVector(annotationView.centerOffset), NSStringFromCGVector(unarchivedAnnotationView.centerOffset));
424427
XCTAssertEqual(annotationView.scalesWithViewingDistance, unarchivedAnnotationView.scalesWithViewingDistance);
425428
}
426-
#end
429+
#endif
430+
431+
#if TARGET_OS_IPHONE
432+
- (void)testUserLocation {
433+
MGLUserLocation *userLocation = [[MGLUserLocation alloc] init];
434+
userLocation.location = [[CLLocation alloc] initWithLatitude:1 longitude:1];
435+
436+
NSString *filePath = [self temporaryFilePathForClass:[MGLUserLocation class]];
437+
[NSKeyedArchiver archiveRootObject:userLocation toFile:filePath];
438+
439+
MGLUserLocation *unarchivedUserLocation = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
440+
441+
XCTAssertEqualObjects(userLocation, unarchivedUserLocation);
442+
unarchivedUserLocation.location = [[CLLocation alloc] initWithLatitude:10 longitude:10];
443+
XCTAssertNotEqualObjects(userLocation, unarchivedUserLocation);
444+
}
445+
#endif
446+
447+
#if TARGET_OS_IPHONE
448+
- (void)testUserLocationAnnotationView {
449+
MGLUserLocationAnnotationView *annotationView = [[MGLUserLocationAnnotationView alloc] init];
450+
annotationView.enabled = NO;
451+
annotationView.selected = YES;
452+
annotationView.draggable = YES;
453+
annotationView.centerOffset = CGVectorMake(10, 10);
454+
annotationView.scalesWithViewingDistance = NO;
455+
456+
NSString *filePath = [self temporaryFilePathForClass:[MGLUserLocationAnnotationView class]];
457+
[NSKeyedArchiver archiveRootObject:annotationView toFile:filePath];
458+
459+
MGLUserLocationAnnotationView *unarchivedAnnotationView = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
460+
461+
XCTAssertEqual(annotationView.enabled, unarchivedAnnotationView.enabled);
462+
XCTAssertEqual(annotationView.selected, unarchivedAnnotationView.selected);
463+
XCTAssertEqual(annotationView.draggable, unarchivedAnnotationView.draggable);
464+
XCTAssertEqualObjects(NSStringFromCGVector(annotationView.centerOffset), NSStringFromCGVector(unarchivedAnnotationView.centerOffset));
465+
XCTAssertEqual(annotationView.scalesWithViewingDistance, unarchivedAnnotationView.scalesWithViewingDistance);
466+
}
467+
#endif
427468

428469
@end

platform/ios/src/MGLUserLocation.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ NS_ASSUME_NONNULL_BEGIN
1111
directly. Instead, you retrieve an existing MGLUserLocation object from the
1212
`userLocation` property of the map view displayed in your application.
1313
*/
14-
@interface MGLUserLocation : NSObject <MGLAnnotation>
14+
@interface MGLUserLocation : NSObject <MGLAnnotation, NSSecureCoding>
1515

1616
#pragma mark Determining the User’s Position
1717

platform/ios/src/MGLUserLocation.m

+38
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,44 @@ - (instancetype)initWithMapView:(MGLMapView *)mapView
2626
return self;
2727
}
2828

29+
+ (BOOL)supportsSecureCoding {
30+
return YES;
31+
}
32+
33+
- (instancetype)initWithCoder:(NSCoder *)decoder {
34+
if (self = [super init]) {
35+
_location = [decoder decodeObjectOfClass:[CLLocation class] forKey:@"location"];
36+
_title = [decoder decodeObjectOfClass:[NSString class] forKey:@"title"];
37+
_subtitle = [decoder decodeObjectOfClass:[NSString class] forKey:@"subtitle"];
38+
}
39+
return self;
40+
}
41+
42+
- (void)encodeWithCoder:(NSCoder *)coder {
43+
[coder encodeObject:_location forKey:@"location"];
44+
[coder encodeObject:_title forKey:@"title"];
45+
[coder encodeObject:_subtitle forKey:@"subtitle"];
46+
}
47+
48+
- (BOOL)isEqual:(id)other {
49+
if (self == other) return YES;
50+
if (![other isKindOfClass:[MGLUserLocation class]]) return NO;
51+
52+
MGLUserLocation *otherUserLocation = other;
53+
return ((!self.location && !otherUserLocation.location) || [self.location distanceFromLocation:otherUserLocation.location] == 0)
54+
&& ((!self.title && !otherUserLocation.title) || [self.title isEqualToString:otherUserLocation.title])
55+
&& ((!self.subtitle && !otherUserLocation.subtitle) || [self.subtitle isEqualToString:otherUserLocation.subtitle]);
56+
}
57+
58+
- (NSUInteger)hash {
59+
NSUInteger hash = [super hash];
60+
hash += [_location hash];
61+
hash += [_heading hash];
62+
hash += [_title hash];
63+
hash += [_subtitle hash];
64+
return hash;
65+
}
66+
2967
+ (BOOL)automaticallyNotifiesObserversForKey:(NSString *)key
3068
{
3169
return ! [key isEqualToString:@"location"] && ! [key isEqualToString:@"heading"];

0 commit comments

Comments
 (0)