我该如何在Swift中精确使用MMWormhole?

4
我有一个iPhone应用程序,并添加了WatchKitExtension。从iPhone应用程序中,我想将一个String传递到WatchApp,该应用程序应该更改手表上的图像。
  • 我已经下载了源文件并导入了MMWormhole.m.h。它们是用Obj-C编写的,因此Xcode为我自动桥接了它们。
  • 我还添加了一个应用组,并为我的WatchExtension和iPhone目标激活了它

GitHub的教程中,它说我必须使用以下方式初始化wormhole:

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

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

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

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

有人可以帮帮我吗?

2个回答

5
以下是我的说明。希望它们能够帮助你完成最简单的用例,然后你可以从那里扩展。(记得构造代码以使其实际有意义!)
  • 将MMWormhole(.h和.m文件)添加到你的项目中。如果你知道如何使用Cocoapods,请使用它,否则请使用git子模块。(我使用git子模块)
  • 因为你需要让.h文件在Swift中可见,所以你需要使用桥接头文件。
  • 设置一个应用程序组,这需要使用开发者门户网站。链接在这里
  • 在您的iPhone构建目标->功能->应用程序组中添加您的组。如果所有三个复选框都不完美,请返回开发者门户网站并确保一切正确或重新开始。

MMWormhole,iPhone端

在你可以访问的某个地方设置wormhole。 注意:你的组ID必须是上面提到的那个!

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,苹果手表端

将虫洞设置在您可以访问到的地方。注意:您的组ID必须是上面提到的那个!

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

5

我想这取决于不同的应用程序,但对于一种应用程序,我将监听器放置在我的主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")
    }

不过,这也可以双向工作,我可以很容易地在我的手表中放置一个监听器,等待手机上的某个界面控制器发起的消息。


它说它不认识 MMWormhole...我尝试将其导入到我的 AppDelegate.swift 中,但当我键入 import MMWormhole 或者加上 .m.h 时,它会显示没有这个模块。 - LinusGeffarth
如果您前往根项目 -> 构建设置并搜索 "Objective-C Bridging Header",路径是否指向正确的文件?该文件应命名为类似于 Projectname-Bridging-Header.h 的东西。在此文件中,您是否有以下行 #import "MMWormhole.h" - prawn
当我搜索时,它没有显示任何内容,对于任何目标,所以我只插入了文件名。这样做对吗?还是我需要文件路径?哦,我添加了#import "MMWormhole.h",但它仍然不起作用 :/ - LinusGeffarth
哦,没有Objective-C桥接头规范吗?试着看看这个。当我无法在Swift中调用Objective-C代码时,这个链接帮了我很多。 https://dev59.com/f2Ag5IYBdhLWcg3wE3nQ - prawn
不知道发生了什么,也不知道到底是什么解决了它,但我添加、删除、导入、移除、重新添加等操作后,它现在可以工作了。你的解决方案很有帮助,谢谢你的代码翻译。 - LinusGeffarth
我有类似的问题。当我使用桥接时,我在 iPhone 应用程序中使用 MMWormhole 没有问题,但在 WatchKit Extension 中却收到了投诉。请参见此处:https://dev59.com/covda4cB1Zd3GeqPgPSD - murvinlai

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接