如何在Watch OS 2中引用不受支持的框架 [英] How to reference non-supported frameworks in Watch OS 2

查看:96
本文介绍了如何在Watch OS 2中引用不受支持的框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将我的应用更新为最新的swift 2.0语法。这样做,我的Watchkit应用程序已经破碎。问题是watchkit app引用了一个引用框架AVFoundation的类。 WatchOS2显然现在不再支持某些标准框架:


支持基于网络的操作包括以下技术:



WatchKit扩展可以通过
NSURLSession对象直接访问网络。 WatchKit扩展可以完全访问
NSURLSession功能,包括在后台下载
文件的功能。有关如何使用此类的信息,请参阅URL
加载系统编程指南。 Watch Connectivity框架
支持Watch应用程序和iOS
应用程序之间的双向通信。使用此框架来协调两个应用程序之间的活动。
请参阅与您的Companion iOS应用程序通信。




iPhone AppDelegate

  import UIKit 
import WatchConnectivity
import AVFoundation
import RSBarcodes

@UIApplicationMain
class AppDelegate:UIResponder,UIApplicationDelegate,WCSessionDelegate {

var window:UIWindow?


func应用程序(应用程序:UIApplication,didFinishLaunchingWithOptions launchOptions:[NSObject:AnyObject]?) - > Bool {

//在应用程序启动后覆盖定制点。

如果WCSession.isSupported(){
let session = WCSession.defaultSession()
session.delegate = self
session.activateSession()
}

返回true
}

// On Watch发送消息。
//将不会回复,因为我们将推送带有图像的数据消息。
func session(session:WCSession,didReceiveMessage message:[String:AnyObject]){
ifgenerateBarcode== message [id] as!字符串{
让代码=消息[代码]为! String
let barcodeImage = RSUnifiedCodeGenerator.shared.generateCode(code,
machineReadableCodeObjectType:AVMetadataObjectTypeCode39Code)!

如果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!

覆盖func willActivate(){
super.willActivate()

如果WCSession.isSupported(){
let session = WCSession.defaultSession()
session.delegate = self
session.activateSession()

//发送请求条形码图像的消息
session.sendMessage(
[id :generateBarcode,code:2166529V],
replyHandler:nil,//不处理响应,iPhone会推送数据消息
errorHandler:nil)
}
}

//在iPhone上推送数据消息
func session(session:WCSession,didReceiveMessageData messageData:NSData){
barcodeImage.setImage(UIImage(data:messageData) ))
}
}


I updated my app to the latest swift 2.0 syntax. In doing so, My watchkit app has become broken. The issue is the watchkit app references a class that references the framework AVFoundation. WatchOS2 apparently now no longer supports some of the standard frameworks:

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

So now I cannot compile the watch kit code as "no such module found" is an error message when trying to use the AVFoundation framework. How can I get around this and keep referencing that class and framework in my apple watch app. Should I be communicating data between the phone and the watch? Is there a way to link the framework to the extension?

What I am trying to do is the following, in my 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)

    }
}

The RSUnifiedCodeGenerator being a class that utilizes AVFoundation to generate barcode images from strings. Furthermore, the type that generator takes is an AVObject: AVMetadataObjectTypeCode39Code. This solution worked well in the first WatchOS, but now remains broken in OS 2. I see that WatchConnectivity may be a solution, and have it just pass me the barcode from the phone itself, but that would require I stop supporting iOS 8. What is the best solution, if any, for using AVFoundation with WatchOS 2. If I can not do that, how else should I go about passing this image to the watch from the phone when called. Thanks.

解决方案

This is an example on how you could use WatchConnectivity for your app.

Please not that this example is rough and does not handle error. The session management should also get some attention for a stable product.

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

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

这篇关于如何在Watch OS 2中引用不受支持的框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆