Apple Watch 与 iOS 应用程序通信一个新的 swift 项目与现有的 obj c 项目
Apple watch communicate with iOS app a new swift project vs to existing obj c project
1) 我已经使用完整的 swift 项目(iOS app in swift 和 Apple Watch target in swift)
代码:
在InterfaceController.swift
@IBAction func buttonPressed() {
let watchMessage = ["SiteName" : "Tech-recipes"]
WKInterfaceController.openParentApplication(watchMessage, reply: { (reply:[NSObject : AnyObject]!, error: NSError!) -> Void in
if let message = reply["Message"] as? String{
println(message)
}
})
}
在AppDelegate.swift
func application(application: UIApplication!, handleWatchKitExtensionRequest userInfo: [NSObject : AnyObject]!, reply: (([NSObject : AnyObject]!) -> Void)!) {
if let siteName = userInfo["SiteName"] as? String{
reply(["Message":"Hello from \(siteName)"])
}
}
输出:"Hello from Tech-recipes"
2) 但是,当我想将 swift 中的 apple watch 目标集成到 obj-c 中的现有项目时,它会崩溃并给我这个错误:"fatal error: unexpectedly found nil while unwrapping an Optional value"
在InterfaceController.swift
@IBAction func buttonPressed() {
let watchMessage = ["SiteName" : "Tech-recipes"]
WKInterfaceController.openParentApplication(watchMessage, reply: { (reply:[NSObject : AnyObject]!, error: NSError!) -> Void in
if let message = reply["Message"] as? String{ //CRASH HERE!!!!
println(message)
}
})
}
iPhoneAppDelegate.m
- (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void (^)(NSDictionary *))reply{
// Performs task on phone here
// Sending back a reply
if ([[userInfo valueForKey:@"SiteName"] length]>0) {
//NSMutableDictionary *reply = [[NSMutableDictionary alloc] init];
[reply setValue:[NSString stringWithFormat:@"Hello from %@", [userInfo valueForKey:@"SiteName"]] forKey:@"Message"];
}
}
更新:
--> 注意到在 handleWatchKitExtensionRequest: 方法中,userInfo 的类型在 swift 和 obj-c 中是不同的,在 [NSObject : AnyObject] 中!和 NSDictionary 分别。如何解决?
--> 错误中得到一个错误:NSError!: [0] (null) @"NSLocalizedDescription" : @"The UIApplicationDelegate in the iPhone App never called reply() in -[UIApplicationDelegate application:handleWatchKitExtensionRequest:reply:]"
解决了!
错过了应用委托中的回复(aNsdictionary)
1) 我已经使用完整的 swift 项目(iOS app in swift 和 Apple Watch target in swift)
代码:
在InterfaceController.swift
@IBAction func buttonPressed() {
let watchMessage = ["SiteName" : "Tech-recipes"]
WKInterfaceController.openParentApplication(watchMessage, reply: { (reply:[NSObject : AnyObject]!, error: NSError!) -> Void in
if let message = reply["Message"] as? String{
println(message)
}
})
}
在AppDelegate.swift
func application(application: UIApplication!, handleWatchKitExtensionRequest userInfo: [NSObject : AnyObject]!, reply: (([NSObject : AnyObject]!) -> Void)!) {
if let siteName = userInfo["SiteName"] as? String{
reply(["Message":"Hello from \(siteName)"])
}
}
输出:"Hello from Tech-recipes"
2) 但是,当我想将 swift 中的 apple watch 目标集成到 obj-c 中的现有项目时,它会崩溃并给我这个错误:"fatal error: unexpectedly found nil while unwrapping an Optional value"
在InterfaceController.swift
@IBAction func buttonPressed() {
let watchMessage = ["SiteName" : "Tech-recipes"]
WKInterfaceController.openParentApplication(watchMessage, reply: { (reply:[NSObject : AnyObject]!, error: NSError!) -> Void in
if let message = reply["Message"] as? String{ //CRASH HERE!!!!
println(message)
}
})
}
iPhoneAppDelegate.m
- (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void (^)(NSDictionary *))reply{
// Performs task on phone here
// Sending back a reply
if ([[userInfo valueForKey:@"SiteName"] length]>0) {
//NSMutableDictionary *reply = [[NSMutableDictionary alloc] init];
[reply setValue:[NSString stringWithFormat:@"Hello from %@", [userInfo valueForKey:@"SiteName"]] forKey:@"Message"];
}
}
更新:
--> 注意到在 handleWatchKitExtensionRequest: 方法中,userInfo 的类型在 swift 和 obj-c 中是不同的,在 [NSObject : AnyObject] 中!和 NSDictionary 分别。如何解决?
--> 错误中得到一个错误:NSError!: [0] (null) @"NSLocalizedDescription" : @"The UIApplicationDelegate in the iPhone App never called reply() in -[UIApplicationDelegate application:handleWatchKitExtensionRequest:reply:]"
解决了!
错过了应用委托中的回复(aNsdictionary)