Official website description: Add resources and pictures - Flutter Chinese documentation - Flutter Chinese developer website - Flutter
On the iOS platform, assets resource files are read through mainBundle . Get the file path through pathForResource:ofType: 's
lookupKeyForAsset
orlookupKeyForAsset:fromPackage:
method. Similarly, FlutterViewController 'slookupKeyForAsset:
orlookupKeyForAsset:fromPackage:
method can also get the file path. It can be used when developing plug-ins , and it is the best choiceFlutterPluginRegistrar
when developing applications using the platform view .FlutterViewController
Because we mainly develop applications, we useFlutterViewController测试。
access icons/heart.png的事例:
OC code:
NSString* key = [registrar lookupKeyForAsset:@"icons/heart.png"];
NSString* path = [[NSBundle mainBundle] pathForResource:key ofType:nil];
Swift Code:
let key = controller.lookupKey(forAsset: "icons/heart.png")
let mainBundle = Bundle.main
let path = mainBundle.path(forResource: key, ofType: nil)
Access the assets/db.sqlite file on the flutter side on IOS
//AppDelegate文件中
let controller = window?.rootViewController as! FlutterViewController
let key: String = controller.lookupKey(forAsset: "assets/db.sqlite");
let mainBundle = Bundle.main
let path = mainBundle.path(forResource: key, ofType: nil)
//"/Users/metajoy-release-1/Library/Developer/CoreSimulator/Devices/004A8A04-436C-43DB-AA34-2C460339FE10/data/Containers/Bundle/Application/07029261-FF77-4D68-A52E-B5D8D23837BA/Runner.app/Frameworks/App.framework/flutter_assets/assets/db.sqlite"