heilerich
heilerich

Reputation: 1042

Adding native swift code to NativeScript application

I'm trying to add native swift code to my NativeScript app. According to these instructions in the documentation I can just add a swift source file to App_Resources/iOS/src/ and then use any publicly exposed classes directly in my TypeScript code. Unfortunately this just doesn't work. I'll just get Cannot find name 'TestClass' and that's it.

Steps to reproduce:

  1. Get a fresh NS project with ios tns create my-app-name --template tns-template-blank-ts

Update: I actually created the App with vue init nativescript-vue/vue-cli-template testapp. That seems to have caused the problems.

  1. Add a TestClass.swift to App_Resources/iOS/src/
import Foundation

public class TestClass: NSObject {
    @objc public func echo(param: String) -> String {
        return param
    }
}
  1. Instantiate it in any TypeScript source file let instance = new TestClass()
  2. Do tns debug ios
  3. Compilation will fail with Cannot find name 'TestClass'

I have also tried generating TypeScript typings with TNS_TYPESCRIPT_DECLARATIONS_PATH="$(pwd)/typings" tns build ios or or just delcaring it as any with declare let KeyCommander: any; to eliminate the possibility that this is a TS related problem. The first approach doesn't generate any typings for my custom class so the TypeScript code will still not compile. The second approach let's the TS code compile but crashes on execution with JS ERROR ReferenceError: Can't find variable: TestClass.

I have also verified that the swift file is indeed getting compiled by inserting a syntax error which will crash the build process.

My NativeScript version is 6.4.0.

What am I missing?

Update: I just realized I actually created the App with vue init nativescript-vue/vue-cli-template testapp. I verified that as mentioned Tyler Blake's answer in an app created with the tns cli the described process actually works. In an app I just freshly created with vue init it doesn't, the objc!nsswiftsupport.d.ts is not being generated.

The question now is: What's causing the difference?

Upvotes: 0

Views: 1006

Answers (2)

heilerich
heilerich

Reputation: 1042

I was able to solve the problem by inspecting the differences between the templates created with tns-cli and vue init. The difference is that the vue init template ships with an outdated version of the nativescript platform. You can just simply change

"tns-ios": {
  "version": "6.0.1"
}

to version 6.4.0 (which the version the tns-cli template comes with) and then the process will work as described in the documentation.

Upvotes: 0

Tyler Blake
Tyler Blake

Reputation: 136

I followed your steps and I was able to get the typings to generate in objc!nsswiftsupport.d.ts. After you generate typings do you have that file with these contents?

declare class TestClass extends NSObject {

    static alloc(): TestClass; // inherited from NSObject

    static new(): TestClass; // inherited from NSObject

    echoWithParam(param: string): string;
}

This shows that NS is able to pick up the Swift code.

All you need to do now is add tns-platform-declarations then in the references.d.ts file, add a line that points to the objc!nsswiftsupport.d.ts file. Then you'll get intellisense in your TS code.

Something like this:

/// <reference path="./typings/objc!nsswiftsupport.d.ts" />

Hope this helps!

Upvotes: 1

Related Questions