From de82bf9cb6cc808f2ec6e8624069441d62ab5447 Mon Sep 17 00:00:00 2001 From: wwwcg Date: Wed, 11 Sep 2024 19:12:30 +0800 Subject: [PATCH] fix(ios): resolve sandbox dir issue in debug mode (#4025) --- framework/ios/base/bridge/HippyBridge.h | 2 +- framework/ios/base/bridge/HippyBridge.mm | 12 +++++---- .../ios/module/loader/HippyFileHandler.mm | 2 +- modules/ios/base/HippyUtils.h | 6 +++++ modules/ios/base/HippyUtils.m | 17 +++++++++++++ renderer/native/ios/renderer/HippyRootView.mm | 3 ++- tests/ios/HippyUtilsTest.m | 25 +++++++++++++++++++ 7 files changed, 59 insertions(+), 8 deletions(-) diff --git a/framework/ios/base/bridge/HippyBridge.h b/framework/ios/base/bridge/HippyBridge.h index 836e51f9ca5..addcc0193e9 100644 --- a/framework/ios/base/bridge/HippyBridge.h +++ b/framework/ios/base/bridge/HippyBridge.h @@ -214,7 +214,7 @@ HIPPY_EXTERN NSString *HippyBridgeModuleNameForClass(Class bridgeModuleClass); @property (nonatomic, copy, readonly) NSArray *bundleURLs; /// Path of sandbox directory -@property (nonatomic, strong) NSURL *sandboxDirectory; +@property (nonatomic, strong) NSString *sandboxDirectory; /// Shared data between different rootViews on same bridge. /// Set by HippyRootView when runHippyApplication. diff --git a/framework/ios/base/bridge/HippyBridge.mm b/framework/ios/base/bridge/HippyBridge.mm index cc396647291..e89b6e9ccbd 100644 --- a/framework/ios/base/bridge/HippyBridge.mm +++ b/framework/ios/base/bridge/HippyBridge.mm @@ -155,7 +155,6 @@ @interface HippyBridge() { BOOL _valid; HippyBundleOperationQueue *_bundlesQueue; NSMutableArray *_bundleURLs; - NSURL *_sandboxDirectory; std::shared_ptr _uriLoader; std::shared_ptr _rootNode; @@ -472,9 +471,10 @@ - (void)setUp { [self loadPendingVendorBundleURLIfNeeded]; // Set the default sandbox directory - [self setSandboxDirectory:[_pendingLoadingVendorBundleURL URLByDeletingLastPathComponent]]; -} + NSString *sandboxDir = [HippyUtils getBaseDirFromResourcePath:_pendingLoadingVendorBundleURL]; + [self setSandboxDirectory:sandboxDir]; +} /// 加载初始化bridge时传入的Bundle URL - (void)loadPendingVendorBundleURLIfNeeded { @@ -1195,10 +1195,12 @@ - (void)registerModuleForFrameUpdates:(id)module withModuleDa [_displayLink registerModuleForFrameUpdates:module withModuleData:moduleData]; } -- (void)setSandboxDirectory:(NSURL *)sandboxDirectory { +- (void)setSandboxDirectory:(NSString *)sandboxDirectory { if (![_sandboxDirectory isEqual:sandboxDirectory]) { _sandboxDirectory = sandboxDirectory; - [self.javaScriptExecutor setSandboxDirectory:[sandboxDirectory absoluteString]]; + if (sandboxDirectory) { + [self.javaScriptExecutor setSandboxDirectory:sandboxDirectory]; + } } } diff --git a/framework/ios/module/loader/HippyFileHandler.mm b/framework/ios/module/loader/HippyFileHandler.mm index 8d8678ecf9e..f74be7b4218 100644 --- a/framework/ios/module/loader/HippyFileHandler.mm +++ b/framework/ios/module/loader/HippyFileHandler.mm @@ -90,7 +90,7 @@ return; } - NSURL *absoluteURL = AbsoluteURLFromHippyFileURL(url, bridge.sandboxDirectory); + NSURL *absoluteURL = AbsoluteURLFromHippyFileURL(url, [NSURL URLWithString:bridge.sandboxDirectory]); if ([absoluteURL isFileURL] || [absoluteURL isFileReferenceURL]) { void (^opBlock)() = ^{ NSError *error; diff --git a/modules/ios/base/HippyUtils.h b/modules/ios/base/HippyUtils.h index 1e1a17dc098..d1c599c45aa 100644 --- a/modules/ios/base/HippyUtils.h +++ b/modules/ios/base/HippyUtils.h @@ -160,6 +160,12 @@ HIPPY_EXTERN NSStringEncoding HippyGetStringEncodingFromURLResponse(NSURLRespons /// users may call it dynamically to determine the Hippy version number. + (NSString *)sdkVersion; +/// Get base directory from given file resource URL. +/// If it is a file URL, return the directory by deleting the last path component. +/// Otherwise, return the string truncated after the last slash. +/// - Parameter url: The given URL. ++ (NSString *)getBaseDirFromResourcePath:(NSURL *)url; + @end diff --git a/modules/ios/base/HippyUtils.m b/modules/ios/base/HippyUtils.m index e9eb53b5aa8..189601cb89f 100644 --- a/modules/ios/base/HippyUtils.m +++ b/modules/ios/base/HippyUtils.m @@ -712,4 +712,21 @@ + (NSString *)sdkVersion { return @(HIPPY_STR(HIPPY_VERSION)); } ++ (NSString *)getBaseDirFromResourcePath:(NSURL *)url { + NSString *urlString = url.absoluteString; + NSRange lastSlashRange = [urlString rangeOfString:@"/" options:NSBackwardsSearch]; + if (lastSlashRange.location == NSNotFound) { + // Return the original string if no slash is found + return urlString; + } + + if ([url isFileURL] || [url isFileReferenceURL]) { + // For file URLs, remove the last path component + return [url URLByDeletingLastPathComponent].absoluteString; + } else{ + // For http/https URLs, truncate after the last slash in the base URL + return [urlString substringToIndex:lastSlashRange.location + 1]; + } +} + @end diff --git a/renderer/native/ios/renderer/HippyRootView.mm b/renderer/native/ios/renderer/HippyRootView.mm index a5389fed0e2..d08fed7628a 100644 --- a/renderer/native/ios/renderer/HippyRootView.mm +++ b/renderer/native/ios/renderer/HippyRootView.mm @@ -28,6 +28,7 @@ #import "HippyBridge.h" #import "Hippybridge+PerformanceAPI.h" #import "HippyUIManager.h" +#import "HippyUtils.h" #import "HippyDeviceBaseInfo.h" #import "HippyTouchHandler.h" #import "HippyJSExecutor.h" @@ -158,7 +159,7 @@ - (instancetype)initWithBridge:(HippyBridge *)bridge _hasBusinessBundleToLoad = YES; // Set the default sandbox directory - [bridge setSandboxDirectory:[businessURL URLByDeletingLastPathComponent]]; + [bridge setSandboxDirectory:[HippyUtils getBaseDirFromResourcePath:businessURL]]; } if (self = [self initWithBridge:bridge moduleName:moduleName diff --git a/tests/ios/HippyUtilsTest.m b/tests/ios/HippyUtilsTest.m index fb41d388923..1b9e3226e97 100644 --- a/tests/ios/HippyUtilsTest.m +++ b/tests/ios/HippyUtilsTest.m @@ -86,5 +86,30 @@ - (void)testHippyURLWithString { } +- (void)testGetBaseDirFromResourcePath { + // Test with an HTTP URL + NSURL *httpURL = [NSURL URLWithString:@"http://localhost:38989/index.bundle?platform=ios"]; + NSString *expectedBaseHTTPURL = @"http://localhost:38989/"; + NSString *baseHTTPURLString = [HippyUtils getBaseDirFromResourcePath:httpURL]; + XCTAssertEqualObjects(baseHTTPURLString, expectedBaseHTTPURL, @"The base URL should be truncated after the last slash."); + + // Test with a file URL + NSURL *fileURL = [NSURL fileURLWithPath:@"/Users/username/Documents/test.txt"]; + NSString *expectedBaseFileURL = @"file:///Users/username/Documents/"; + NSString *baseFileURLString = [HippyUtils getBaseDirFromResourcePath:fileURL]; + XCTAssertEqualObjects(baseFileURLString, expectedBaseFileURL, @"The base file URL should be the directory path."); + + // Test with a URL with no slashes + NSURL *noSlashesURL = [NSURL URLWithString:@"mailto:user@example.com"]; + NSString *expectedNoSlashesURL = @"mailto:user@example.com"; + NSString *baseNoSlashesURLString = [HippyUtils getBaseDirFromResourcePath:noSlashesURL]; + XCTAssertEqualObjects(baseNoSlashesURLString, expectedNoSlashesURL, @"The URL with no slashes should be returned as is."); + + // Test with a nil URL + NSURL *nilURL = nil; + NSString *baseNilURLString = [HippyUtils getBaseDirFromResourcePath:nilURL]; + XCTAssertNil(baseNilURLString, @"The base URL should be nil for a nil input URL."); +} + @end