[osx] Added OS X support for offline downloads

This commit is contained in:
Minh Nguyễn
2016-03-05 18:00:11 -08:00
committed by Minh Nguyễn
parent 081ee9fbef
commit e36218a866
26 changed files with 160 additions and 250 deletions

View File

@@ -50,6 +50,12 @@
'../platform/darwin/src/MGLDownloadController_Private.h',
'../platform/darwin/src/MGLDownloadRegion_Private.h',
'../platform/darwin/src/MGLTilePyramidDownloadRegion.mm',
'../platform/darwin/src/MGLAccountManager_Private.h',
'../platform/darwin/src/MGLAccountManager.m',
'../platform/darwin/src/NSBundle+MGLAdditions.h',
'../platform/darwin/src/NSBundle+MGLAdditions.m',
'../platform/darwin/src/NSProcessInfo+MGLAdditions.h',
'../platform/darwin/src/NSProcessInfo+MGLAdditions.m',
'../platform/ios/src/MGLMapboxEvents.h',
'../platform/ios/src/MGLMapboxEvents.m',
'../platform/ios/src/MGLAPIClient.h',
@@ -57,8 +63,6 @@
'../platform/ios/src/MGLLocationManager.h',
'../platform/ios/src/MGLLocationManager.m',
'../platform/ios/src/MGLMapView.mm',
'../platform/ios/src/MGLAccountManager_Private.h',
'../platform/ios/src/MGLAccountManager.m',
'../platform/ios/src/MGLUserLocation_Private.h',
'../platform/ios/src/MGLUserLocation.m',
'../platform/ios/src/MGLUserLocationAnnotationView.h',
@@ -67,10 +71,6 @@
'../platform/ios/src/MGLAnnotationImage.m',
'../platform/ios/src/MGLCompactCalloutView.h',
'../platform/ios/src/MGLCompactCalloutView.m',
'../platform/ios/src/NSBundle+MGLAdditions.h',
'../platform/ios/src/NSBundle+MGLAdditions.m',
'../platform/ios/src/NSProcessInfo+MGLAdditions.h',
'../platform/ios/src/NSProcessInfo+MGLAdditions.m',
'../platform/ios/vendor/SMCalloutView/SMCalloutView.h',
'../platform/ios/vendor/SMCalloutView/SMCalloutView.m',
'../platform/ios/vendor/Fabric/FABAttributes.h',

View File

@@ -43,8 +43,18 @@
'../platform/darwin/src/MGLPolyline.mm',
'../platform/darwin/src/MGLPolygon.mm',
'../platform/darwin/src/MGLMapCamera.mm',
'../platform/osx/src/MGLAccountManager_Private.h',
'../platform/osx/src/MGLAccountManager.m',
'../platform/darwin/src/MGLDownloadable.mm',
'../platform/darwin/src/MGLDownloadable_Private.h',
'../platform/darwin/src/MGLDownloadController.mm',
'../platform/darwin/src/MGLDownloadController_Private.h',
'../platform/darwin/src/MGLDownloadRegion_Private.h',
'../platform/darwin/src/MGLTilePyramidDownloadRegion.mm',
'../platform/darwin/src/MGLAccountManager_Private.h',
'../platform/darwin/src/MGLAccountManager.m',
'../platform/darwin/src/NSBundle+MGLAdditions.h',
'../platform/darwin/src/NSBundle+MGLAdditions.m',
'../platform/darwin/src/NSProcessInfo+MGLAdditions.h',
'../platform/darwin/src/NSProcessInfo+MGLAdditions.m',
'../platform/osx/src/MGLMapView_Private.h',
'../platform/osx/src/MGLMapView.mm',
'../platform/osx/src/MGLMapView+IBAdditions.m',
@@ -55,10 +65,6 @@
'../platform/osx/src/MGLAttributionButton.h',
'../platform/osx/src/MGLAttributionButton.m',
'../platform/osx/src/MGLAnnotationImage.m',
'../platform/osx/src/NSBundle+MGLAdditions.h',
'../platform/osx/src/NSBundle+MGLAdditions.m',
'../platform/osx/src/NSProcessInfo+MGLAdditions.h',
'../platform/osx/src/NSProcessInfo+MGLAdditions.m',
],
'variables': {

View File

@@ -0,0 +1,49 @@
#import <Foundation/Foundation.h>
#import "MGLTypes.h"
NS_ASSUME_NONNULL_BEGIN
/**
The MGLAccountManager object provides a global way to set a Mapbox API access
token, as well as other settings used framework-wide.
*/
@interface MGLAccountManager : NSObject
#pragma mark Authorizing Access
/**
Set the [Mapbox access token](https://www.mapbox.com/help/define-access-token/)
to be used by all instances of MGLMapView in the current application.
Mapbox-hosted vector tiles and styles require an API access token, which you
can obtain from the
[Mapbox account page](https://www.mapbox.com/studio/account/tokens/). Access
tokens associate requests to Mapbox’s vector tile and style APIs with your
Mapbox account. They also deter other developers from using your styles without
your permission.
@param accessToken A Mapbox access token. Calling this method with a value of
`nil` has no effect.
@note You must set the access token before attempting to load any Mapbox-hosted
style. Therefore, you should generally set it before creating an instance of
MGLMapView. The recommended way to set an access token is to add an entry to
your application’s Info.plist file with the key `MGLMapboxAccessToken` and
the type String. Alternatively, you may call this method from your
application delegate’s `-applicationDidFinishLaunching:` method.
*/
+ (void)setAccessToken:(nullable NSString *)accessToken;
/**
Returns the
[Mapbox access token](https://www.mapbox.com/help/define-access-token/) in use
by instances of MGLMapView in the current application.
*/
+ (nullable NSString *)accessToken;
+ (BOOL)mapboxMetricsEnabledSettingShownInApp __attribute__((deprecated("Telemetry settings are now always shown in the ℹ️ menu.")));
@end
NS_ASSUME_NONNULL_END

View File

@@ -1,18 +1,27 @@
#import "MGLAccountManager_Private.h"
#import "MGLMapboxEvents.h"
#import "MGLMapView.h"
#import "NSBundle+MGLAdditions.h"
#import "NSProcessInfo+MGLAdditions.h"
#import "NSString+MGLAdditions.h"
#if TARGET_OS_IPHONE || TARGET_OS_SIMULATOR
#import "MGLMapboxEvents.h"
#import "FABKitProtocol.h"
#import "Fabric+FABKits.h"
@interface MGLAccountManager() <FABKit>
@interface MGLAccountManager () <FABKit>
@property (atomic) NSString *accessToken;
@end
#else
@interface MGLAccountManager ()
@property (atomic) NSString *accessToken;
@end
#endif
@implementation MGLAccountManager
@@ -26,9 +35,7 @@
}
}
// Can be called from any thread.
//
+ (instancetype) sharedManager {
+ (instancetype)sharedManager {
if (NSProcessInfo.processInfo.mgl_isInterfaceBuilderDesignablesAgent) {
return nil;
}
@@ -39,37 +46,43 @@
_sharedManager = [[self alloc] init];
});
};
if ( ! [[NSThread currentThread] isMainThread]) {
if (![[NSThread currentThread] isMainThread]) {
dispatch_sync(dispatch_get_main_queue(), ^{
setupBlock();
});
}
else {
} else {
setupBlock();
}
return _sharedManager;
}
+ (BOOL) mapboxMetricsEnabledSettingShownInApp {
+ (BOOL)mapboxMetricsEnabledSettingShownInApp {
NSLog(@"mapboxMetricsEnabledSettingShownInApp is no longer necessary; the Mapbox iOS SDK has changed to always provide a telemetry setting in-app.");
return YES;
}
+ (void) setAccessToken:(NSString *) accessToken {
+ (void)setAccessToken:(NSString *)accessToken {
accessToken = [accessToken stringByTrimmingCharactersInSet:
[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if ( ! [accessToken length]) return;
if (!accessToken.length) {
return;
}
[MGLAccountManager sharedManager].accessToken = accessToken;
#if TARGET_OS_IPHONE || TARGET_OS_SIMULATOR
// Update MGLMapboxEvents
// NOTE: This is (likely) the initial setup of MGLMapboxEvents
[MGLMapboxEvents sharedManager];
#endif
}
+ (NSString *) accessToken {
+ (NSString *)accessToken {
return [MGLAccountManager sharedManager].accessToken;
}
#if TARGET_OS_IPHONE || TARGET_OS_SIMULATOR
#pragma mark - Fabric
+ (NSString *)bundleIdentifier {
@@ -85,15 +98,15 @@
if (fabric) {
NSDictionary *configuration = [fabric configurationDictionaryForKitClass:[MGLAccountManager class]];
if ( ! configuration || ! configuration[@"accessToken"]) {
NSLog(@"Configuration dictionary returned by Fabric was nil or doesn't have accessToken. Can't initialize MGLAccountManager.");
if (!configuration || !configuration[@"accessToken"]) {
NSLog(@"Configuration dictionary returned by Fabric was nil or doesn’t have accessToken. Can’t initialize MGLAccountManager.");
return;
}
[self setAccessToken:configuration[@"accessToken"]];
MGLAccountManager *sharedAccountManager = [self sharedManager];
NSLog(@"MGLAccountManager was initialized with access token: %@", sharedAccountManager.accessToken);
} else {
NSLog(@"MGLAccountManager is used in a project that doesn't have Fabric.");
NSLog(@"MGLAccountManager is used in a project that doesn’t have Fabric.");
}
// https://github.com/mapbox/mapbox-gl-native/issues/2966
@@ -105,4 +118,6 @@
[MGLMapView class];
}
#endif
@end

View File

@@ -2,9 +2,10 @@
@interface MGLAccountManager (Private)
/** Returns the shared instance of the `MGLAccountManager` class. */
/// Returns the shared instance of the `MGLAccountManager` class.
+ (instancetype)sharedManager;
/// The current global access token.
@property (atomic) NSString *accessToken;
@end

View File

@@ -29,9 +29,25 @@
- (instancetype)initWithFileName:(NSString *)fileName {
if (self = [super init]) {
#if TARGET_OS_IPHONE || TARGET_OS_SIMULATOR
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *fileCachePath = [paths.firstObject stringByAppendingPathComponent:fileName];
_mbglFileSource = new mbgl::DefaultFileSource(fileCachePath.UTF8String, [NSBundle mainBundle].resourceURL.path.UTF8String);
NSString *cachePath = [paths.firstObject stringByAppendingPathComponent:fileName];
#elif TARGET_OS_MAC
NSURL *cacheDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSCachesDirectory
inDomain:NSUserDomainMask
appropriateForURL:nil
create:YES
error:nil];
cacheDirectoryURL = [cacheDirectoryURL URLByAppendingPathComponent:
[NSBundle mainBundle].bundleIdentifier];
[[NSFileManager defaultManager] createDirectoryAtURL:cacheDirectoryURL
withIntermediateDirectories:YES
attributes:nil
error:nil];
NSURL *cacheURL = [cacheDirectoryURL URLByAppendingPathComponent:fileName];
NSString *cachePath = cacheURL ? cacheURL.path : @"";
#endif
_mbglFileSource = new mbgl::DefaultFileSource(cachePath.UTF8String, [NSBundle mainBundle].resourceURL.path.UTF8String);
// Observe for changes to the global access token (and find out the current one).
[[MGLAccountManager sharedManager] addObserver:self

View File

@@ -1,5 +1,9 @@
#import "MGLTilePyramidDownloadRegion.h"
#if !TARGET_OS_IPHONE && !TARGET_OS_SIMULATOR
#import <Cocoa/Cocoa.h>
#endif
#import "MGLDownloadRegion_Private.h"
#import "MGLGeometry_Private.h"
#import "MGLStyle.h"

View File

@@ -4,8 +4,10 @@ void mgl_linkProcessInfoCategory();
@interface NSProcessInfo (MGLAdditions)
/// Returns YES if the current process is Interface Builder’s helper process for
/// rendering designables.
/**
Returns YES if the current process is Interface Builder’s helper process for
rendering designables.
*/
- (BOOL)mgl_isInterfaceBuilderDesignablesAgent;
@end

View File

@@ -0,0 +1,17 @@
#import "NSProcessInfo+MGLAdditions.h"
void mgl_linkProcessInfoCategory() {}
#if TARGET_OS_IPHONE || TARGET_OS_SIMULATOR
static NSString * const MGLIBDesignablesAgentProcessName = @"IBDesignablesAgentCocoaTouch";
#elif TARGET_OS_MAC
static NSString * const MGLIBDesignablesAgentProcessName = @"IBDesignablesAgent";
#endif
@implementation NSProcessInfo (MGLAdditions)
- (BOOL)mgl_isInterfaceBuilderDesignablesAgent {
return [self.processName isEqualToString:MGLIBDesignablesAgentProcessName];
}
@end

View File

@@ -1,33 +0,0 @@
#import <Foundation/Foundation.h>
#import "MGLTypes.h"
NS_ASSUME_NONNULL_BEGIN
/** The MGLAccountManager object provides a global way to set a Mapbox API access token, as well as other settings used framework-wide. */
@interface MGLAccountManager : NSObject
#pragma mark Authorizing Access
/**
Set the Mapbox API access token for the framework.
You can set an access token on MGLAccountManager or on an individual map view. The same token is used throughout the framework.
@param accessToken The Mapbox API access token.
*/
+ (void)setAccessToken:(nullable NSString *)accessToken;
/**
Retreive the Mapbox API access token for the framework.
You can set an access token on MGLAccountManager or on an individual map view. The same token is used throughout the framework.
@return accessToken The Mapbox API access token.
*/
+ (nullable NSString *)accessToken;
+ (BOOL)mapboxMetricsEnabledSettingShownInApp __attribute__((deprecated("Telemetry settings are now always shown in the ℹ️ menu.")));
@end
NS_ASSUME_NONNULL_END

View File

@@ -1,9 +0,0 @@
#import <Foundation/Foundation.h>
void mgl_linkProcessInfoCategory();
@interface NSProcessInfo (MGLAdditions)
- (BOOL)mgl_isInterfaceBuilderDesignablesAgent;
@end

View File

@@ -1,12 +0,0 @@
#import "NSProcessInfo+MGLAdditions.h"
@implementation NSProcessInfo (MGLAdditions)
void mgl_linkProcessInfoCategory() {}
- (BOOL)mgl_isInterfaceBuilderDesignablesAgent
{
return [self.processName isEqualToString:@"IBDesignablesAgentCocoaTouch"];
}
@end

View File

@@ -1,44 +0,0 @@
#import <Foundation/Foundation.h>
#import "MGLTypes.h"
NS_ASSUME_NONNULL_BEGIN
/** The MGLAccountManager object provides a global way to set a Mapbox API
access token, as well as other settings used framework-wide. */
@interface MGLAccountManager : NSObject
#pragma mark Authorizing access
/** @name Authorizing Access */
/** Set the
[Mapbox access token](https://www.mapbox.com/help/define-access-token/) to
used by all instances of MGLMapView in the current application.
Mapbox-hosted vector tiles and styles require an API access token, which you
can obtain from the
[Mapbox account page](https://www.mapbox.com/studio/account/tokens/). Access
tokens associate requests to Mapbox’s vector tile and style APIs with your
Mapbox account. They also deter other developers from using your styles
without your permission.
@param accessToken A Mapbox access token. Calling this method with a value
of `nil` has no effect.
@note You must set the access token before attempting to load any
Mapbox-hosted style. Therefore, you should generally set it before
creating an instance of MGLMapView. The recommended way to set an access
token is to add an entry to your application’s Info.plist file with the
key `MGLMapboxAccessToken` and the type String. Alternatively, you may
call this method from your application delegate’s
-applicationDidFinishLaunching: method. */
+ (void)setAccessToken:(nullable NSString *)accessToken;
/** Returns the
[Mapbox access token](https://www.mapbox.com/help/define-access-token/) in
use by instances of MGLMapView in the current application. */
+ (nullable NSString *)accessToken;
@end
NS_ASSUME_NONNULL_END

View File

@@ -9,6 +9,9 @@ FOUNDATION_EXPORT const unsigned char MapboxVersionString[];
#import <Mapbox/MGLAccountManager.h>
#import <Mapbox/MGLAnnotation.h>
#import <Mapbox/MGLAnnotationImage.h>
#import <Mapbox/MGLDownloadable.h>
#import <Mapbox/MGLDownloadRegion.h>
#import <Mapbox/MGLDownloadController.h>
#import <Mapbox/MGLGeometry.h>
#import <Mapbox/MGLMapCamera.h>
#import <Mapbox/MGLMapView.h>
@@ -21,4 +24,5 @@ FOUNDATION_EXPORT const unsigned char MapboxVersionString[];
#import <Mapbox/MGLPolyline.h>
#import <Mapbox/MGLShape.h>
#import <Mapbox/MGLStyle.h>
#import <Mapbox/MGLTilePyramidDownloadRegion.h>
#import <Mapbox/MGLTypes.h>

View File

@@ -19,6 +19,8 @@ static void InitializeMapbox() {
[MGLAccountManager class];
[MGLAnnotationImage class];
[MGLDownloadable class];
[MGLDownloadController class];
[MGLMapCamera class];
[MGLMapView class];
[MGLMultiPoint class];
@@ -27,4 +29,5 @@ static void InitializeMapbox() {
[MGLPolyline class];
[MGLShape class];
[MGLStyle class];
[MGLTilePyramidDownloadRegion class];
}

View File

@@ -1,54 +0,0 @@
#import "MGLAccountManager_Private.h"
#import <MGLMapView.h>
@interface MGLAccountManager ()
@property (atomic) NSString *accessToken;
@end
@implementation MGLAccountManager
#pragma mark - Internal
+ (void)load {
// Read the initial configuration from Info.plist.
NSString *accessToken = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"MGLMapboxAccessToken"];
if (accessToken.length) {
self.accessToken = accessToken;
}
}
+ (instancetype)sharedManager {
static dispatch_once_t onceToken;
static MGLAccountManager *_sharedManager;
void (^setupBlock)() = ^{
dispatch_once(&onceToken, ^{
_sharedManager = [[self alloc] init];
});
};
if (![[NSThread currentThread] isMainThread]) {
dispatch_sync(dispatch_get_main_queue(), ^{
setupBlock();
});
}
else {
setupBlock();
}
return _sharedManager;
}
+ (void)setAccessToken:(NSString *)accessToken {
accessToken = [accessToken stringByTrimmingCharactersInSet:
[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if (![accessToken length]) return;
[MGLAccountManager sharedManager].accessToken = accessToken;
}
+ (NSString *)accessToken {
return [MGLAccountManager sharedManager].accessToken;
}
@end

View File

@@ -1,15 +0,0 @@
#import <MGLAccountManager.h>
@interface MGLAccountManager (Private)
/**
Returns the shared instance of the `MGLAccountManager` class.
This method can be called from any thread.
*/
+ (instancetype)sharedManager;
/// The current global access token.
@property (atomic) NSString *accessToken;
@end

View File

@@ -1,4 +1,4 @@
#import <MGLAnnotationImage.h>
#import "MGLAnnotationImage.h"
@interface MGLAnnotationImage ()

View File

@@ -1,4 +1,4 @@
#import <MGLMapView+IBAdditions.h>
#import "MGLMapView+IBAdditions.h"
#import "MGLMapView_Private.h"

View File

@@ -1,18 +1,18 @@
#import "MGLMapView_Private.h"
#import "MGLAccountManager_Private.h"
#import "MGLAttributionButton.h"
#import "MGLCompassCell.h"
#import "MGLOpenGLLayer.h"
#import "MGLStyle.h"
#import "../../darwin/src/MGLAccountManager_Private.h"
#import "../../darwin/src/MGLGeometry_Private.h"
#import "../../darwin/src/MGLMultiPoint_Private.h"
#import <MGLMapCamera.h>
#import <MGLPolygon.h>
#import <MGLPolyline.h>
#import <MGLAnnotationImage.h>
#import <MGLMapViewDelegate.h>
#import "MGLMapCamera.h"
#import "MGLPolygon.h"
#import "MGLPolyline.h"
#import "MGLAnnotationImage.h"
#import "MGLMapViewDelegate.h"
#import <mbgl/mbgl.hpp>
#import <mbgl/annotation/point_annotation.hpp>
@@ -30,8 +30,8 @@
#import <map>
#import <unordered_set>
#import "NSBundle+MGLAdditions.h"
#import "NSProcessInfo+MGLAdditions.h"
#import "../../darwin/src/NSBundle+MGLAdditions.h"
#import "../../darwin/src/NSProcessInfo+MGLAdditions.h"
#import "../../darwin/src/NSException+MGLAdditions.h"
#import "../../darwin/src/NSString+MGLAdditions.h"

View File

@@ -1,4 +1,4 @@
#import <MGLMapView.h>
#import "MGLMapView.h"
void mgl_linkMapViewIBCategory();

View File

@@ -1,16 +0,0 @@
#import <Foundation/Foundation.h>
#import "MGLTypes.h"
NS_ASSUME_NONNULL_BEGIN
void mgl_linkBundleCategory();
@interface NSBundle (MGLAdditions)
/// Returns the Mapbox.framework bundle.
+ (instancetype)mgl_frameworkBundle;
@end
NS_ASSUME_NONNULL_END

View File

@@ -1,13 +0,0 @@
#import "NSBundle+MGLAdditions.h"
#import "MGLAccountManager.h"
void mgl_linkBundleCategory() {}
@implementation NSBundle (MGLAdditions)
+ (instancetype)mgl_frameworkBundle {
return [self bundleForClass:[MGLAccountManager class]];
}
@end

View File

@@ -1,11 +0,0 @@
#import "NSProcessInfo+MGLAdditions.h"
void mgl_linkProcessInfoCategory() {}
@implementation NSProcessInfo (MGLAdditions)
- (BOOL)mgl_isInterfaceBuilderDesignablesAgent {
return [self.processName isEqualToString:@"IBDesignablesAgent"];
}
@end