Skip to content

Commit

Permalink
Reland "Reverts "[ios] Fix app extension not able to find assets from… (
Browse files Browse the repository at this point in the history
#46329)

Relands #46283

The original PR had a bug where the relative assets path is reset after not being found in the `bundle`. It should not be reset unless it was not found inside the info.plist in `bundle`

Fixes flutter/flutter#124292

[C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style
  • Loading branch information
Chris Yang authored Sep 28, 2023
1 parent 1d15b35 commit 7598ab4
Show file tree
Hide file tree
Showing 32 changed files with 1,177 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
FLUTTER_ASSERT_ARC

const NSString* kDefaultAssetPath = @"Frameworks/App.framework/flutter_assets";
static NSString* GetFlutterAssetsPathFromBundle(NSBundle* bundle, NSString* relativeAssetsPath);

NSBundle* FLTFrameworkBundleInternal(NSString* flutterFrameworkBundleID, NSURL* searchURL) {
NSDirectoryEnumerator<NSURL*>* frameworkEnumerator = [NSFileManager.defaultManager
Expand All @@ -29,7 +30,7 @@
}

NSBundle* FLTGetApplicationBundle() {
NSBundle* mainBundle = [NSBundle mainBundle];
NSBundle* mainBundle = NSBundle.mainBundle;
// App extension bundle is in <AppName>.app/PlugIns/Extension.appex.
if ([mainBundle.bundleURL.pathExtension isEqualToString:@"appex"]) {
// Up two levels.
Expand All @@ -48,7 +49,7 @@
flutterFrameworkBundle = [NSBundle bundleWithIdentifier:flutterFrameworkBundleID];
}
if (flutterFrameworkBundle == nil) {
flutterFrameworkBundle = [NSBundle mainBundle];
flutterFrameworkBundle = NSBundle.mainBundle;
}
return flutterFrameworkBundle;
}
Expand All @@ -58,13 +59,23 @@
}

NSString* FLTAssetsPathFromBundle(NSBundle* bundle) {
NSString* flutterAssetsPath = FLTAssetPath(bundle);
NSString* relativeAssetsPath = FLTAssetPath(bundle);
NSString* flutterAssetsPath = GetFlutterAssetsPathFromBundle(bundle, relativeAssetsPath);
if (flutterAssetsPath.length == 0) {
flutterAssetsPath = GetFlutterAssetsPathFromBundle(NSBundle.mainBundle, relativeAssetsPath);
}
return flutterAssetsPath;
}

static NSString* GetFlutterAssetsPathFromBundle(NSBundle* bundle, NSString* relativeAssetsPath) {
// 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:@""];

NSString* assetsPath = [bundle pathForResource:relativeAssetsPath ofType:nil];
if (assetsPath.length == 0) {
assetsPath = [[NSBundle mainBundle] pathForResource:flutterAssetsPath ofType:@""];
// In app extension, using full relative path (kDefaultAssetPath)
// returns nil when the app bundle is not loaded. Try to use
// the sub folder name, which can successfully return a valid path.
assetsPath = [bundle pathForResource:@"flutter_assets" ofType:nil];
}
return assetsPath;
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,21 +93,34 @@ - (void)testFLTAssetsURLFromBundle {
id mockBundle = OCMClassMock([NSBundle class]);
OCMStub([mockBundle objectForInfoDictionaryKey:@"FLTAssetsPath"]).andReturn(@"foo/assets");
NSString* resultAssetsPath = @"path/to/foo/assets";
OCMStub([mockBundle pathForResource:@"foo/assets" ofType:@""]).andReturn(resultAssetsPath);
OCMStub([mockBundle pathForResource:@"foo/assets" ofType:nil]).andReturn(resultAssetsPath);
NSString* path = FLTAssetsPathFromBundle(mockBundle);
XCTAssertEqualObjects(path, @"path/to/foo/assets");
}
{
// Found asset path in info.plist, is not overriden by main bundle
id mockBundle = OCMClassMock([NSBundle class]);
id mockMainBundle = OCMPartialMock(NSBundle.mainBundle);
OCMStub([mockBundle objectForInfoDictionaryKey:@"FLTAssetsPath"]).andReturn(@"foo/assets");
OCMStub([mockMainBundle objectForInfoDictionaryKey:@"FLTAssetsPath"]).andReturn(nil);
NSString* resultAssetsPath = @"path/to/foo/assets";
OCMStub([mockBundle pathForResource:@"foo/assets" ofType:nil]).andReturn(resultAssetsPath);
NSString* path = FLTAssetsPathFromBundle(mockBundle);
XCTAssertEqualObjects(path, @"path/to/foo/assets");
[mockMainBundle stopMocking];
}
{
// No asset path in info.plist, defaults to main bundle
id mockBundle = OCMClassMock([NSBundle class]);
id mockMainBundle = OCMPartialMock([NSBundle mainBundle]);
NSString* resultAssetsPath = @"path/to/foo/assets";
OCMStub([mockBundle pathForResource:@"Frameworks/App.framework/flutter_assets" ofType:@""])
OCMStub([mockBundle pathForResource:@"Frameworks/App.framework/flutter_assets" ofType:nil])
.andReturn(nil);
OCMStub([mockMainBundle pathForResource:@"Frameworks/App.framework/flutter_assets" ofType:@""])
OCMStub([mockMainBundle pathForResource:@"Frameworks/App.framework/flutter_assets" ofType:nil])
.andReturn(resultAssetsPath);
NSString* path = FLTAssetsPathFromBundle(mockBundle);
XCTAssertEqualObjects(path, @"path/to/foo/assets");
[mockMainBundle stopMocking];
}
}

Expand Down
Loading

0 comments on commit 7598ab4

Please sign in to comment.