添加 Watchkit 扩展并让 iPhone 完成工作
Adding Watchkit Extension and letting the iPhone do the work
我需要开发一个 iPhone 应用程序并为其实施 Watchkit 扩展。事情是这样的:确实有很多 classes,尤其是那些负责网络通信的(身份验证令牌等)。现在构建该应用程序的人正在做一些事情,比如从网络通信中显示 UIAlert
s,如下所示:
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 扩展中使用这个网络通信 class,因为在 Apple 的 Watchkit 中没有 UIViewController
- 这意味着当我添加这个 class 到我的监视目标。
这只是一个例子,还有很多其他类似的东西,我真的不想重构整个源代码。
现在我的问题是:我能否以某种方式让 iPhone 完成所有工作(例如,从服务器获取一些数据,同时处理所有身份验证令牌内容),然后将结果推送到手表套件扩展?我已经阅读了一些关于 WCSession
的内容 - 我可以将其用于我的案例吗?我主要是想在 WKInterfaceMap
上显示一些兴趣点。
关于您的第一个问题,您会在 watchkit 中找到关于 Web 服务的解决方案 。在 link 中,社区建议让 iPhone 应用程序完成繁重的工作(获取数据等),而只 return 将信息发送给 watchApp。
其次,您可以使用 watchconnectivity 框架,也可以使用 MMWormhole 与您的 iPhone 应用进行通信。您需要做的就是获取 Place of Interests 数据并将其发送到您的 watchApp。从那里您可以填充您的地图。
我需要开发一个 iPhone 应用程序并为其实施 Watchkit 扩展。事情是这样的:确实有很多 classes,尤其是那些负责网络通信的(身份验证令牌等)。现在构建该应用程序的人正在做一些事情,比如从网络通信中显示 UIAlert
s,如下所示:
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 扩展中使用这个网络通信 class,因为在 Apple 的 Watchkit 中没有 UIViewController
- 这意味着当我添加这个 class 到我的监视目标。
这只是一个例子,还有很多其他类似的东西,我真的不想重构整个源代码。
现在我的问题是:我能否以某种方式让 iPhone 完成所有工作(例如,从服务器获取一些数据,同时处理所有身份验证令牌内容),然后将结果推送到手表套件扩展?我已经阅读了一些关于 WCSession
的内容 - 我可以将其用于我的案例吗?我主要是想在 WKInterfaceMap
上显示一些兴趣点。
关于您的第一个问题,您会在 watchkit 中找到关于 Web 服务的解决方案
其次,您可以使用 watchconnectivity 框架,也可以使用 MMWormhole 与您的 iPhone 应用进行通信。您需要做的就是获取 Place of Interests 数据并将其发送到您的 watchApp。从那里您可以填充您的地图。