From 1fc7a9b82ea0c064c20247f0902b3854fff1942f Mon Sep 17 00:00:00 2001 From: Fabian Guerra Soto Date: Mon, 18 Feb 2019 13:16:20 -0800 Subject: [ios, macos] Expose the url session configuration object. (#13886) The `MGLNetworkConfiguration` class was make public, and added `sessionConfiguration` property to let developers customize the `NSURLSessionConfiguration` object that is used to make HTTP requests in the SDK. --- platform/darwin/filesource-files.json | 1 + platform/darwin/src/MGLNetworkConfiguration.h | 29 +++++++++++--- platform/darwin/src/MGLNetworkConfiguration.m | 52 ++++++++++++++++++++------ platform/darwin/src/http_file_source.mm | 10 ++--- platform/ios/CHANGELOG.md | 1 + platform/ios/ios.xcodeproj/project.pbxproj | 4 +- platform/ios/sdk-files.json | 2 +- platform/ios/src/Mapbox.h | 1 + platform/macos/CHANGELOG.md | 1 + platform/macos/macos.xcodeproj/project.pbxproj | 2 +- platform/macos/sdk-files.json | 2 +- platform/macos/src/Mapbox.h | 1 + 12 files changed, 78 insertions(+), 28 deletions(-) diff --git a/platform/darwin/filesource-files.json b/platform/darwin/filesource-files.json index f435c43877..3ee96f9b95 100644 --- a/platform/darwin/filesource-files.json +++ b/platform/darwin/filesource-files.json @@ -2,6 +2,7 @@ "//": "This file can be edited manually and is the canonical source.", "sources": [ "platform/darwin/src/MGLLoggingConfiguration.m", + "platform/darwin/src/MGLNetworkConfiguration.m", "platform/darwin/src/http_file_source.mm", "platform/default/src/mbgl/storage/sqlite3.cpp" ], diff --git a/platform/darwin/src/MGLNetworkConfiguration.h b/platform/darwin/src/MGLNetworkConfiguration.h index aaac5a330c..6c56050aae 100644 --- a/platform/darwin/src/MGLNetworkConfiguration.h +++ b/platform/darwin/src/MGLNetworkConfiguration.h @@ -1,18 +1,37 @@ #import +#import "MGLFoundation.h" + NS_ASSUME_NONNULL_BEGIN /** - The MGLNetworkConfiguration object provides a global way to set a base API URL for - retrieval of map data, styles, and other resources. - - Currently, MGLNetworkConfiguration is a private API. + The `MGLNetworkConfiguration` object provides a global way to set a base + `NSURLSessionConfiguration`, and other resources. */ +MGL_EXPORT @interface MGLNetworkConfiguration : NSObject -/// Returns the shared instance of the `MGLNetworkConfiguration` class. +/** + Returns the shared instance of the `MGLNetworkConfiguration` class. + */ @property (class, nonatomic, readonly) MGLNetworkConfiguration *sharedManager; +/** + The session configuration object that is used by the `NSURLSession` objects + in this SDK. + + If this property is set to nil or if no session configuration is provided this property + is set to the default session configuration. + + Assign this object before instantiating any `MGLMapView` object. + + @note: `NSURLSession` objects store a copy of this configuration. Any further changes + to mutable properties on this configuration object passed to a session’s initializer + will not affect the behavior of that session. + + */ +@property (atomic, strong, null_resettable) NSURLSessionConfiguration *sessionConfiguration; + @end NS_ASSUME_NONNULL_END diff --git a/platform/darwin/src/MGLNetworkConfiguration.m b/platform/darwin/src/MGLNetworkConfiguration.m index 61422f7caa..8262e7eea1 100644 --- a/platform/darwin/src/MGLNetworkConfiguration.m +++ b/platform/darwin/src/MGLNetworkConfiguration.m @@ -1,23 +1,51 @@ #import "MGLNetworkConfiguration.h" +@interface MGLNetworkConfiguration () + +@property (strong) NSURLSessionConfiguration *sessionConfig; + +@end + @implementation MGLNetworkConfiguration + (instancetype)sharedManager { static dispatch_once_t onceToken; static MGLNetworkConfiguration *_sharedManager; - void (^setupBlock)(void) = ^{ - dispatch_once(&onceToken, ^{ - _sharedManager = [[self alloc] init]; - }); - }; - if (![[NSThread currentThread] isMainThread]) { - dispatch_sync(dispatch_get_main_queue(), ^{ - setupBlock(); - }); - } else { - setupBlock(); - } + dispatch_once(&onceToken, ^{ + _sharedManager = [[self alloc] init]; + _sharedManager.sessionConfiguration = nil; + }); + return _sharedManager; } +- (void)setSessionConfiguration:(NSURLSessionConfiguration *)sessionConfiguration { + @synchronized (self) { + if (sessionConfiguration == nil) { + _sessionConfig = [self defaultSessionConfiguration]; + } else { + _sessionConfig = sessionConfiguration; + } + } +} + +- (NSURLSessionConfiguration *)sessionConfiguration { + NSURLSessionConfiguration *sessionConfig = nil; + @synchronized (self) { + sessionConfig = _sessionConfig; + } + return sessionConfig; +} + +- (NSURLSessionConfiguration *)defaultSessionConfiguration { + NSURLSessionConfiguration* sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration]; + + sessionConfiguration.timeoutIntervalForResource = 30; + sessionConfiguration.HTTPMaximumConnectionsPerHost = 8; + sessionConfiguration.requestCachePolicy = NSURLRequestReloadIgnoringLocalCacheData; + sessionConfiguration.URLCache = nil; + + return sessionConfiguration; +} + @end diff --git a/platform/darwin/src/http_file_source.mm b/platform/darwin/src/http_file_source.mm index f11d44ea54..c3918ad62b 100644 --- a/platform/darwin/src/http_file_source.mm +++ b/platform/darwin/src/http_file_source.mm @@ -8,6 +8,7 @@ #import #import "MGLLoggingConfiguration_Private.h" +#import "MGLNetworkConfiguration.h" #include #include @@ -82,13 +83,10 @@ class HTTPFileSource::Impl { public: Impl() { @autoreleasepool { - NSURLSessionConfiguration* sessionConfig = - [NSURLSessionConfiguration defaultSessionConfiguration]; - sessionConfig.timeoutIntervalForResource = 30; - sessionConfig.HTTPMaximumConnectionsPerHost = 8; - sessionConfig.requestCachePolicy = NSURLRequestReloadIgnoringLocalCacheData; - sessionConfig.URLCache = nil; + NSURLSessionConfiguration *sessionConfig = + [MGLNetworkConfiguration sharedManager].sessionConfiguration; + session = [NSURLSession sessionWithConfiguration:sessionConfig]; userAgent = getUserAgent(); diff --git a/platform/ios/CHANGELOG.md b/platform/ios/CHANGELOG.md index aa7ad42b9e..34977e2a03 100644 --- a/platform/ios/CHANGELOG.md +++ b/platform/ios/CHANGELOG.md @@ -7,6 +7,7 @@ Mapbox welcomes participation and contributions from everyone. Please read [CONT * Fixed a bug where setting `MGLMapView.userTrackingMode` to `MGLUserTrackingModeFollowWithHeading` or `MGLUserTrackingModeFollowWithCourse` would be ignored if the user’s location was not already available. ([#13849](https://github.com/mapbox/mapbox-gl-native/pull/13849)) * Improved tilt gesture performance. ([#13902](https://github.com/mapbox/mapbox-gl-native/pull/13902)) * Fixed a bug where `layoutSubviews` was always called on device rotation, regardless of the application's or top-most view controller's supported orientations. ([#13900](https://github.com/mapbox/mapbox-gl-native/pull/13900)) +* Added `MGLNetworkConfiguration` class to customize the SDK's `NSURLSessionConfiguration` object. ([#11447](https://github.com/mapbox/mapbox-gl-native/pull/13886)) ## 4.8.0 - January 30, 2019 diff --git a/platform/ios/ios.xcodeproj/project.pbxproj b/platform/ios/ios.xcodeproj/project.pbxproj index 8d5a725848..0aa68018be 100644 --- a/platform/ios/ios.xcodeproj/project.pbxproj +++ b/platform/ios/ios.xcodeproj/project.pbxproj @@ -443,7 +443,7 @@ 96E516F22000596D00A02306 /* NSException+MGLAdditions.h in Headers */ = {isa = PBXBuildFile; fileRef = DA8848141CBAFA6200AB86E3 /* NSException+MGLAdditions.h */; }; 96E516F32000597100A02306 /* NSDictionary+MGLAdditions.h in Headers */ = {isa = PBXBuildFile; fileRef = 408AA8551DAEDA0800022900 /* NSDictionary+MGLAdditions.h */; }; 96E516F42000597D00A02306 /* NSData+MGLAdditions.h in Headers */ = {isa = PBXBuildFile; fileRef = 35305D461D22AA450007D005 /* NSData+MGLAdditions.h */; }; - 96E516F5200059B100A02306 /* MGLNetworkConfiguration.h in Headers */ = {isa = PBXBuildFile; fileRef = DD0902A41DB18F1B00C5BDCE /* MGLNetworkConfiguration.h */; }; + 96E516F5200059B100A02306 /* MGLNetworkConfiguration.h in Headers */ = {isa = PBXBuildFile; fileRef = DD0902A41DB18F1B00C5BDCE /* MGLNetworkConfiguration.h */; settings = {ATTRIBUTES = (Public, ); }; }; 96E516F6200059EC00A02306 /* MGLRendererFrontend.h in Headers */ = {isa = PBXBuildFile; fileRef = 92F2C3EC1F0E3C3A00268EC0 /* MGLRendererFrontend.h */; }; 96E516F720005A2700A02306 /* MGLAnnotationContainerView.h in Headers */ = {isa = PBXBuildFile; fileRef = 40EDA1BD1CFE0D4A00D9EA68 /* MGLAnnotationContainerView.h */; }; 96E516F820005A3000A02306 /* MGLCompactCalloutView.h in Headers */ = {isa = PBXBuildFile; fileRef = DA8848441CBAFB9800AB86E3 /* MGLCompactCalloutView.h */; }; @@ -722,7 +722,7 @@ DAF25720201902BC00367EF5 /* MGLHillshadeStyleLayerTests.mm in Sources */ = {isa = PBXBuildFile; fileRef = DAF2571F201902BB00367EF5 /* MGLHillshadeStyleLayerTests.mm */; }; DD0902A91DB1929D00C5BDCE /* MGLNetworkConfiguration.m in Sources */ = {isa = PBXBuildFile; fileRef = DD0902A21DB18DE700C5BDCE /* MGLNetworkConfiguration.m */; }; DD0902AA1DB1929D00C5BDCE /* MGLNetworkConfiguration.m in Sources */ = {isa = PBXBuildFile; fileRef = DD0902A21DB18DE700C5BDCE /* MGLNetworkConfiguration.m */; }; - DD0902AB1DB192A800C5BDCE /* MGLNetworkConfiguration.h in Headers */ = {isa = PBXBuildFile; fileRef = DD0902A41DB18F1B00C5BDCE /* MGLNetworkConfiguration.h */; }; + DD0902AB1DB192A800C5BDCE /* MGLNetworkConfiguration.h in Headers */ = {isa = PBXBuildFile; fileRef = DD0902A41DB18F1B00C5BDCE /* MGLNetworkConfiguration.h */; settings = {ATTRIBUTES = (Public, ); }; }; DD4823751D94AE6C00EB71B7 /* fill_filter_style.json in Resources */ = {isa = PBXBuildFile; fileRef = DD4823721D94AE6C00EB71B7 /* fill_filter_style.json */; }; DD4823761D94AE6C00EB71B7 /* line_filter_style.json in Resources */ = {isa = PBXBuildFile; fileRef = DD4823731D94AE6C00EB71B7 /* line_filter_style.json */; }; DD4823771D94AE6C00EB71B7 /* numeric_filter_style.json in Resources */ = {isa = PBXBuildFile; fileRef = DD4823741D94AE6C00EB71B7 /* numeric_filter_style.json */; }; diff --git a/platform/ios/sdk-files.json b/platform/ios/sdk-files.json index 0bf09e48fa..2267f3ce42 100644 --- a/platform/ios/sdk-files.json +++ b/platform/ios/sdk-files.json @@ -158,6 +158,7 @@ "MGLAccountManager.h": "platform/darwin/src/MGLAccountManager.h", "NSValue+MGLAdditions.h": "platform/darwin/src/NSValue+MGLAdditions.h", "MGLVectorStyleLayer.h": "platform/darwin/src/MGLVectorStyleLayer.h", + "MGLNetworkConfiguration.h": "platform/darwin/src/MGLNetworkConfiguration.h", "MGLComputedShapeSource.h": "platform/darwin/src/MGLComputedShapeSource.h", "MGLLoggingConfiguration.h": "platform/darwin/src/MGLLoggingConfiguration.h", "MGLLocationManager.h": "platform/darwin/src/MGLLocationManager.h", @@ -236,7 +237,6 @@ "MGLLoggingConfiguration_Private.h": "platform/darwin/src/MGLLoggingConfiguration_Private.h", "NSComparisonPredicate+MGLAdditions.h": "platform/darwin/src/NSComparisonPredicate+MGLAdditions.h", "MGLMapAccessibilityElement.h": "platform/ios/src/MGLMapAccessibilityElement.h", - "MGLNetworkConfiguration.h": "platform/darwin/src/MGLNetworkConfiguration.h", "MGLScaleBar.h": "platform/ios/src/MGLScaleBar.h", "NSString+MGLAdditions.h": "platform/darwin/src/NSString+MGLAdditions.h", "UIDevice+MGLAdditions.h": "platform/ios/src/UIDevice+MGLAdditions.h", diff --git a/platform/ios/src/Mapbox.h b/platform/ios/src/Mapbox.h index 829583be6a..43300b6aa5 100644 --- a/platform/ios/src/Mapbox.h +++ b/platform/ios/src/Mapbox.h @@ -71,3 +71,4 @@ FOUNDATION_EXPORT MGL_EXPORT const unsigned char MapboxVersionString[]; #import "NSPredicate+MGLAdditions.h" #import "MGLLocationManager.h" #import "MGLLoggingConfiguration.h" +#import "MGLNetworkConfiguration.h" diff --git a/platform/macos/CHANGELOG.md b/platform/macos/CHANGELOG.md index 4754ae8ddb..7e9c844059 100644 --- a/platform/macos/CHANGELOG.md +++ b/platform/macos/CHANGELOG.md @@ -7,6 +7,7 @@ * Added an `MGLStyle.performsPlacementTransitions` property to control how long it takes for colliding labels to fade out. ([#13565](https://github.com/mapbox/mapbox-gl-native/pull/13565)) * Fixed a crash when casting large numbers in `NSExpression`. ([#13580](https://github.com/mapbox/mapbox-gl-native/pull/13580)) * Added the `-[MGLShapeSource leavesOfCluster:offset:limit:]`, `-[MGLShapeSource childrenOfCluster:]`, `-[MGLShapeSource zoomLevelForExpandingCluster:]` methods for inspecting a cluster in an `MGLShapeSource`s created with the `MGLShapeSourceOptionClustered` option. Feature querying now returns clusters represented by `MGLPointFeatureCluster` objects (that conform to the `MGLCluster` protocol). ([#12952](https://github.com/mapbox/mapbox-gl-native/pull/12952) +* Added `MGLNetworkConfiguration` class to customize the SDK's `NSURLSessionConfiguration` object. ([#11447](https://github.com/mapbox/mapbox-gl-native/pull/13886)) ### Annotations diff --git a/platform/macos/macos.xcodeproj/project.pbxproj b/platform/macos/macos.xcodeproj/project.pbxproj index a618b85a7a..ad93c317ed 100644 --- a/platform/macos/macos.xcodeproj/project.pbxproj +++ b/platform/macos/macos.xcodeproj/project.pbxproj @@ -283,7 +283,7 @@ DAF25716201901C200367EF5 /* MGLHillshadeStyleLayer.h in Headers */ = {isa = PBXBuildFile; fileRef = DAF25714201901C200367EF5 /* MGLHillshadeStyleLayer.h */; settings = {ATTRIBUTES = (Public, ); }; }; DAF25721201902C100367EF5 /* MGLHillshadeStyleLayerTests.mm in Sources */ = {isa = PBXBuildFile; fileRef = DAF2571D201902A500367EF5 /* MGLHillshadeStyleLayerTests.mm */; }; DD0902B21DB1AC6400C5BDCE /* MGLNetworkConfiguration.m in Sources */ = {isa = PBXBuildFile; fileRef = DD0902AF1DB1AC6400C5BDCE /* MGLNetworkConfiguration.m */; }; - DD0902B31DB1AC6400C5BDCE /* MGLNetworkConfiguration.h in Headers */ = {isa = PBXBuildFile; fileRef = DD0902B01DB1AC6400C5BDCE /* MGLNetworkConfiguration.h */; }; + DD0902B31DB1AC6400C5BDCE /* MGLNetworkConfiguration.h in Headers */ = {isa = PBXBuildFile; fileRef = DD0902B01DB1AC6400C5BDCE /* MGLNetworkConfiguration.h */; settings = {ATTRIBUTES = (Public, ); }; }; DD58A4C91D822C6700E1F038 /* MGLExpressionTests.mm in Sources */ = {isa = PBXBuildFile; fileRef = DD58A4C71D822C6200E1F038 /* MGLExpressionTests.mm */; }; /* End PBXBuildFile section */ diff --git a/platform/macos/sdk-files.json b/platform/macos/sdk-files.json index f7d2fd3426..3c728ad37d 100644 --- a/platform/macos/sdk-files.json +++ b/platform/macos/sdk-files.json @@ -109,6 +109,7 @@ "MGLFillStyleLayer.h": "platform/darwin/src/MGLFillStyleLayer.h", "MGLCoordinateFormatter.h": "platform/darwin/src/MGLCoordinateFormatter.h", "MGLShapeOfflineRegion.h": "platform/darwin/src/MGLShapeOfflineRegion.h", + "MGLNetworkConfiguration.h": "platform/darwin/src/MGLNetworkConfiguration.h", "MGLOverlay.h": "platform/darwin/src/MGLOverlay.h", "MGLPolyline.h": "platform/darwin/src/MGLPolyline.h", "MGLLineStyleLayer.h": "platform/darwin/src/MGLLineStyleLayer.h", @@ -170,7 +171,6 @@ "NSExpression+MGLPrivateAdditions.h": "platform/darwin/src/NSExpression+MGLPrivateAdditions.h", "NSCompoundPredicate+MGLAdditions.h": "platform/darwin/src/NSCompoundPredicate+MGLAdditions.h", "MGLSymbolStyleLayer_Private.h": "platform/darwin/src/MGLSymbolStyleLayer_Private.h", - "MGLNetworkConfiguration.h": "platform/darwin/src/MGLNetworkConfiguration.h", "NSProcessInfo+MGLAdditions.h": "platform/darwin/src/NSProcessInfo+MGLAdditions.h", "MGLRendererFrontend.h": "platform/darwin/src/MGLRendererFrontend.h", "NSValue+MGLStyleAttributeAdditions.h": "platform/darwin/src/NSValue+MGLStyleAttributeAdditions.h", diff --git a/platform/macos/src/Mapbox.h b/platform/macos/src/Mapbox.h index 0fd81a4df7..4d54c753df 100644 --- a/platform/macos/src/Mapbox.h +++ b/platform/macos/src/Mapbox.h @@ -66,3 +66,4 @@ FOUNDATION_EXPORT MGL_EXPORT const unsigned char MapboxVersionString[]; #import "NSExpression+MGLAdditions.h" #import "NSPredicate+MGLAdditions.h" #import "MGLLoggingConfiguration.h" +#import "MGLNetworkConfiguration.h" -- cgit v1.2.1