Skip to content

Objective-C code not working in NativeScript app #5022

Closed
@ReeceReynolds

Description

@ReeceReynolds

Environment

  • CLI: 6.1.0

Describe the bug
I am trying to add Objective-C code to a NativeScript project I am working on. I have followed the documentation Adding Objective-C Code to a NativeScript App but have had no luck. When running tns run ios I get the following error:

Undefined symbols for architecture x86_64:
"OBJC_CLASS$_WCSession", referenced from:
objc-class-ref in SendToWatch.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
note: Using new build systemnote: Planning buildnote: Constructing build description

I have also tried to create a plugin for this but run into a Podspec issue.

SendToWatch.h

#import <WatchKit/WatchKit.h>
#import <Foundation/Foundation.h>
#import <WatchConnectivity/WatchConnectivity.h>

@interface SendToWatch : NSObject

-(void)sendDouble: (double)value;
-(void)packageData: (NSDictionary *)request;
-(void)session: (WCSession *)session activationDidCompleteWithState:(WCSessionActivationState)activationState error:(NSError *)error;
-(void)sessionDidBecomeInactive: (WCSession *)session;
-(void)sessionDidDeactivate: (WCSession *)session;

@end

SendToWatch.m

#import "SendToWatch.h"
#import <WatchConnectivity/WatchConnectivity.h>

@interface SendToWatch () <WCSessionDelegate>

@end

@implementation SendToWatch

-(void)sendDouble: (double)value {
    NSDictionary *message = @{@"request":[NSString stringWithFormat:@"%0.2f", value]};
    [self packageData:message];
}

-(void)packageData: (NSDictionary*)request {
    if(WCSession.isSupported) {
        WCSession *session = [WCSession defaultSession];
        session.delegate = self;
        [session activateSession];
        
        if(session.reachable) {
            NSError *error = nil;
            [session updateApplicationContext:request error:&error]; // Send message to Apple Watch
            if(error) {
                NSLog(@"%@",error.localizedDescription);
            }
        } else {
            NSLog(@"Session is not reachabe!");
        }
    } else {
        NSLog(@"Session is not supported!");
    }
}

// Needed to conform to WCSessionDelegate protocol
-(void)session: (WCSession *)session activationDidCompleteWithState:(WCSessionActivationState)activationState error:(NSError *)error {
}

// Needed to conform to WCSessionDelegate protocol
-(void)sessionDidBecomeInactive: (WCSession *)session {
}

// Needed to conform to WCSessionDelegate protocol
-(void)sessionDidDeactivate: (WCSession *)session {
}

@end

module.modulemap

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

File Structure
The SendToWatch.m SendToWatch.h and module.modulemap live inside the src folder:

  • Project/app/App_Resources/iOS/src/

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions