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';
Sign up to request clarification or add additional context in comments.

5 Comments

hi...thanks for replay...i tried your code...but the delegate does not fire....although i implement uiwebviewdelegate....
Have you 'told' the web view that your view controller is its delegate? Add the next line: webView.delegate = self; in your viewDidLoad method...
Or connect the delegate of the webView to your view controller in Interface Builder (if you use Interface Builder)...
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... :)
Probably this is the reason. I believe that NimbleKit does exactly the same as I've posted here.
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

Comments

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 Comment

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??

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.