如何在 Watch OS 2 中引用不支持的框架
How to reference non-supported frameworks in Watch OS 2
我将我的应用更新为最新的 swift 2.0 语法。这样一来,My watchkit 应用程序就坏了。问题是 watchkit 应用程序引用了一个引用框架 AVFoundation 的 class。 WatchOS2 显然现在不再支持一些标准框架:
Support for network-based operations includes the following technologies:
WatchKit extensions can access the network directly through an
NSURLSession object. WatchKit extensions have full access to the
NSURLSession capabilities, including the ability to download files in
the background. For information on how to use this class, see URL
Loading System Programming Guide. The Watch Connectivity framework
supports bidirectional communication between your Watch app and iOS
app. Use this framework to coordinate activities between the two apps.
See Communicating with Your Companion iOS App.
Available System Technologies for WatchKit
所以现在我无法编译手表套件代码,因为 "no such module found" 是尝试使用 AVFoundation 框架时的错误消息。我怎样才能解决这个问题并在我的 Apple Watch 应用程序中继续引用 class 和框架。我应该在 phone 和手表之间传输数据吗?有没有办法 link 扩展框架?
我想做的是在我的 InterfaceController 中:
override func willActivate() {
super.willActivate()
let defaultsShared = NSUserDefaults(suiteName: "somesharedappgroup")
let defaults = NSUserDefaults.standardUserDefaults()
if let barcodeString = defaultsShared!.objectForKey("barcode") as? String {
if let barcodeContent = RSUnifiedCodeGenerator.shared.generateCode(barcodeString, machineReadableCodeObjectType: AVMetadataObjectTypeCode39Code) {
barcode.setImage(barcodeContent)
label.setText("ID: \(barcodeString)")
} else {
label.setText("Please setup extensions in the settings of SHPID.")
barcode.setImage(nil)
}
} else {
label.setText("Please setup extensions in the settings of SHPID.")
barcode.setImage(nil)
}
}
RSUnifiedCodeGenerator
是一个 class,它利用 AVFoundation 从字符串生成条形码图像。此外,生成器采用的类型是一个 AVObject:AVMetadataObjectTypeCode39Code。该解决方案在第一个 WatchOS 中运行良好,但现在在 OS 中仍然存在问题。 2. 我看到 WatchConnectivity 可能是一个解决方案,它只是将 phone 中的条形码传递给我本身,但这需要我停止支持 iOS 8. 将 AVFoundation 与 WatchOS 一起使用的最佳解决方案是什么(如果有的话) 2. 如果我做不到,我还应该怎么做调用时将此图像从 phone 传递给手表。谢谢。
我认为使用 WatchConnectivity 是正确的做法。
对于以前的版本支持,如果唯一的经销商断路器是 AVMetadataObjectTypeCode39Code
,也许您可以打印它的值并将其直接传递给函数?
这是一个关于如何为您的应用程序使用 WatchConnectivity 的示例。
请注意,这个例子很粗糙并且没有处理错误。会话管理也应该得到一些稳定产品的关注。
iPhone AppDelegate
import UIKit
import WatchConnectivity
import AVFoundation
import RSBarcodes
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, WCSessionDelegate {
var window: UIWindow?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
if WCSession.isSupported() {
let session = WCSession.defaultSession()
session.delegate = self
session.activateSession()
}
return true
}
// On Watch sends the message.
// Will not reply as we will push a data message with image.
func session(session: WCSession, didReceiveMessage message: [String : AnyObject]) {
if "generateBarcode" == message["id"] as! String {
let code = message["code"] as! String
let barcodeImage = RSUnifiedCodeGenerator.shared.generateCode(code,
machineReadableCodeObjectType: AVMetadataObjectTypeCode39Code)!
if WCSession.isSupported() {
let session = WCSession.defaultSession()
session.delegate = self
session.activateSession()
session.sendMessageData(UIImagePNGRepresentation(barcodeImage)!,
replyHandler: nil, errorHandler: nil)
}
}
}
}
观看InterfaceController
import WatchKit
import Foundation
import WatchConnectivity
class InterfaceController: WKInterfaceController, WCSessionDelegate {
@IBOutlet var barcodeImage: WKInterfaceImage!
override func willActivate() {
super.willActivate()
if WCSession.isSupported() {
let session = WCSession.defaultSession()
session.delegate = self
session.activateSession()
// Send a message requesting a barcode image
session.sendMessage(
["id": "generateBarcode", "code": "2166529V"],
replyHandler: nil, // Do not handle response, iPhone will push a data message
errorHandler: nil)
}
}
// On iPhone pushes a data message
func session(session: WCSession, didReceiveMessageData messageData: NSData) {
barcodeImage.setImage(UIImage(data: messageData))
}
}
我将我的应用更新为最新的 swift 2.0 语法。这样一来,My watchkit 应用程序就坏了。问题是 watchkit 应用程序引用了一个引用框架 AVFoundation 的 class。 WatchOS2 显然现在不再支持一些标准框架:
Support for network-based operations includes the following technologies:
WatchKit extensions can access the network directly through an NSURLSession object. WatchKit extensions have full access to the NSURLSession capabilities, including the ability to download files in the background. For information on how to use this class, see URL Loading System Programming Guide. The Watch Connectivity framework supports bidirectional communication between your Watch app and iOS app. Use this framework to coordinate activities between the two apps. See Communicating with Your Companion iOS App.
Available System Technologies for WatchKit
所以现在我无法编译手表套件代码,因为 "no such module found" 是尝试使用 AVFoundation 框架时的错误消息。我怎样才能解决这个问题并在我的 Apple Watch 应用程序中继续引用 class 和框架。我应该在 phone 和手表之间传输数据吗?有没有办法 link 扩展框架?
我想做的是在我的 InterfaceController 中:
override func willActivate() {
super.willActivate()
let defaultsShared = NSUserDefaults(suiteName: "somesharedappgroup")
let defaults = NSUserDefaults.standardUserDefaults()
if let barcodeString = defaultsShared!.objectForKey("barcode") as? String {
if let barcodeContent = RSUnifiedCodeGenerator.shared.generateCode(barcodeString, machineReadableCodeObjectType: AVMetadataObjectTypeCode39Code) {
barcode.setImage(barcodeContent)
label.setText("ID: \(barcodeString)")
} else {
label.setText("Please setup extensions in the settings of SHPID.")
barcode.setImage(nil)
}
} else {
label.setText("Please setup extensions in the settings of SHPID.")
barcode.setImage(nil)
}
}
RSUnifiedCodeGenerator
是一个 class,它利用 AVFoundation 从字符串生成条形码图像。此外,生成器采用的类型是一个 AVObject:AVMetadataObjectTypeCode39Code。该解决方案在第一个 WatchOS 中运行良好,但现在在 OS 中仍然存在问题。 2. 我看到 WatchConnectivity 可能是一个解决方案,它只是将 phone 中的条形码传递给我本身,但这需要我停止支持 iOS 8. 将 AVFoundation 与 WatchOS 一起使用的最佳解决方案是什么(如果有的话) 2. 如果我做不到,我还应该怎么做调用时将此图像从 phone 传递给手表。谢谢。
我认为使用 WatchConnectivity 是正确的做法。
对于以前的版本支持,如果唯一的经销商断路器是 AVMetadataObjectTypeCode39Code
,也许您可以打印它的值并将其直接传递给函数?
这是一个关于如何为您的应用程序使用 WatchConnectivity 的示例。
请注意,这个例子很粗糙并且没有处理错误。会话管理也应该得到一些稳定产品的关注。
iPhone AppDelegate
import UIKit
import WatchConnectivity
import AVFoundation
import RSBarcodes
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, WCSessionDelegate {
var window: UIWindow?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
if WCSession.isSupported() {
let session = WCSession.defaultSession()
session.delegate = self
session.activateSession()
}
return true
}
// On Watch sends the message.
// Will not reply as we will push a data message with image.
func session(session: WCSession, didReceiveMessage message: [String : AnyObject]) {
if "generateBarcode" == message["id"] as! String {
let code = message["code"] as! String
let barcodeImage = RSUnifiedCodeGenerator.shared.generateCode(code,
machineReadableCodeObjectType: AVMetadataObjectTypeCode39Code)!
if WCSession.isSupported() {
let session = WCSession.defaultSession()
session.delegate = self
session.activateSession()
session.sendMessageData(UIImagePNGRepresentation(barcodeImage)!,
replyHandler: nil, errorHandler: nil)
}
}
}
}
观看InterfaceController
import WatchKit
import Foundation
import WatchConnectivity
class InterfaceController: WKInterfaceController, WCSessionDelegate {
@IBOutlet var barcodeImage: WKInterfaceImage!
override func willActivate() {
super.willActivate()
if WCSession.isSupported() {
let session = WCSession.defaultSession()
session.delegate = self
session.activateSession()
// Send a message requesting a barcode image
session.sendMessage(
["id": "generateBarcode", "code": "2166529V"],
replyHandler: nil, // Do not handle response, iPhone will push a data message
errorHandler: nil)
}
}
// On iPhone pushes a data message
func session(session: WCSession, didReceiveMessageData messageData: NSData) {
barcodeImage.setImage(UIImage(data: messageData))
}
}