How to reference non-supported frameworks in Watch OS 2

前端 未结 2 1400
日久生厌
日久生厌 2021-02-20 10:35

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 A

2条回答
  •  耶瑟儿~
    2021-02-20 11:03

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

提交回复
热议问题