iOS Development

swift – Plz be the Hero: Remedy the WatchOS-iOS Integration Problem for Digicam & OCR

Spread the love


I’ve developed an iOS app that performs OCR and interacts with GPT-4 for textual content processing. Now I am attempting to increase its performance to a companion WatchOS app. The WatchOS app ought to management the digital camera to seize photographs and talk with the iOS app to course of OCR and obtain outcomes.

Nonetheless, I am working into points with the WCSession communication between the 2 apps. The iOS app works nice by itself, however integrating WatchOS performance has been difficult. I would like the watch app to ship a message to the iOS app, set off the digital camera, carry out OCR, after which show the outcomes on the watch, with the flexibility to provoke a brand new seize course of.

Listed below are the important thing elements of my code:

WatchToiOSConnector.swift:

import Basis
import WatchConnectivity

class WatchConnector: NSObject, WCSessionDelegate {
    var session: WCSession
    
    override init() {
        self.session = WCSession.default
        tremendous.init()
        self.session.delegate = self
        self.session.activate()
    }
    
    func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) {
        // Deal with session activation completion
        if activationState == .activated {
            // The session is prepared for communication
        } else if let error = error {
            // Deal with the error
            print("WCSession activation failed with error: (error.localizedDescription)")
        }
    }
    
    func sendMacroToiOS(macro: String) {
        guard WCSession.default.isReachable else {
            // Deal with the error - the iOS app is probably not reachable
            print("Can not attain iOS app")
            return
        }
        
        let message = ["macro": macro]
        session.sendMessage(message, replyHandler: { reply in
            // Deal with the reply obtained from the iOS app, if wanted
            print("Acquired reply: (reply)")
        }, errorHandler: { error in
            // Deal with the error
            print("Error sending message: (error.localizedDescription)")
        })
    }
}

ContentView.swift for WatchOS:

import SwiftUI
import WatchConnectivity

struct ContentView: View {
    @ObservedObject var watchConnector = WatchConnector()
    @State non-public var photoCaptureSuccess: Bool?
    
    var physique: some View {
        VStack {
            Button("Seize Picture") {
                capturePhoto()
            }
            
            if let success = photoCaptureSuccess {
                Textual content(success ? "Picture captured efficiently!" : "Didn't seize picture.")
            }
        }
    }
    
    non-public func capturePhoto() {
        // Name a way on the WatchConnector to ship the picture seize request
        watchConnector.sendCapturePhotoMessage()
        // Replace the photoCaptureSuccess based mostly on the precise response from the iOS app.
    }
}

@primary
struct OCR_companion_watch_appApp: App {
    var physique: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *