watch-connectivity / react-native-watch-connectivity

Enable communication between apple watch app and react native
http://mtford.co.uk/react-native-watch-connectivity/
MIT License
710 stars 91 forks source link

iOS don't recognize app #129

Open joaonew opened 1 week ago

joaonew commented 1 week ago

Issue: Trouble Integrating WatchOS Functionality into React Native App

Environment

Steps Taken

  1. Installed the library using yarn.
  2. Added a new watchOS target to the RN app through Xcode as a companion app to the iOS app.
  3. Created an Info.plist file for the watchOS target, mirroring the example provided in the library.
  4. Updated the iOS app's Info.plist with:
    <key>WKCompanionAppBundleIdentifier</key>
    <string>$(BUNDLEID).watchkitapp</string>
  5. Updated WatchApp.swift and ContentView.swift to receive text input from the iOS app.
  6. Created watchConnectivity.js in the RN app to handle watch connectivity.
  7. Updated App.tsx to initialize watch connectivity and attempt sending a message to watchOS.
  8. Testing with a real iOS device paired with an Apple Watch.

Current Status

Code Snippets

WatchApp.swift

import SwiftUI
import WatchConnectivity

class WatchConnectivityManager: NSObject, WCSessionDelegate, ObservableObject {
    // ... (full code omitted for brevity)
}

@main
struct ClickCardWatchApp: App {
    @StateObject private var connectivityManager = WatchConnectivityManager.shared

    var body: some Scene {
        WindowGroup {
            ContentView()
                .environmentObject(connectivityManager)
        }
    }
}

watchConnectivity.js

import { useEffect, useCallback } from 'react'
import {
  watchEvents,
  sendMessage,
  useReachability,
} from 'react-native-watch-connectivity'

const useWatchConnectivity = () => {
  const isReachable = useReachability()

  useEffect(() => {
    console.log('Watch reachability status:', isReachable)
  }, [isReachable])

  useEffect(() => {
    console.log('Initializing watch connectivity...')

    const messageUnsubscribe = watchEvents.addListener(
      'message',
      (message, reply) => {
        console.log('Received message from watch:', message)
        reply({ text: 'Message received by React Native app' })
      },
    )

    return () => {
      console.log('Cleaning up watch connectivity...')
      messageUnsubscribe()
    }
  }, [])

  const sendMessageToWatch = useCallback(
    async (message) => {
      console.log('Attempting to send message:', message)
      if (isReachable) {
        try {
          await sendMessage(message, (reply) => {
            console.log('Reply from watch:', reply)
          })
          console.log('Message sent to watch successfully')
        } catch (error) {
          console.error('Failed to send message to watch:', error)
        }
      } else {
        console.log('Watch is not reachable. Cannot send message.')
      }
    },
    [isReachable],
  )

  return { isReachable, sendMessageToWatch }
}

export default useWatchConnectivity

Issue

Despite following the setup steps, I'm unable to establish communication between the iOS app and the watchOS app. The iOS app detects that the watch is paired, but I can't confirm if the watch app is installed or reachable.

Questions

  1. Am I missing any crucial steps in the setup process?
  2. Do I need to make changes to the AppDelegate? If so, what changes are required?
  3. Are there any known issues with this library and the current React Native version (0.74.4)?
  4. How can I debug the communication between the iOS app and the watchOS app?

Additional Information

Any help or guidance would be greatly appreciated. Thank you!

domthomhive commented 4 days ago

Which Apple Watch series device/simulator is this re-producible on? 1 or 2?