Reputation: 70
I am trying to write an iOS plugin for Unity, which requires the use of a bridge with a C layer.
The plugin itself is written in Swift.
Currently my implementation works like so:
// Swift
@objc public class SomeClass {
@objc public func SomeFunction() -> String {
return "Hello"
}
}
// C
extern "C" {
void _someFunction() {
// I can access the Swift file using "[SomeClass shared]"
// This returns the "Hello" string correctly
NSString *helloMessage = [[SomeClass shared] SomeFunction];
}
}
This works, but now I need to make an async call which requires a callback from the Swift implementation.
// Swift
@objc public class SomeClass {
@objc public func SomeFunction(completionHandler: (String) -> Void) {
// Gets called after some async operations
completionHandler(“hello”)
}
}
Is it possible to call this Swift function from the C layer and how would you do so?
// C
extern "C" {
void _someFunction() {
// ??????
}
}
Upvotes: 2
Views: 636
Reputation: 257709
If you set up bridging well then the call should be like
void _someFunction() {
[[SomeClass shared] SomeFunctionWithCompletionHandler:^(NSString * _Nonnull value) {
NSLog(@"Result: %@", value);
}];
}
Btw, classes to be visible in Objective-C should be 'is-a' NSObject
, like
@objcMembers public class SomeClass: NSObject {
public static var shared = SomeClass()
public func SomeFunction() -> String {
return "Hello"
}
public func SomeFunction(completionHandler: (String) -> Void) {
// Gets called after some async operations
completionHandler("hello")
}
}
Tested with Xcode 13.2 / iOS 15.2
Upvotes: 1