Skip to content

Objective-C code not working in NativeScript app #5022

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ReeceReynolds opened this issue Sep 17, 2019 · 8 comments
Closed

Objective-C code not working in NativeScript app #5022

ReeceReynolds opened this issue Sep 17, 2019 · 8 comments

Comments

@ReeceReynolds
Copy link

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/
@ReeceReynolds
Copy link
Author

Have downloaded and looked at the #4343 issue but no progress was made on my problem

@KristianDD
Copy link
Contributor

Hi @ReeceReynolds ,
According to this Stack Overflow thread you should include the WatchConnectivity.framework to your application.

To do so, add to the app/App_Resources/iOS/build.xcconfig the following line:
OTHER_LDFLAGS = $(inherited) -framework "WatchConnectivity"

@ReeceReynolds
Copy link
Author

@KristianDD Thank you for your quick reply! I have added the line you suggested to the build.xcconfig although now the project is no longer building the WatchOS app. In console it usually prints the Apple Watch integration is in beta but I am no longer getting the confirmation.

@KristianDD KristianDD reopened this Sep 17, 2019
@support support bot removed the question label Sep 17, 2019
@KristianDD
Copy link
Contributor

@ReeceReynolds this is strange. Will check if we can reproduce this behaviour.

@KristianDD
Copy link
Contributor

@ReeceReynolds I managed to get the first compilation error:

Undefined symbols for architecture x86_64:
"OBJC_CLASS$_WCSession", referenced from:

After I added the OTHER_LDFLAGS = $(inherited) -framework "WatchConnectivity" line, the project build competed successfully and the application was installed on the Device Simulator and on the Watch simulator. Also the app on the Watch worked as expected.

Please try removing platforms folder and starting a clean build.

Also you can check if removing OTHER_LDFLAGS = $(inherited) -framework "WatchConnectivity" will solve the missing Watch app beta message.

I am unable to test on a real device at the moment and I am using Simulators. Do you deploy on a real device or a Simulator?

@ReeceReynolds
Copy link
Author

From a fresh build I get the Watch app beta message. The iOS app installs on the device but the Watch app doesn't. I am deploying on both real devices and Simulators.

After temporarily removing OTHER_LDFLAGS = $(inherited) -framework "WatchConnectivity" I get the Watch beta message every time.

@KristianDD
Copy link
Contributor

@ReeceReynolds as we are unable to reproduce it with our project and all works as expected, could you please try to isolate the problem and attach to the issue a sample project that doesn't install on your watch. That will help us investigate the issue further.

@rosen-vladimirov
Copy link
Contributor

Closing due to inactivity.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants