When it comes to iOS, Titanium always relied on Objective-C based native modules. Over time, Swift was introduced and became more and more stable over the last years. That's why in Titanium SDK 8+, we moved our iOS core-system to an own framework, which allows developers like you to write full-featured and pure Swift frameworks. It does not only allow you to use external Swift libraries (like Alamofire or IGListKit), but also to write the modules themselves in Swift as well.
To get started with Swift modules in Titanium, simply create a new module project from the CLI:
It will prompt you to select a name, a module-identifier, project location and finally, the code-base. In Titanium 8+, you can select between Objective-C and Swift based modules. We are not planning to deprecate Objective-C anytime soon and leave it up the developer to select the best fit. Both module code-bases can be used together in one project without issues. And that's it! The generated module contains a <ModuleID>Module.swift and a <ModuleID>ExampleProxy.swift that are the 1:1 pardons of their Objective-C siblings. All required Titanium libraries are already included and you are ready to go.
Writing Swift API's for Titanium
When it comes to method naming conventions, the concepts that you may be used to are the same as before. If you are completely new to native Titanium modules, we recommend you to read the iOS Module Quick Start guide before. This guide references some examples from the titanium-swift-module-example repository that you can clone and try yourself.
As you noticed, the
args parameter is an Optional and should be guarded before it's usage, since it can be
nil when calling it with arguments, e.g.
Test.post(). The same goes for the first and second parameter of the method, which includes the URL of the method in the first parameter and the callback (an instance of the
KrollCallback class) as the second parameter. Once ready, you can pass the arguments to your native API's and return to your proxy (if necessary).
Well, you learned how to create native Swift-based modules. Once your module is ready, you can package it (
appc run -p ios --build-only) and deploy it to GitHub or Gitt.io. If you have further questions, make sure to visit our TiSlack community or reach out to us via JIRA. Code strong!