Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TIMOB-17665] Back port to stable Fixed user location for iOS 8 #49

Merged
merged 3 commits into from
Sep 15, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 22 additions & 1 deletion apidoc/View.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,27 @@ description: |
You can add [Route](Modules.Map.Route) objects to the map to create paths between
two or more points of interest.

To use the `userLocation` property in iOS 8 and later, add either the
[`NSLocationWhenInUseUsageDescription`](https://developer.apple.com/library/prerelease/ios/documentation/General/Reference/InfoPlistKeyReference/Articles/CocoaKeys.html#//apple_ref/doc/uid/TP40009251-SW26)
or
[`NSLocationAlwaysUsageDescription`](https://developer.apple.com/library/prerelease/ios/documentation/General/Reference/InfoPlistKeyReference/Articles/CocoaKeys.html#//apple_ref/doc/uid/TP40009251-SW18)
key to the iOS plist section of the project's `tiapp.xml` file.

<ti:app>
<ios>
<plist>
<dict>
<key>NSLocationAlwaysUsageDescription</key>
<string>
Specify the reason for accessing the user’s location information.
This appears in the alert dialog when asking the user for permission to
access their location.
</string>
</dict>
</plist>
</ios>
</ti:app>

extends: Titanium.UI.View
excludes: {
events: [ 'singletap', 'doubletap', 'dblclick', 'longpress', 'pinch',
Expand Down Expand Up @@ -371,7 +392,7 @@ properties:
map.
description: |
If `true`, the user's location is marked with a pin, and the My Location button will appear in the top
right corner of the screen.
right corner of the screen. Starting in iOS 8, permissions must be added to tiapp.xml. Details in description.
type: Boolean
default: false

Expand Down
7 changes: 5 additions & 2 deletions ios/Classes/TiMapView.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
@end


@interface TiMapView : TiUIView<MKMapViewDelegate> {
@interface TiMapView : TiUIView<MKMapViewDelegate, CLLocationManagerDelegate> {
MKMapView *map;
BOOL regionFits;
BOOL animate;
Expand All @@ -30,7 +30,10 @@

// routes
// dictionaries for object tracking and association
CFMutableDictionaryRef mapLine2View; // MKPolyline(route line) -> MKPolylineView(route view)
CFMutableDictionaryRef mapLine2View; // MKPolyline(route line) -> MKPolylineView(route view)

// Location manager needed for iOS 8 permissions
CLLocationManager *locationManager;
}

@property (nonatomic, readonly) CLLocationDegrees longitudeDelta;
Expand Down
36 changes: 34 additions & 2 deletions ios/Classes/TiMapView.m
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ -(void)dealloc
CFRelease(mapLine2View);
mapLine2View = nil;
}
RELEASE_TO_NIL(locationManager);
[super dealloc];
}

Expand Down Expand Up @@ -62,8 +63,10 @@ -(MKMapView*)map
map = [[MKMapView alloc] initWithFrame:CGRectZero];
map.delegate = self;
map.userInteractionEnabled = YES;
map.showsUserLocation = [TiUtils boolValue:[self.proxy valueForKey:@"userLocation"]]; // defaults
map.autoresizingMask = UIViewAutoresizingNone;
if (![TiUtils isIOS8OrGreater]) {
map.showsUserLocation = [TiUtils boolValue:[self.proxy valueForKey:@"userLocation"] def:NO];
}
[self addSubview:map];
mapLine2View = CFDictionaryCreateMutable(NULL, 10, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
//Initialize loaded state to YES. This will automatically go to NO if the map needs to download new data
Expand Down Expand Up @@ -434,7 +437,29 @@ -(void)setRegionFit_:(id)value
-(void)setUserLocation_:(id)value
{
ENSURE_SINGLE_ARG(value,NSObject);
[self map].showsUserLocation = [TiUtils boolValue:value];

// Release the locationManager in case it was already created
RELEASE_TO_NIL(locationManager);
BOOL userLocation = [TiUtils boolValue:value def:NO];
// if userLocation is true and this is iOS 8 or greater, then ask for permission
if (userLocation && [TiUtils isIOS8OrGreater]) {
// the locationManager needs to be created to permissions
locationManager = [[CLLocationManager alloc] init];
// set the "userLocation" on the delegate callback to avoid console warnings from the OS
locationManager.delegate = self;
if ([[NSBundle mainBundle] objectForInfoDictionaryKey:@"NSLocationAlwaysUsageDescription"]){
[locationManager requestAlwaysAuthorization];
} else if ([[NSBundle mainBundle] objectForInfoDictionaryKey:@"NSLocationWhenInUseUsageDescription"]){
[locationManager requestWhenInUseAuthorization];
} else {
NSLog(@"[ERROR] The keys NSLocationAlwaysUsageDescription or NSLocationWhenInUseUsageDescription are not defined in your tiapp.xml. Starting with iOS8 this is required.");
}
// Create the map
[self map];
} else {
// else, just apply the userLocation
[self map].showsUserLocation = userLocation;
}
}

-(void)setLocation_:(id)location
Expand Down Expand Up @@ -529,6 +554,13 @@ -(void)addOverlay:(MKPolyline*)polyline level:(MKOverlayLevel)level

#pragma mark Delegates

// Delegate for >= iOS 8
-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
if(status != kCLAuthorizationStatusAuthorized) return;
self.map.showsUserLocation = [TiUtils boolValue:[self.proxy valueForKey:@"userLocation"] def:NO];
}

// Delegate for >= iOS 7
- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id < MKOverlay >)overlay
{
Expand Down
2 changes: 2 additions & 0 deletions ios/documentation/changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Change Log
<pre>
v2.0.3 Fixed "userLocation" permissions for iOS 8 [TIMOB-17665]

v2.0.2 Fixed ignoring userLocation property during view creation [TIMOB-12733]

v2.0.1 Fixed annotation not showing leftButton rightButton [TC-3524]
Expand Down
2 changes: 1 addition & 1 deletion ios/manifest
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# this is your module manifest and used by Titanium
# during compilation, packaging, distribution, etc.
#
version: 2.0.2
version: 2.0.3
apiversion: 2
description: External version of Map module
author: Jeff Haynie & Jon Alter
Expand Down
Binary file removed ios/ti.map-iphone-2.0.2.zip
Binary file not shown.