From e404eeae1bcd1edcbf4574bb14b3640d1d300038 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Minh=20Nguye=CC=82=CC=83n?= Date: Thu, 21 Apr 2016 17:52:16 -0700 Subject: [PATCH] [ios] Fixed crash on missing access token MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixed a race condition in which the access token alert controller would fail to present because the view controller hadn’t yet entered the view hierarchy, plus a downstream crash caused by the access token being missing. --- platform/ios/app/MBXViewController.m | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/platform/ios/app/MBXViewController.m b/platform/ios/app/MBXViewController.m index 2f4caf993..d8bdc6d99 100644 --- a/platform/ios/app/MBXViewController.m +++ b/platform/ios/app/MBXViewController.m @@ -54,12 +54,19 @@ static const CLLocationCoordinate2D WorldTourDestinations[] = { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(restoreState:) name:UIApplicationWillEnterForegroundNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(saveState:) name:UIApplicationWillTerminateNotification object:nil]; - self.styleIndex = -1; - [self cycleStyles:self]; - [self restoreState:nil]; +} + +- (void)viewDidAppear:(BOOL)animated +{ + [super viewDidAppear:animated]; - if ( ! [MGLAccountManager accessToken].length) + if ([MGLAccountManager accessToken].length) + { + self.styleIndex = -1; + [self cycleStyles:self]; + } + else { 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) @@ -76,6 +83,9 @@ static const CLLocationCoordinate2D WorldTourDestinations[] = { NSString *accessToken = textField.text; [[NSUserDefaults standardUserDefaults] setObject:accessToken forKey:MBXMapboxAccessTokenDefaultsKey]; [MGLAccountManager setAccessToken:accessToken]; + + self.styleIndex = -1; + [self cycleStyles:self]; [self.mapView reloadStyle:self]; }]; [alertController addAction:OKAction];