2

I succeed to call a Objective C method from a JavaScript method using the following

///javascript

    function hi()
     {
     CallNKitAction("callFromJavascript className=TestCarouselViewController&index="+index);   
     }
            hi();
////objective c

-(void)callFromJavascript
{



 NSLog(@"Before:%f",refToSelf.carouselView.alpha);
 [UIView beginAnimations:nil context:nil];
 [UIView setAnimationDuration:0.6f];
 refToSelf.carouselView.alpha=0.0f;
 [UIView commitAnimations];
 NSLog(@"After:%f",refToSelf.carouselView.alpha);

}

but I don't know how to call with a parameter. Can anyone help?

3 Answers 3

2

Here is some code sample (inside the Objective-C):

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    NSString *url = [[request URL] absoluteString];

    static NSString *urlPrefix = @"myApp://";

    if ([url hasPrefix:urlPrefix]) {
        NSString *paramsString = [url substringFromIndex:[urlPrefix length]];
        NSArray *paramsArray = [paramsString componentsSeparatedByString:@"&"];
        int paramsAmount = [paramsArray count];

        for (int i = 0; i < paramsAmount; i++) {
            NSArray *keyValuePair = [[paramsArray objectAtIndex:i] componentsSeparatedByString:@"="];
            NSString *key = [keyValuePair objectAtIndex:0];
            NSString *value = nil;
            if ([keyValuePair count] > 1) {
                value = [keyValuePair objectAtIndex:1];
            }

            if (key && [key length] > 0) {
                if (value && [value length] > 0) {
                    if ([key isEqualToString:"index"]) {
                        // Use the index...
                    }
                }
            }
        }

        return NO;
    }
    else {
        return YES;
    }
}

Inside JS:

location.href = 'myApp://index=10';
5
  • hi...thanks for replay...i tried your code...but the delegate does not fire....although i implement uiwebviewdelegate....
    – Rony
    Jul 18, 2010 at 11:34
  • Have you 'told' the web view that your view controller is its delegate? Add the next line: webView.delegate = self; in your viewDidLoad method... Jul 18, 2010 at 12:03
  • Or connect the delegate of the webView to your view controller in Interface Builder (if you use Interface Builder)... Jul 18, 2010 at 12:08
  • yeap i did it...there was no prob with this as u think...i think as i am using NimbleKit...i should go through other approach..ok..thanks u a lot..i solve the prob... :)
    – Rony
    Jul 18, 2010 at 12:33
  • Probably this is the reason. I believe that NimbleKit does exactly the same as I've posted here. Jul 19, 2010 at 6:47
2

Noticing that this is unanswered - I have a little project that simplifies the calling of Objective-C from UIWebViews.

It provides a small Javascript function to invoke the Objective-C and a class (called KPWebViewInterceptor) that sits between your WebView and ViewController.

You can read about it and download from: https://github.com/dannywartnaby/KPWebViewInterceptor

0
0

You'll have to do that with the UIWebViewDelegate. You probably already have a UIWebView, the only thing you have to do is to check out this delegate-method:

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType

Source: http://developer.apple.com/iphone/library/documentation/uikit/reference/UIWebViewDelegate_Protocol/Reference/Reference.html

1
  • hi...thanks for so quick replay....can u say more details?? i want to pass the "index" value from javascript to obj c.. can u give sample code of what u want to say??
    – Rony
    Jul 18, 2010 at 10:26

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

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.