हाल ही में, मैं एक परियोजना पर काम कर रहा हूं, फिर से वॉच/आईफोन संचार से संबंधित है। लेकिन मेरा कोड कभी-कभी काम करता है और कभी-कभी काम नहीं करता है जो मेरे लिए अजीब तरह का है क्योंकि मुझे लगता है कि कोड या तो काम करना चाहिए या नहीं। यह 50/50 नहीं हो सकता है। इसलिए, मुझे नहीं पता कि क्या गलत हो जाता है। iPhone परWCSession.sendMessage काम करता है 50/50
सेटअप WCSession:
class WatchCommunicationController: NSObject, WCSessionDelegate {
var session : WCSession?
override init(){
// super class init
super.init()
// if WCSession is supported
if WCSession.isSupported() { // it is supported
// get default session
session = WCSession.defaultSession()
// set delegate
session!.delegate = self
// activate session
session!.activateSession()
} else {
print("iPhone does not support WCSession")
}
}
... ...
}
Watch पर समान WCSession स्थापना:
class PhoneCommunicationController: NSObject, WCSessionDelegate {
var session : WCSession?
override init(){
// super class init
super.init()
// if WCSession is supported
if WCSession.isSupported() { // it is supported
// get default session
session = WCSession.defaultSession()
// set delegate
session!.delegate = self
// activate session
session!.activateSession()
} else {
print("Watch does not support WCSession")
}
}
... ...
}
Watch पर संदेश भेजने:
समारोह sendGesture (इशारा: जीकेजीस्टर) {
// if WCSession is reachable
if session!.reachable { // it is reachable
// create the interactive message with gesture
let message : [String : AnyObject]
message = [
"Type":"Gesture",
"Content":gesture.rawValue
]
// send message
session!.sendMessage(message, replyHandler: nil, errorHandler: nil)
print("Watch send gesture \(gesture)")
} else{ // it is not reachable
print("WCSession is not reachable")
}
}
संबंधित enum:
enum GKGesture: Int {
case Push = 0, Left, Right, Up, Down
}
iPhone पर संदेश प्राप्त होता है:
func session(session: WCSession, didReceiveMessage message: [String : AnyObject]) {
//retrieve info
let type = message["Type"] as! String
let content = message["Content"]
switch type {
case "Gesture":
handleGesture(GKGesture(rawValue: content as! Int)!)
default:
print("Received message \(message) is invalid with type of \(type)")
}
}
func handleGesture(gesture : GKGesture){
print("iPhone receives gesture \(gesture)")
var notificationName = ""
switch gesture {
case .Up:
notificationName = "GestureUp"
case .Down:
notificationName = "GestureDown"
case .Left:
notificationName = "GestureLeft"
case .Right:
notificationName = "GestureRight"
case .Push:
notificationName = "GesturePush"
}
NSNotificationCenter.defaultCenter().postNotificationName(notificationName, object: nil)
}
किसी भी तरह मैं Xcode पर मेरे Watch एप्लिकेशन डिबग नहीं कर सकते , डीबग सत्र बस नहीं होगा tach। मुझे नहीं पता क्यों। इसलिए, मैं सिर्फ आईफोन के साथ एक तरफा डीबग।
कभी कभी मैं "इशारा प्राप्त करता है" प्रिंट आउट, और कभी कभी नहीं। और अधिसूचना प्राप्त करने के लिए एक ही मिला है।
क्या आपने "सामग्री और सेटिंग्स रीसेट करें" की कोशिश की है और अपनी घड़ी/आईफोन सिमुलेटर और एक्सकोड को पुनरारंभ किया है? यह आपके डीबग सत्र को संलग्न करने के लिए सुझाव है। मुझे बताएं कि यह कैसे जाता है और फिर मैं वॉच कनेक्ट कनेक्टिविटी के मुद्दों की कोशिश कर सकता हूं और आपकी मदद कर सकता हूं – Philip
मैं अपने आईफोन और ऐप्पल वॉच के साथ डीबग करता हूं। –
क्या आपको पहुंच के लिए कुछ खास करना पड़ा? मेरा कोड बहुत समान है, लेकिन पहुंचने की जांच करते समय मेरा चेक हमेशा 'झूठा' देता है – prawn