【问题标题】:Adding Watchkit Extension and letting the iPhone do the work添加 Watchkit 扩展并让 iPhone 完成工作
【发布时间】:2018-01-31 11:13:58
【问题描述】:

我需要开发一个 iPhone 应用程序并对其实施 Watchkit 扩展。事情是这样的:确实有很多类,尤其是那些负责网络通信的类(身份验证令牌等)。现在,构建应用程序的人正在做一些事情,比如显示来自网络通信的UIAlerts,如下所示:

class func showReloadDataDialog(_ action: @escaping (_ action: UIAlertAction) -> Void) {
    let alert = UIAlertController.init(title: String.localizedString("AlertTitleError", ""),
                                       message: String.localizedString("ActivationRetrySupportedNetworksLoadMessage", ""),
                                       preferredStyle: .alert)
    let defaultAction = UIAlertAction.init(title: String.localizedString("AlertNoButton", ""), style: .default, handler: nil)
    alert.addAction(defaultAction)
    let reloadAction = UIAlertAction.init(title: String.localizedString("AlertYesButton", ""), style: .default, handler: action)
    alert.addAction(reloadAction)
    self.present(alert)
}

这是present() 函数:

// MARK: Present
class func present(_ alert: UIAlertController) {
    if let window = UIApplication.shared.delegate?.window, var topController = window!.rootViewController {
        while topController.presentedViewController != nil {
            topController = topController.presentedViewController!
        }
        if let navigationController = topController as? UINavigationController {
            topController = navigationController.visibleViewController!
        }
        if topController is UIAlertController {
            return
        }
        topController.present(alert, animated: true, completion: nil)
    }
}

如您所见,他们所做的事情与他们应该做的事情有些不同。现在这确实是个问题,因为我不能在我的 Watchkit 扩展中使用这个 Network 通信类,因为 Apple 的 Watchkit 中没有 UIViewController - 这意味着当我将这个类添加到我的 watch 目标时它不会编译。

这只是一个例子,还有很多其他类似的东西,我真的不想重构整个源代码。

现在我的问题是:我能否以某种方式让 iPhone 完成所有工作(例如,在处理所有身份验证令牌内容的同时从服务器获取一些数据)然后将结果推送到 Watchkit 扩展?我已经阅读了一些关于 WCSession 的内容 - 我可以将其用于我的情况吗?我基本上是想在WKInterfaceMap 上显示一些兴趣点。

【问题讨论】:

    标签: ios iphone swift watchkit watchconnectivity


    【解决方案1】:

    至于您的第一个问题,您将在 watchkit 中找到有关 Web 服务的解决方案 here。在链接中,社区建议让 iPhone 应用完成繁重的工作(获取数据等)并将信息返回给 watchApp。

    其次,您可以使用 watchconnectivity 框架,也可以使用 MMWormhole 与您的 iPhone 应用程序进行通信。您需要做的就是获取 Place of Interests 数据并将其发送到您的 watchApp。从那里您可以填充您的地图。

    【讨论】:

      猜你喜欢
      • 2010-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-18
      • 2013-11-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多