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

前端之家收集整理的这篇文章主要介绍了ios – 如何在Watch OS 2中引用不支持的框架前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我更新了我的应用程序到最新的 swift 2.0语法.在这样做时,我的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

所以现在我无法编译手表套件代码为“没有这样的模块发现”是一个错误消息,当试图使用AVFoundation框架.我如何解决这个问题,并在我的苹果手表应用程序中继续引用该类和框架.我应该在手机和手表之间传输数据吗?有没有办法将框架链接到扩展名?

我正在尝试做的是以下,在我的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中运行良好,但现在在操作系统2中仍然存在破坏.我看到WatchConnectivity可能是一个解决方案,并且只是让我从手机本身传递条形码,但这将需要我停止支持iOS 8.什么是最好的解决方案,如果有的话,在WatchOS 2中使用AVFoundation.如果我不能这样做,我还应该怎么去把这个图像从手机传递到手表.谢谢.

解决方法

这是一个关于如何为您的应用程序使用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"],// 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))
  }
}
原文链接:https://www.f2er.com/iOS/330160.html

猜你在找的iOS相关文章