Search code examples
iosobjective-cswiftwatchkitmmwormhole

How do I exactly use MMWormhole with Swift?


I have an iPhone application and added a WatchKitExtension. From the iPhone App I want to pass a String to the WatchApp which is supposed to change an image on the Watch.

  • What I already did was to download the source files and import the MMWormhole.m & .h. They are written in Obj-C and so Xcode automatically bridged them for me.
  • I also added an app group and activated it for my WatchExtension & my iPhone target

In the tutorial on GitHub it says I have to initialize the wormhole with:

self.wormhole = [[MMWormhole alloc] initWithApplicationGroupIdentifier:@"group.com.mutualmobile.wormhole"
                                                                          optionalDirectory:@"wormhole"];

...and send a message using:

[self.wormhole passMessageObject:@{@"titleString" : title} 
                         identifier:@"messageIdentifier"];

But I have actually no idea where to put that, I am using Swift in my iPhone application and the WatchExtension.

Can anyone please help me there?


Solution

  • I suppose it depends on different applications, but for one application I put the listeners in the didFinishLaunchingWithOptions method of my app delegate in the main iOS app. This was because the user would be using the watch, and they would be relaying information off to the phone

    There were multiple listeners...

    var wormhole = MMWormhole(applicationGroupIdentifier: "group", optionalDirectory: nil)
    
    wormhole.listenForMessageWithIdentifier("identifier", listener: { (message) -> Void in
                    //do stuff
    })
    
    wormhole.listenForMessageWithIdentifier("identifier2", listener: { (message) -> Void in
                //do stuff
    })
    
    wormhole.listenForMessageWithIdentifier("identifier3", listener: { (message) -> Void in
                //do stuff
    })
    

    And then in a WKInterfaceController, I sent a message. Sometimes in an action, sometimes in the willActivate method. It really depends on the flow of your app

    var wormhole = MMWormhole(applicationGroupIdentifier: "group", optionalDirectory: nil)
        @IBAction func buttonPushed(){            
            wormhole.passMessageObject("object", identifier: "identifier1")
        }
    

    This can work both ways though, I could have very easily put a listener in my watch which would wait for messages initiated by some Interface Controller on the phone.