user6555550
user6555550

Reputation:

Call Methods in Extension Delegate

Is it possible to call methods in the extension delegate from a other IntefaceController in general?

Something like:

InterfaceController *interfaceController =[[InterfaceController alloc] init]; 
interfaceController callMethod

My Interface Controller

    #import "InterfaceController.h"
#import "OrdinaryEventRow.h"
#import <UIKit/UIKit.h> 
#import <WatchConnectivity/WatchConnectivity.h>

@interface InterfaceController()    

@implementation InterfaceController

- (void)awakeWithContext:(id)context {
    [super awakeWithContext:context];
    //Configure interface objects here.

-(void)doSomething {
    [self presentControllerWithName:@"goalView" context:nil];
}

@end

ExtensionDelegate:

#import "ExtensionDelegate.h"
#import "InterfaceController.h"
#import <WatchConnectivity/WatchConnectivity.h>
#import "setGoal.h"

@implementation ExtensionDelegate


//Handle Local Notification Actions
-(void)handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UNNotification *)localNotification{

    if([identifier isEqualToString:@"action"]){
        //Setup WCSession
        if ([WCSession isSupported]) {
            [[WCSession defaultSession] setDelegate:self];
            [[WCSession defaultSession] activateSession];

            //Get the value from slider
            NSString *someString = [[NSUserDefaults standardUserDefaults]
                                    stringForKey:@"Update"];
            NSString *Update = @"Update";
            NSDictionary *applicationData = [[NSDictionary alloc] initWithObjects:@[Update] forKeys:@[@"Update"]];
            //Send Message to the iPhone (handle over the goal value)
            [[WCSession defaultSession] sendMessage:applicationData
                                       replyHandler:^(NSDictionary *reply) {
                                           //handle reply from iPhone app here
                                       }
                                       errorHandler:^(NSError *error) {
                                           //catch any errors here
                                       }
             ];
        }
    }



//If Goal Setting was clicked
    if([identifier isEqualToString:@"action3"]){
        //here I want to call doSomething from InterfaceController

    }
}

So I just want to call a method defined in the InterfaceController from ExtensionDelegate.

Upvotes: 0

Views: 1406

Answers (1)

lostAtSeaJoshua
lostAtSeaJoshua

Reputation: 1755

There is not a way to init a WKInterfaceController from the ExtensionDelegate and call a method on it. If the controller you are trying to call a method is the root controller, you can get the rootController from the WKExtension in the ExtensionDelegate cast it and call a method on it.

// Objective-C
if ([WKExtension shared].rootController isKindOfClass:[InitialInterfaceController class]) {
    (InitialInterfaceController *)[WKExtension shared].rootController customMethod];
}

// Swift
    if let root = WKExtension.shared().rootInterfaceController as? InitialInterfaceController {
    root.customMethod()
}

* My objective-c is a little rusty so if there is a syntax error there please update or let me know in the comments and I can edit.

From your code example, you are trying to do an action based on a local notification so the best thing to do is handle the notification in the Interface Controller itself. Depending on what watchOS you are using

watchOS 3

handleAction(withIdentifier:for:) reference

watchOS 2

handleAction(withIdentifier:for:) reference

Important note here for these methods to be called your extension delegate does not implement the handleAction(withIdentifier:for:) method, WatchKit calls this method on your app’s root interface controller to respond to button taps in your notification interface.

Upvotes: 2

Related Questions