Advanced Concepts
Adding ObjectiveC/Swift Code to an application
You can add Objective-C/Swift source files to App_Resources/iOS/src
. For Objective-C files, create a .modulemap
file. To add a CocoaPod, edit App_Resources/iOS/Podfile
:
App_Resources/
ββiOS/
βββsrc/
ββββShimmer.swift
ββββShimmer.h
ββββShimmer.m
ββββmodule.modulemap
βββPodfile
ββ...more
Adding Swift code β
Define the swfit file in App_Resources/iOS/src
.
// HelloSwift.swift
importUIKit
classHelloSwift: NSObject {
@objcpublicvar stringToReturn: String="Hello from Swift!"
@objcpublicfuncgetString() ->String {
return stringToReturn;
}
}
Given the example above, your JavaScript or TypeScript code can reference the Swift code by using the full class name:
consthelloSwift=newHelloSwift()
helloSwift.stringToReturn ='Custom hello from Swift!'
console.log(helloSwift.getString())
// prints: Custom hello from Swift!
Using @objc
β
@objc
allows exposing variables and functions to use them from JS/TS, note that in the example the variables and methods have this notation.
Using @objcMembers
β
A shortcut to the @objc
notation is to use the @objcMembers
notation at the class level to make the entire class accessible.
// HelloSwift.swift
importUIKit
@objcMembers
classHelloSwift: NSObject {
publicvar stringToReturn: String="Hello from Swift!"
publicfuncgetString() ->String {
return stringToReturn;
}
}
- Previous
- Adding Java/Kotlin Code