Pass a swift closure to an objective-C function which takes a block as parameter - ios

I have a function in objective-C as following
- (void) fetchChannelListForWatch:(void (^)(NSDictionary *))callback
I want to pass a swift callback closure into this like this:
fetchChannelListForWatch(replyHandler)
where replyHandler is a closure of type
replyHandler: ([String : AnyObject]) -> Void)
and I get the error:
Cannot invoke 'fetchChannelListForWatch' with an argument list of type '(([String : AnyObject]) -> Void)'
The replyHandler is coming from WatchConnectivity delegate
func session(session: WCSession, didReceiveMessage message: [String : AnyObject], replyHandler: ([String : AnyObject]) -> Void)
so I cannot change the type of replyHandler.
How can I pass my swift closure with parameter
replyHandler: [String: AnyObject] -> ()
into an objective-C function that takes a block with parameter
- (void) fetchChannelListForWatch:(void (^)(NSDictionary *))callback
Your help is much appreciated!

I think this could be a shortcut to your problem:
func session(session: WCSession, didReceiveMessage message: [String : AnyObject], replyHandler: ([String : AnyObject]) -> Void){
let objCObject = ObjectiveCClass()
objCObject.fetchChannelListForWatch { (dict) -> Void in
replyHandler(dict as! [String : AnyObject]?)
}
}

The bridged type for NSDictionary is
[NSObject: AnyObject]
In your case you need to update your replyHandler to
replyHandler: ([NSObject : AnyObject]) -> Void)
Here is the relevant documentation https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/WorkingWithCocoaDataTypes.html

Related

Watch can not receive reply from phone with WatchConnectivity

Test with iphone11(14.6) and watch series3(7.5)
In my project, I use WatchConnectivity between phone and watch to communication,
When watch send message with below code, every time run is OK.
wcSession.sendMessage(message as [String : Any], replyHandler: nil)
But when watch send message need reply, it seems error.
wcSession.sendMessage(message) { (reply) in
} errorHandler: { (error) in
}
In Phone with below code to deal with request
func session(_ session: WCSession, didReceiveMessage message: [String : Any]) {
}
func session(_ session: WCSession, didReceiveMessage message: [String : Any], replyHandler: #escaping ([String : Any]) -> Void) {
}
I debug the code, didReceiveMessage with reply data can be called, but watch can not receive the reply. So what’s my problem, Before the mobile and watch update, everything is good, every update will cause problems, how to ensure the stability of the app?Thank you very much.

Apple Watch Kit load data on start up

How to quickly load data on Apple Watch? UserDefaults doesn't work since watchOS 2, so we can only use WCSessionDelegate, right?
Now, on Watch App start I call wcSession?.sendMessage(someThing, replyHandler: someFunc, errorHandler: otherFunc), then on iPhone app I send back some data in
func session(_ session: WCSession, didReceiveMessage message: [String : Any], replyHandler: #escaping ([String : Any]) -> Void)
And finally receive it on watch app in func session(_ session: WCSession, didReceiveMessage message: [String : Any]), but that takes like 3 seconds.
What would be better way to get data on start up?
You could try using the reply Handler function instead of initiating a new message. It should work faster.
Call this from watch:
func sendRequest() {
if session.activationState == .activated && session.isReachable {
session.sendMessage(["Watch Message" : "Request"], replyHandler: { (reply) in
// Handle reply here
}, errorHandler: { (error) in
print("***** Error Did Occur: \(error) *****")
})
}
}
Handle and Respond on Phone:
func session(_ session: WCSession, didReceiveMessage message: [String : Any], replyHandler: #escaping ([String : Any]) -> Void) {
if let messageFromWatch = message["Watch Message"] {
let messageData = messageFromWatch as! String
// Message From Watch to Activate Watch Connectivity Session
if messageData == "Request" {
replyHandler(["Response" : data])
}
}
}

wrong WCSessionDelegate callback function called

I use the WCSession.defaultSession().sendMessage function to send a message from the watch to the iphone, with non nil replyHandler closure
on the iphone,
func session(session: WCSession, didReceiveMessage message: [String : AnyObject])
is called and not the callback that should be called:
func session(session: WCSession, didReceiveMessage message: [String : AnyObject], replyHandler: ([String : AnyObject]) -> Void)
and thus i cannot call replyHandler :-(
any idea why?
thanks a lot!
Edit:
code used to make the call:
WCSession.defaultSession().sendMessage(applicationDict, replyHandler: { (reply : [String : AnyObject]) -> Void in
...
},
errorHandler: { (er : NSError ) -> Void in
// Handle error
print(er.description)
});

Call a public method of a WKInterfaceController from ExtensionDelegate class- WatchKit

I am calling the
`[[WCSession defaultSession] updateApplicationContext:message error:error]`
method which is triggered in my
`-(void)session:(WCSession *)session didReceiveApplicationContext:(NSDictionary<NSString *,id> *)applicationContext`
method in ExtensionDelegate. But from here I want to call a public method in a WKInterfaceController to update my UI. I don't want to reload the root controllers as this particular controller is not the root controller. Is it possible to call any public method from ExtensionDelegate. Can I call
-(void)session:(WCSession *)session didReceiveApplicationContext:(NSDictionary<NSString *,id> *)applicationContext
from somewhere within Interface controller instead of ExtensionDelegate?
You can use WCSession's
public func sendMessage(message: [String : AnyObject], replyHandler: (([String : AnyObject]) -> Void)?, errorHandler: ((NSError) -> Void)?)
this will invoke WCSessionDelegate's
public func session(session: WCSession, didReceiveMessage message: [String : AnyObject], replyHandler: ([String : AnyObject]) -> Void)
method in delegate class. This can be used from both app and extension for instantaneous message transfer. Read more

Sending a dictionary from Watch to App - WatchConnectivity

I have a dictionary
dict = [Int : Bool]
and i was wondering if there is a simple way to send it to iPhone from watch and vice-versa through interactive messaging in WatchConnectivity framework?
Thank you.
You can do so like this:
if (WCSession.isSupported()) {
let session = WCSession.default
session.delegate = self
session.activate()
}
let dict = [Int : bool]
let message = ["message" : dict]
WCSession.default.sendMessage(message,
replyHandler: { (reply) -> Void in
},
errorHandler: { (error) -> Void in
}
)
In your watchkit extention write following code :
func getDataFromParentApp(image: String) {
let dictionary = ["Desired Word":image]
WKInterfaceController.openParentApplication(dictionary) {
(replyInfo, error) -> Void in
}
And in the AppDelegate write following one :
func application(application: UIApplication!,
handleWatchKitExtensionRequest userInfo: [NSObject : AnyObject]!,
reply: (([NSObject : AnyObject]!) -> Void)!) {
getImageForWordThatRhymesWithDat(userInfo, reply)
}
You can refer this link.

Resources