Skip to content

Commit

Permalink
#747 nsbundle search with cinterop
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex009 committed Dec 7, 2024
1 parent 49f7518 commit 55f4849
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 17 deletions.
9 changes: 5 additions & 4 deletions resources/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import org.jetbrains.kotlin.gradle.plugin.KotlinCompilation
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeCompilation
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget

/*
* Copyright 2019 IceRock MAG Inc. Use of this source code is governed by the Apache 2.0 license.
*/

import org.jetbrains.kotlin.gradle.plugin.KotlinCompilation
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget

plugins {
id("multiplatform-library-extended-convention")
id("multiplatform-android-publish-convention")
Expand Down Expand Up @@ -38,6 +37,8 @@ kotlin {
compilations.getByName(KotlinCompilation.MAIN_COMPILATION_NAME) {
val appleNative by cinterops.creating {
defFile(project.file("src/appleMain/def/appleNative.def"))

includeDirs("$projectDir/src/appleMain/objective-c")
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions resources/src/appleMain/def/appleNative.def
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# https://kotlinlang.org/docs/native-definition-file.html
language = Objective-C
package = dev.icerock.moko.resources.apple.native
#staticLibraries = libMRResourcesBundle.a
headers = MRResourcesBundle.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,25 @@

package dev.icerock.moko.resources.utils

import kotlinx.cinterop.BetaInteropApi
import kotlinx.cinterop.ObjCObjectBaseMeta
import dev.icerock.moko.resources.apple.native.ResourcesBundleAnchor
import kotlinx.cinterop.ExperimentalForeignApi
import platform.Foundation.NSBundle
import platform.Foundation.NSDirectoryEnumerator
import platform.Foundation.NSFileManager
import platform.Foundation.NSLog
import platform.Foundation.NSURL
import platform.Foundation.pathExtension
import kotlin.experimental.ExperimentalObjCName

@OptIn(ExperimentalObjCName::class, BetaInteropApi::class)
@ObjCName("ObjCAnchorClass")
internal object ObjCAnchorClass : ObjCObjectBaseMeta()

fun NSBundle.Companion.loadableBundle(identifier: String): NSBundle {
// we should use search by our class because dynamic framework with resources can be placed in
// external directory, not inside app directory (NSBundle.main). for example in case of
// SwiftUI preview - app directory empty, but dynamic framework with resources will be in
// different directory (DerivedData)
@OptIn(BetaInteropApi::class)
val bundlePath: String = NSBundle.bundleForClass(ObjCAnchorClass).bundlePath
@OptIn(ExperimentalForeignApi::class)
val rootBundle: NSBundle = requireNotNull(ResourcesBundleAnchor.getResourcesBundle()) {
"root NSBundle can't be found"
}
val bundlePath: String = rootBundle.bundlePath

val enumerator: NSDirectoryEnumerator = requireNotNull(
NSFileManager.defaultManager.enumeratorAtPath(bundlePath)
Expand Down
5 changes: 3 additions & 2 deletions resources/src/appleMain/objective-c/MRResourcesBundle.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#import <Foundation/NSBundle.h>

@interface ResourcesBundleAnchor : NSObject
@end

NSBundle* getResourcesBundle();
+ (NSBundle*) getResourcesBundle;

@end
5 changes: 3 additions & 2 deletions resources/src/appleMain/objective-c/MRResourcesBundle.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
#import "MRResourcesBundle.h"

@implementation ResourcesBundleAnchor
@end

NSBundle* getResourcesBundle() {
+ (NSBundle*) getResourcesBundle {
return [NSBundle bundleForClass:[ResourcesBundleAnchor class]];
}

@end

0 comments on commit 55f4849

Please sign in to comment.