How to use MMWormhole with Swift exactly?

I have an iPhone app and added WatchKitExtension. From the iPhone application I want to transfer Stringto WatchApp, which should change the image to Watch.

  • What I have already done is load the source files and import MMWormhole.mand .h. They are written in Obj-C, and therefore Xcode automatically disabled them for me.
  • I also added an application group and activated it for my WatchExtension and my goal for the iPhone.

GitHub tutorial 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 no idea where to put it, I use Swift in the iPhone app and WatchExtension.

- ?

+4
2

, , didFinishLaunchingWithOptions iOS. , ,

...

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
})

WKInterfaceController . , willActivate. .

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

, , , .

+5

. , , . ( , !)

  • MMWormhole (.h .m), . , Cocoapods, , git. ( git)
  • , .h Swift, .
  • , .
  • iPhone → → . , Portal Developer , .

MMWormhole, iPhone

-, . . !

let wormhole = MMWormhole(applicationGroupIdentifier: "group.testMe.now", optionalDirectory: nil)
wormhole.listenForMessageWithIdentifier("wormholeMessageFromWatch", listener: { (message ) -> Void in
    if let messageFromWatch = message as? String {
          // do something with messageFromWatch
    }
})

iPhone

wormhole.passMessageObject("message from phone to watch", identifier: "wormholeMessageFromPhone")

iPhone MMWormhole (, )

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    universe.initWormhole(.phone, messageHandler: { (message) -> () in
        universe.wormhole.passMessageObject("the phone got \(message)", identifier: "wormholeMessageFromPhone")
    })
    return true
}

MMWormhole, Apple Watch

-, . . !

let wormhole = MMWormhole(applicationGroupIdentifier: "group.testMe.now", optionalDirectory: nil)
wormhole.listenForMessageWithIdentifier("wormholeMessageFromPhone", listener: { (message ) -> Void in
    if let messageFromPhone = message as? String {
          // do something with messageFromPhone
    }
})

MMWormhole, ,

override func awakeWithContext(context: AnyObject?) {
    super.awakeWithContext(context)
    universe.initWormhole(.watch, messageHandler: { (message) -> () in
        println("MMWormhole Message Came to Watch: \(message)")
    })
}

MMWormhole,

// force open the parent application because otherwise the message goes nowhere until the app is opened
WKInterfaceController.openParentApplication(["":""], reply: nil) 
universe.wormhole.passMessageObject("[from watch to phone]", identifier: "wormholeMessageFromWatch")
+3

Source: https://habr.com/ru/post/1584524/


All Articles