To access the main bundle from an app extension in Swift, you need to get the URL for the bundle first. Then you can access the resources in the bundle using the url.
To get the URL for the main bundle, use the following code in your app extension:
main.swift265 chars7 lines
This code gets the URL for the "Main.bundle" and creates a Bundle
object. You can then access resources in the bundle using the url(forResource:withExtension:)
method.
Note: You need to specify the name of the resource bundle in the url(forResource:withExtension:)
method. If your bundle has a different name, you need to modify the parameter accordingly.
Alternatively, you can access the main bundle directly without creating a Bundle
object. However, this may not work in all cases.
main.swift98 chars3 lines
This code accesses the main bundle directly and retrieves the URL for the "my_image.png" resource. However, this may not work correctly in some cases, especially if the app extension is embedded in the main app.
gistlibby LogSnag