[ios] fix asset url not found when loading app extension (flutter/engine#46073)

In b3fc48b91b, I refactored the assetsPath to use NSURL. It turns out that when the app bundle is not loaded (during launching app exgtension), the assetURL will return nil using the `URLForResource`, but the `pathForResource` successfully returns the raw path.

This PR reverts back to the raw path solution.

part of https://github.com/flutter/flutter/issues/124287

[C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style
This commit is contained in:
Chris Yang
2023-09-20 12:55:47 -07:00
committed by GitHub
parent 792830c13f
commit bc8e93271b
4 changed files with 25 additions and 29 deletions

View File

@@ -48,7 +48,7 @@ NSString* FLTAssetPath(NSBundle* bundle);
// If the key is not set, `flutter_assets` is used as the raw path value.
//
// If no valid asset is found under the raw path, returns nil.
NSURL* FLTAssetsURLFromBundle(NSBundle* bundle);
NSString* FLTAssetsPathFromBundle(NSBundle* bundle);
NS_ASSUME_NONNULL_END

View File

@@ -54,15 +54,17 @@ NSBundle* FLTFrameworkBundleWithIdentifier(NSString* flutterFrameworkBundleID) {
}
NSString* FLTAssetPath(NSBundle* bundle) {
return [bundle objectForInfoDictionaryKey:@"FLTAssetsPath"] ?: kDefaultAssetPath;
return [bundle objectForInfoDictionaryKey:@"FLTAssetsPath"] ?: @"flutter_assets";
}
NSURL* FLTAssetsURLFromBundle(NSBundle* bundle) {
NSString* FLTAssetsPathFromBundle(NSBundle* bundle) {
NSString* flutterAssetsPath = FLTAssetPath(bundle);
NSURL* assets = [bundle URLForResource:flutterAssetsPath withExtension:nil];
// Use the raw path solution so that asset path can be returned from unloaded bundles.
// See https://github.com/flutter/engine/pull/46073
NSString* assetsPath = [bundle pathForResource:flutterAssetsPath ofType:@""];
if (!assets) {
assets = [[NSBundle mainBundle] URLForResource:flutterAssetsPath withExtension:nil];
if (assetsPath.length == 0) {
assetsPath = [[NSBundle mainBundle] pathForResource:flutterAssetsPath ofType:@""];
}
return assets;
return assetsPath;
}

View File

@@ -139,19 +139,20 @@ flutter::Settings FLTDefaultSettingsForBundle(NSBundle* bundle, NSProcessInfo* p
// Checks to see if the flutter assets directory is already present.
if (settings.assets_path.empty()) {
NSURL* assetsURL = FLTAssetsURLFromBundle(bundle);
NSString* assetsPath = FLTAssetsPathFromBundle(bundle);
if (!assetsURL) {
if (assetsPath.length == 0) {
NSLog(@"Failed to find assets path for \"%@\"", bundle);
} else {
settings.assets_path = assetsURL.path.UTF8String;
settings.assets_path = assetsPath.UTF8String;
// Check if there is an application kernel snapshot in the assets directory we could
// potentially use. Looking for the snapshot makes sense only if we have a VM that can use
// it.
if (!flutter::DartVM::IsRunningPrecompiledCode()) {
NSURL* applicationKernelSnapshotURL =
[assetsURL URLByAppendingPathComponent:@(kApplicationKernelSnapshotFileName)];
[NSURL URLWithString:@(kApplicationKernelSnapshotFileName)
relativeToURL:[NSURL fileURLWithPath:assetsPath]];
NSError* error;
if ([applicationKernelSnapshotURL checkResourceIsReachableAndReturnError:&error]) {
settings.application_kernel_asset = applicationKernelSnapshotURL.path.UTF8String;

View File

@@ -89,31 +89,24 @@ FLUTTER_ASSERT_ARC
- (void)testFLTAssetsURLFromBundle {
{
// Found asset path in info.plist (even not reachable)
// Found asset path in info.plist
id mockBundle = OCMClassMock([NSBundle class]);
OCMStub([mockBundle objectForInfoDictionaryKey:@"FLTAssetsPath"]).andReturn(@"foo/assets");
NSURL* mockAssetsURL = OCMClassMock([NSURL class]);
OCMStub([mockBundle URLForResource:@"foo/assets" withExtension:nil]).andReturn(mockAssetsURL);
OCMStub([mockAssetsURL checkResourceIsReachableAndReturnError:NULL]).andReturn(NO);
OCMStub([mockAssetsURL path]).andReturn(@"foo/assets");
NSURL* url = FLTAssetsURLFromBundle(mockBundle);
XCTAssertEqualObjects(url.path, @"foo/assets");
NSString* resultAssetsPath = @"path/to/foo/assets";
OCMStub([mockBundle pathForResource:@"foo/assets" ofType:@""]).andReturn(resultAssetsPath);
NSString* path = FLTAssetsPathFromBundle(mockBundle);
XCTAssertEqualObjects(path, @"path/to/foo/assets");
}
{
// No asset path in info.plist, defaults to main bundle
id mockBundle = OCMClassMock([NSBundle class]);
id mockMainBundle = OCMPartialMock([NSBundle mainBundle]);
NSURL* mockAssetsURL = OCMClassMock([NSURL class]);
OCMStub([mockBundle URLForResource:@"Frameworks/App.framework/flutter_assets"
withExtension:nil])
.andReturn(nil);
OCMStub([mockAssetsURL checkResourceIsReachableAndReturnError:NULL]).andReturn(NO);
OCMStub([mockAssetsURL path]).andReturn(@"path/to/foo/assets");
OCMStub([mockMainBundle URLForResource:@"Frameworks/App.framework/flutter_assets"
withExtension:nil])
.andReturn(mockAssetsURL);
NSURL* url = FLTAssetsURLFromBundle(mockBundle);
XCTAssertEqualObjects(url.path, @"path/to/foo/assets");
NSString* resultAssetsPath = @"path/to/foo/assets";
OCMStub([mockBundle pathForResource:@"flutter_assets" ofType:@""]).andReturn(nil);
OCMStub([mockMainBundle pathForResource:@"flutter_assets" ofType:@""])
.andReturn(resultAssetsPath);
NSString* path = FLTAssetsPathFromBundle(mockBundle);
XCTAssertEqualObjects(path, @"path/to/foo/assets");
}
}