views:

95

answers:

3

Hi all,

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

A: 

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

Tim van Elsloo
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
ok..at last i got it but using different method...thanks all
Rony
A: 

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

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: http://blog.knowledgeisporridge.com/?p=73

dannywartnaby