WilliamVH
WilliamVH

Reputation: 11

Marshalling setup when using obj-c class in nativescript (with typescript) project

Following the instructions on https://nativescript.org/blog/adding-objective-c-code-to-a-nativescript-app/ I setup a very simple nativescript project (with typescript underneath):

  1. tns create simpleApp --tsc
  2. tns platform add ios
  3. tns prepare ios
  4. run the generated ios project via xcode.

The app runs correctly.

Next I added a custom obj-c class to the project (App_Resources/iOS/src/Stuff .h and .m):

#import <Foundation/Foundation.h>

@interface Stuff: NSObject
+(void)doStuff;
@end
#import "Stuff.h"

@implementation Stuff
+(void)doStuff {
    NSLog(@"I should see this...");
}
@end

And a module.modulemap:

module Stuff {
    header "Stuff.h"
    export *
}

A tns prapare ios command successfully processes the class.

When I try to add the 'Stuff' object to the typescript source, I get a cannot find name Stuff. How do I get the marshalling/metadata between typescript and obj-c working? Do I need to run some scripts? add imports? The tutorial does not mention anything about that...

This is what I added to the nativescript default template app:

onTap() {
        this._counter--;
        this.updateMessage();
        Stuff.doStuff();
    }

And hoped to see the log text when tapping the button...

Upvotes: 0

Views: 264

Answers (1)

WilliamVH
WilliamVH

Reputation: 11

I found a way to add the required .d.ts files to the project: generating-typescript-typings

Run the following in the src-root of your nativescript project:

TNS_TYPESCRIPT_DECLARATIONS_PATH="$(pwd)/typings" tns build ios

It generates a typings folder containing all the .d.ts files for the ios classes, including the Stuff class that was created manually. The Typescript code is correctly being interpreted now... New obj-c classes can be addressed in the TS code once they have been processed by the line above.

Upvotes: 0

Related Questions