Skip to content

Commit db0c399

Browse files
committed
#97 fix bundle search in static frameworks (new bundle search logic)
1 parent 2f85d3b commit db0c399

1 file changed

Lines changed: 25 additions & 10 deletions

File tree

  • resources/src/iosMain/kotlin/dev/icerock/moko/resources/utils

resources/src/iosMain/kotlin/dev/icerock/moko/resources/utils/NSBundleExt.kt

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,35 @@
55
package dev.icerock.moko.resources.utils
66

77
import platform.Foundation.NSBundle
8+
import platform.Foundation.NSDirectoryEnumerator
9+
import platform.Foundation.NSFileManager
10+
import platform.Foundation.NSURL
11+
import platform.Foundation.pathExtension
812

913
fun NSBundle.Companion.loadableBundle(identifier: String): NSBundle {
1014
// try get already loaded bundle
1115
NSBundle.bundleWithIdentifier(identifier)?.let { return it }
1216

13-
// try load from app framework
14-
NSBundle.mainBundle
15-
.pathsForResourcesOfType(ext = "framework", inDirectory = "Frameworks")
16-
.filterIsInstance<String>()
17-
.mapNotNull { NSBundle.bundleWithPath(it) }
18-
.flatMap { it.pathsForResourcesOfType(ext = "bundle", inDirectory = null) }
19-
.filterIsInstance<String>()
20-
// load each loadable bundle to correct load by identifier later
21-
.forEach { NSBundle.bundleWithPath(it)?.bundleIdentifier }
17+
val bundlePath: String = NSBundle.mainBundle.bundlePath
18+
val enumerator: NSDirectoryEnumerator = requireNotNull(NSFileManager.defaultManager.enumeratorAtPath(bundlePath))
19+
while (true) {
20+
val relativePath: String = enumerator.nextObject() as? String ?: break
21+
val url = NSURL(fileURLWithPath = relativePath)
22+
if (url.pathExtension == "bundle") {
23+
val fullPath = "$bundlePath/$relativePath"
24+
val loadedIdentifier: String? = NSBundle.bundleWithPath(fullPath)?.bundleIdentifier
25+
if(isBundleSearchLogEnabled) {
26+
println("moko-resources auto-load bundle with identifier $loadedIdentifier at path $fullPath")
27+
}
28+
}
29+
}
2230

23-
return NSBundle.bundleWithIdentifier(identifier)!!
31+
val resultBundle = NSBundle.bundleWithIdentifier(identifier)
32+
if (resultBundle == null) {
33+
throw IllegalArgumentException("bundle with identifier $identifier not found")
34+
}
35+
36+
return resultBundle
2437
}
38+
39+
var isBundleSearchLogEnabled = false

0 commit comments

Comments
 (0)