2017-01-18 3 views
0

Ich greife auf Kontakte eines Benutzers zu, um den Kontakt Name und Geburtstag vom iPhone an die AppleWatch zu senden. Ich habe Zugriff auf die Kontakte und kann die Daten auf dem Telefon anzeigen, aber ich habe Probleme beim Senden der Informationen mit WatchConnectivity. Ich benutze das Ray Wenderlich Buch WatchOS by Tutorials als Leitfaden, habe aber immer noch Probleme.Sende Name und Geburtstag an Suche mit WatchConnectivity

Hier ist, was ich auf der Telefonseite habe, Watch Connectivity in AppDelegate.swift einzurichten.

// MARK: Watch Connectivity 
extension AppDelegate: WCSessionDelegate { 

func sessionDidBecomeInactive(_ session: WCSession) { 
    print("WC Session did become inactive.") 
} 

func sessionDidDeactivate(_ session: WCSession) { 
    print("WC Session did deactivate.") 
    WCSession.default().activate() 
} 


func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) { 
    if let error = error { 
     print("WC Session activation failed with error:" + "\(error.localizedDescription)") 
     return 
    } 
    print("Phone activated with state:" + "\(activationState.rawValue)") 
} 

func setUpWatchConnectivity() { 
    if WCSession.isSupported() { 
     let session = WCSession.default() 
     session.delegate = self 
     session.activate() 
     } 
    } 

} 

Und das in ViewController.swift

// MARK Watch Connectivity 

extension ViewController { 


func sendNameAndBirthdayToWatch() { 
    if WCSession.isSupported() { 
     let session = WCSession.default() 
     if session.isWatchAppInstalled { 
      let nameAndBirthday = ["name": nameLabel.text, "birthday": birthdayLabel.text] 
      do { 
       try session.updateApplicationContext(nameAndBirthday) 
      } catch { 
       print("Error") 
      } 
     } 
    } 
    } 

} 

Ich rufe sendNameAndBirthdayToWatch() mit einem UIButton am Telefon. Als Prototyp zeigt das Telefon derzeit den Namen und den Geburtstag auf zwei separaten Etiketten an, die aus den Kontakten im Xcode-Simulator stammen, damit ich zumindest weiß, dass ich die Daten erhalte.

Auf die Uhr in der ExtensionDelegate.swift Ich habe.

// MARK: Watch Connectivity 
extension ExtensionDelegate: WCSessionDelegate { 

func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) { 
    if let error = error { 
     print("Activation failed with error: \(error.localizedDescription)") 
     return 
    } 
    print("Watch activated with activation state: \(activationState.rawValue) ") 
} 

func setupWatchConnectivity() { 
    if WCSession.isSupported() { 
     let session = WCSession.default() 
     session.delegate = self 
     session.activate() 
    } 
} 

func session(_ session: WCSession, didReceiveApplicationContext applicationContext: [String : Any]) { 
    if let name = applicationContext["name"] as? String, let birthday = applicationContext["birthday"] as? String { 
     InterfaceController.sharedInterfaceController.updateWatchLabel(name: name, birthday: birthday) 
    } 
} 
} 

Hier bin ich fest. Ich bin mir nicht sicher, wo ich falsch gelaufen bin und/oder wie ich mich vorwärts bewegen kann.

Ich rufe updateWatchLabel() in der InterfaceController.swift auf der Uhr, aber ich sehe keine Ergebnisse.

Vielen Dank im Voraus für jede Hilfe. Ich habe das ganze eine ganze Woche lang angeguckt und leider ist das Beispielprojekt im Wenderlich-Buch ein bisschen zu kompliziert für mein Verständnis.

Ich verwende Xcode 8.2.1 und Swift 3.

+0

Haben Sie versucht, echte Geräte anstelle des Simulators zu verwenden, um zu sehen, ob Sie dann mehr Erfolg haben? – ccjensen

+0

@ccjensen Ich habe und was ich bemerkt habe ist, dass ich keine Nachricht in der Konsole bekomme, dass WatchConnectivity auf der Uhr aktiviert ist. Daran arbeite ich gerade. –

Antwort

1

In der Zeit antwortete ich meine eigene Frage.

Hier ist der Code auf dem Telefon in ViewController.swift. Der Code bleibt der gleiche wie in meinem ursprünglichen Post in AppDelegate.swift.

func sendMessageToWatch() { 
    if WCSession.isSupported() { 
     let session = WCSession.default() 
     if session.isWatchAppInstalled { 
      do { 
       let message = ["message": "A message as String"] 
       try session.updateApplicationContext(message) 
      } catch { 
       print("Error: \(error)") 
      } 

     } else if session.isWatchAppInstalled == false { 
      print("No watch app installed") 
     } 
    } 
} 

Ich bin im Moment Aufruf dieser Funktion durch Drücken einer UIButton auf dem Telefon. Dann auf der Uhrseite in InterfaceController.swift für das Empfangsende. Ich habe im Grunde den Code von ExtensionDelegate.swift zu InterfaceController.swift verschoben.

// MARK: Watch Connectivity 
func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) { 
    if let error = error { 
     print("Activation failed with error: \(error.localizedDescription)") 
     return 
    } 
    print("Watch activated with activation state: \(activationState.rawValue) ") 
} 


func setupWatchConnectivity() { 
    if WCSession.isSupported() { 
     let session = WCSession.default() 
     session.delegate = self 
     session.activate() 
    } 
} 

func session(_ session: WCSession, didReceiveApplicationContext applicationContext: [String : Any]) { 
    let message = applicationContext["message"] as? String 

    DispatchQueue.main.async(execute: { 
     self.label.setText(message) 
    }) 

}