From 96e08572272e1614581c56eac30fccc87573168f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Minh=20Nguye=CC=82=CC=83n?= Date: Mon, 18 Apr 2016 01:56:38 -0700 Subject: [PATCH] [ios] Prompt for access token in iosapp If no access token is found in user defaults at launch, iosapp prompts the user for an access token. Renamed the user defaults key for the access token to conform to Cocoa naming conventions. Fixes #4696. --- platform/ios/app/MBXAppDelegate.h | 2 ++ platform/ios/app/MBXAppDelegate.m | 8 ++++---- platform/ios/app/MBXViewController.m | 30 ++++++++++++++++++++++++++++ 3 files changed, 36 insertions(+), 4 deletions(-) diff --git a/platform/ios/app/MBXAppDelegate.h b/platform/ios/app/MBXAppDelegate.h index da081fdcd..8145d3b8c 100644 --- a/platform/ios/app/MBXAppDelegate.h +++ b/platform/ios/app/MBXAppDelegate.h @@ -1,5 +1,7 @@ #import +extern NSString * const MBXMapboxAccessTokenDefaultsKey; + @interface MBXAppDelegate : UIResponder @property (strong, nonatomic) UIWindow *window; diff --git a/platform/ios/app/MBXAppDelegate.m b/platform/ios/app/MBXAppDelegate.m index 9ceee7cad..c2834bfa7 100644 --- a/platform/ios/app/MBXAppDelegate.m +++ b/platform/ios/app/MBXAppDelegate.m @@ -2,6 +2,8 @@ #import "MBXViewController.h" #import +NSString * const MBXMapboxAccessTokenDefaultsKey = @"MBXMapboxAccessToken"; + @implementation MBXAppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions @@ -12,14 +14,12 @@ if (accessToken) { // Store to preferences so that we can launch the app later on without having to specify // token. - [[NSUserDefaults standardUserDefaults] setObject:accessToken forKey:@"access_token"]; + [[NSUserDefaults standardUserDefaults] setObject:accessToken forKey:MBXMapboxAccessTokenDefaultsKey]; } else { // Try to retrieve from preferences, maybe we've stored them there previously and can reuse // the token. - accessToken = [[NSUserDefaults standardUserDefaults] objectForKey:@"access_token"]; + accessToken = [[NSUserDefaults standardUserDefaults] objectForKey:MBXMapboxAccessTokenDefaultsKey]; } - if ( ! accessToken) NSLog(@"No access token set. Mapbox vector tiles won't work."); - [MGLAccountManager setAccessToken:accessToken]; } diff --git a/platform/ios/app/MBXViewController.m b/platform/ios/app/MBXViewController.m index 3ba081682..55ced8131 100644 --- a/platform/ios/app/MBXViewController.m +++ b/platform/ios/app/MBXViewController.m @@ -1,4 +1,6 @@ #import "MBXViewController.h" + +#import "MBXAppDelegate.h" #import "MBXCustomCalloutView.h" #import "MBXOfflinePacksTableViewController.h" @@ -57,6 +59,34 @@ static const CLLocationCoordinate2D WorldTourDestinations[] = { [self cycleStyles:self]; [self restoreState:nil]; + + if ( ! [MGLAccountManager accessToken].length) + { + UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Access Token" message:@"Enter your Mapbox access token to load Mapbox-hosted tiles and styles:" preferredStyle:UIAlertControllerStyleAlert]; + [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) + { + textField.keyboardType = UIKeyboardTypeURL; + textField.autocorrectionType = UITextAutocorrectionTypeNo; + textField.autocapitalizationType = UITextAutocapitalizationTypeNone; + }]; + + [alertController addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil]]; + UIAlertAction *OKAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) + { + UITextField *textField = alertController.textFields.firstObject; + NSString *accessToken = textField.text; + [[NSUserDefaults standardUserDefaults] setObject:accessToken forKey:MBXMapboxAccessTokenDefaultsKey]; + [MGLAccountManager setAccessToken:accessToken]; + [self.mapView reloadStyle:self]; + }]; + [alertController addAction:OKAction]; + + if ([alertController respondsToSelector:@selector(setPreferredAction:)]) + { + alertController.preferredAction = OKAction; + } + [self presentViewController:alertController animated:YES completion:nil]; + } } - (void)saveState:(__unused NSNotification *)notification