【问题标题】:How do I exactly use MMWormhole with Swift?如何在 Swift 中准确使用 MMWormhole?
【发布时间】:2015-04-24 15:45:02
【问题描述】:

我有一个 iPhone 应用程序并添加了一个 WatchKitExtension。从 iPhone 应用程序中,我想将 String 传递给应该更改手表上的图像的 WatchApp。

  • 我已经做的是下载源文件并导入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 应用程序中使用 Swift WatchExtension。

有人可以帮我吗?

【问题讨论】:

    标签: ios objective-c swift watchkit mmwormhole


    【解决方案1】:

    我想这取决于不同的应用程序,但对于一个应用程序,我将侦听器放在主 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 它说那里没有这样的模块。
    • 如果你去你的根项目 -> 构建设置并搜索“Objective-C Briding Header”,路径是否指向正确的文件?该文件应命名为 Projectname-Bringing-Header.h 之类的名称。在这个文件中你有#import "MMWormhole.h" 行吗?
    • 当我搜索它没有显示任何目标,所以我只是插入了文件名。这是正确的还是我需要文件的路径?哦,我添加了#import "MMWormhole.h",但它仍然不起作用:/
    • 嗯。没有客观的 c 桥接头规范吗?试试看这个。当我无法快速调用 obj c 代码时,此链接对我有所帮助。 stackoverflow.com/questions/24002369/…
    • 不知道发生了什么以及实际解决了它,但我添加、删除、导入、删除、读取等,不知何故它现在可以工作了。您的解决方案很有帮助,感谢您的代码翻译。
    【解决方案2】:

    这是我的指示。希望他们可以帮助您处理最简单的用例,然后您可以从那里扩展。 (请记住构建您的代码,使其真正有意义!)

    • 将 MMWormhole(.h 和 .m)添加到您的项目中。如果您知道如何使用 Cocoapods,请这样做,否则,只需使用 git 子模块。 (我使用 git submmodules)
    • 因为您需要 .h 从 Swift 中可见,所以您需要使用桥接头。
    • 设置应用组,需要使用开发者门户。 Link is here
    • 在您的 iPhone 构建目标 -> 功能 -> 应用程序组中并添加您的组。如果所有三个复选框都不完美,请返回开发者门户并确保一切正常或重新开始。

    MMWormhole,iPhone 端

    在你能到达的地方设置虫洞。 注意:您的组 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,Apple Watch 侧

    在你能到达的地方设置虫洞。 注意:您的组 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")
    

    【讨论】:

      猜你喜欢
      • 2022-10-16
      • 1970-01-01
      • 2013-01-13
      • 1970-01-01
      • 1970-01-01
      • 2020-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多