Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 91ccb7d

Browse files
Chris Yangharryterkelsen
Chris Yang
authored andcommitted
Reland "Reverts "[ios] Fix app extension not able to find assets from… (#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
1 parent b5b138f commit 91ccb7d

File tree

32 files changed

+1177
-17
lines changed

32 files changed

+1177
-17
lines changed

shell/platform/darwin/common/framework/Source/FlutterNSBundleUtils.mm

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
FLUTTER_ASSERT_ARC
1010

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

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

3132
NSBundle* FLTGetApplicationBundle() {
32-
NSBundle* mainBundle = [NSBundle mainBundle];
33+
NSBundle* mainBundle = NSBundle.mainBundle;
3334
// App extension bundle is in <AppName>.app/PlugIns/Extension.appex.
3435
if ([mainBundle.bundleURL.pathExtension isEqualToString:@"appex"]) {
3536
// Up two levels.
@@ -48,7 +49,7 @@
4849
flutterFrameworkBundle = [NSBundle bundleWithIdentifier:flutterFrameworkBundleID];
4950
}
5051
if (flutterFrameworkBundle == nil) {
51-
flutterFrameworkBundle = [NSBundle mainBundle];
52+
flutterFrameworkBundle = NSBundle.mainBundle;
5253
}
5354
return flutterFrameworkBundle;
5455
}
@@ -58,13 +59,23 @@
5859
}
5960

6061
NSString* FLTAssetsPathFromBundle(NSBundle* bundle) {
61-
NSString* flutterAssetsPath = FLTAssetPath(bundle);
62+
NSString* relativeAssetsPath = FLTAssetPath(bundle);
63+
NSString* flutterAssetsPath = GetFlutterAssetsPathFromBundle(bundle, relativeAssetsPath);
64+
if (flutterAssetsPath.length == 0) {
65+
flutterAssetsPath = GetFlutterAssetsPathFromBundle(NSBundle.mainBundle, relativeAssetsPath);
66+
}
67+
return flutterAssetsPath;
68+
}
69+
70+
static NSString* GetFlutterAssetsPathFromBundle(NSBundle* bundle, NSString* relativeAssetsPath) {
6271
// Use the raw path solution so that asset path can be returned from unloaded bundles.
6372
// See https://github.com/flutter/engine/pull/46073
64-
NSString* assetsPath = [bundle pathForResource:flutterAssetsPath ofType:@""];
65-
73+
NSString* assetsPath = [bundle pathForResource:relativeAssetsPath ofType:nil];
6674
if (assetsPath.length == 0) {
67-
assetsPath = [[NSBundle mainBundle] pathForResource:flutterAssetsPath ofType:@""];
75+
// In app extension, using full relative path (kDefaultAssetPath)
76+
// returns nil when the app bundle is not loaded. Try to use
77+
// the sub folder name, which can successfully return a valid path.
78+
assetsPath = [bundle pathForResource:@"flutter_assets" ofType:nil];
6879
}
6980
return assetsPath;
7081
}

shell/platform/darwin/ios/framework/Source/FlutterDartProjectTest.mm

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,21 +93,34 @@ - (void)testFLTAssetsURLFromBundle {
9393
id mockBundle = OCMClassMock([NSBundle class]);
9494
OCMStub([mockBundle objectForInfoDictionaryKey:@"FLTAssetsPath"]).andReturn(@"foo/assets");
9595
NSString* resultAssetsPath = @"path/to/foo/assets";
96-
OCMStub([mockBundle pathForResource:@"foo/assets" ofType:@""]).andReturn(resultAssetsPath);
96+
OCMStub([mockBundle pathForResource:@"foo/assets" ofType:nil]).andReturn(resultAssetsPath);
9797
NSString* path = FLTAssetsPathFromBundle(mockBundle);
9898
XCTAssertEqualObjects(path, @"path/to/foo/assets");
9999
}
100+
{
101+
// Found asset path in info.plist, is not overriden by main bundle
102+
id mockBundle = OCMClassMock([NSBundle class]);
103+
id mockMainBundle = OCMPartialMock(NSBundle.mainBundle);
104+
OCMStub([mockBundle objectForInfoDictionaryKey:@"FLTAssetsPath"]).andReturn(@"foo/assets");
105+
OCMStub([mockMainBundle objectForInfoDictionaryKey:@"FLTAssetsPath"]).andReturn(nil);
106+
NSString* resultAssetsPath = @"path/to/foo/assets";
107+
OCMStub([mockBundle pathForResource:@"foo/assets" ofType:nil]).andReturn(resultAssetsPath);
108+
NSString* path = FLTAssetsPathFromBundle(mockBundle);
109+
XCTAssertEqualObjects(path, @"path/to/foo/assets");
110+
[mockMainBundle stopMocking];
111+
}
100112
{
101113
// No asset path in info.plist, defaults to main bundle
102114
id mockBundle = OCMClassMock([NSBundle class]);
103115
id mockMainBundle = OCMPartialMock([NSBundle mainBundle]);
104116
NSString* resultAssetsPath = @"path/to/foo/assets";
105-
OCMStub([mockBundle pathForResource:@"Frameworks/App.framework/flutter_assets" ofType:@""])
117+
OCMStub([mockBundle pathForResource:@"Frameworks/App.framework/flutter_assets" ofType:nil])
106118
.andReturn(nil);
107-
OCMStub([mockMainBundle pathForResource:@"Frameworks/App.framework/flutter_assets" ofType:@""])
119+
OCMStub([mockMainBundle pathForResource:@"Frameworks/App.framework/flutter_assets" ofType:nil])
108120
.andReturn(resultAssetsPath);
109121
NSString* path = FLTAssetsPathFromBundle(mockBundle);
110122
XCTAssertEqualObjects(path, @"path/to/foo/assets");
123+
[mockMainBundle stopMocking];
111124
}
112125
}
113126

0 commit comments

Comments
 (0)