如何在Swift中准确地使用MMWormhole? [英] How do I exactly use MMWormhole with Swift?

查看:294
本文介绍了如何在Swift中准确地使用MMWormhole?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个iPhone应用程序,并添加了一个WatchKitExtension.我想从iPhone App传递String到WatchApp,该应用程序应该更改Watch上的图像.

  • 我已经做的是下载源文件并导入MMWormhole.m& .h.它们是用Obj-C编写的,因此Xcode为我自动桥接.
  • 我还添加了一个应用程序组,并为我的WatchExtension&我的iPhone目标

GitHub 上的教程中,它说我必须初始化虫洞与:

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

...并使用以下消息发送消息:

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

但是我实际上不知道该放在哪里,我在我的iPhone应用程序 WatchExtension中使用了Swift.

有人可以帮我吗?

我想它取决于不同的应用程序,但是对于一个应用程序,我将侦听器放在主iOS应用程序中我的应用程序委托的didFinishLaunchingWithOptions方法中.这是因为用户将使用手表,并且他们会将信息中继到手机上

有多个听众...

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

这可以双向工作,我可以很容易地在手表中放置一个侦听器,该侦听器将等待电话上某些接口控制器启动的消息.

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?

解决方案

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.

这篇关于如何在Swift中准确地使用MMWormhole?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆