gpt4 book ai didi

ios - 如何在 Watch OS 2 中引用不支持的框架

转载 作者:IT王子 更新时间:2023-10-29 05:37:12 26 4
gpt4 key购买 nike

我将我的应用更新为最新的 swift 2.0 语法。这样一来,My watchkit 应用程序就坏了。问题是 watchkit 应用程序引用了一个引用框架 AVFoundation 的类。 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

所以现在我无法编译 watch 套件代码,因为在尝试使用 AVFoundation 框架时出现“未找到此类模块”的错误消息。我怎样才能解决这个问题并在我的 Apple Watch 应用程序中继续引用该类和框架。我应该在手机和 watch 之间传输数据吗?有没有办法将框架链接到扩展?

我想做的是在我的 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 是一个利用 AVFoundation 从字符串生成条码图像的类。此外,生成器采用的类型是一个 AVObject:AVMetadataObjectTypeCode39Code。该解决方案在第一个 WatchOS 中运行良好,但现在在 OS 2 中仍然存在问题。我看到 WatchConnectivity 可能是一个解决方案,它只是将手机本身的条形码传递给我,但这需要我停止支持 iOS 8。什么是将 AVFoundation 与 WatchOS 2 一起使用的最佳解决方案(如果有的话)。如果我不能这样做,我还应该如何在调用时将此图像从手机传递到 watch 。谢谢。

最佳答案

这是一个关于如何为您的应用使用 WatchConnectivity 的示例。

请注意,这个例子很粗糙并且没有处理错误。 session 管理也应该得到一些稳定产品的关注。

enter image description here

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

关于ios - 如何在 Watch OS 2 中引用不支持的框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33006593/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com