0

I was wondering if anyone knew how to do this. I have a bunch of NSNotifications and I'd like to create listeners in Javascript, embedded in a UIWebView, that will get executed when the NSNotifications are received.

I know this is possible using PhoneGap and the method sendPluginResult, but I was wondering if there was another way of doing it without cordova.

Thanks

2 Answers 2

0

Create your listener outside the UIWebView and send the stringByEvaluatingJavaScriptFromString message to your web view.

- (void)registerObserver
{
    NSArray *names = [NSArray arrayWithObjects:
        @"FirstNotification", @"SecondNotification", @"ThirdNotification", nil];

    for (NSString *name in names)
    {
        [[NSNotificationCenter defaultCenter]
            addObserver:self
               selector:@selector(notificationReceived:)
                   name:@"NotificationName"
                 object:nil];
    }
}

- (void)notificationReceived:(NSNotification *notification)
{
    NSString *js = [NSString stringWithFormat:
        @"notificationReceived('%@');", notification.name];
    [self.webView stringByEvaluatingJavaScriptFromString:js];
}
0

in the call back NSNotification method call this:

[yourwebview stringByEvaluatingJavaScriptFromString:@"methodName()"];

and create that "methodname" in your javascript code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.