Calling a Method on Watchkit

2019-01-28 13:50发布

问题:

I attended to a watchkit hackathon yesterday and I had some problems regarding calling a method on an NSObject class which uses the Google Maps API and send local notifications. If I call this method from my Watchkit extension, the code doesn't compile, but If I call from the ViewController, for example, everything works perfectly

#import "InterfaceController.h"
#import "Methods.h"

@interface InterfaceController()

 @end


 @implementation InterfaceController

- (instancetype)initWithContext:(id)context {
self = [super initWithContext:context];
if (self){
    // Initialize variables here.
    // Configure interface objects here.
    NSLog(@"%@ initWithContext", self);

}
return self;
}
- (IBAction)butRoute
{
     Methods *mt = [[Methods alloc]init];
    [mt notif:@"ARRIVING!"];
    //***** If I call this method, my code won't compile!!! *****

}

- (void)willActivate {
// This method is called when watch view controller is about to be visible to user
NSLog(@"%@ will activate", self);
}

- (void)didDeactivate {
// This method is called when watch view controller is no longer visible
NSLog(@"%@ did deactivate", self);
}

@end

The error I get is:

回答1:

Check the target for your Methods class and make sure it is in your watch kit extensions target.

Alternately, look at building a framework for your shared classes. https://developer.apple.com/videos/wwdc/2014/?id=416



回答2:

I don't know what xcode version you are using but take into account that the initWithContext method is no longer valid. You should be using:

- (void)awakeWithContext:(id)context

And you shouldn't be overwriting it, just use it.



回答3:

just remove #import line and replace it with WatchKit framework.