iphone - Call Objective C method from JavaScript with parameter - Stack Overflow

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

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 mitAnimations];
 NSLog(@"After:%f",refToSelf.carouselView.alpha);

}

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

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 mitAnimations];
 NSLog(@"After:%f",refToSelf.carouselView.alpha);

}

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

Share Improve this question edited Aug 14, 2015 at 9:53 vaultah 46.7k12 gold badges120 silver badges144 bronze badges asked Jul 18, 2010 at 10:10 RonyRony 1,2294 gold badges22 silver badges42 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 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 ponentsSeparatedByString:@"&"];
        int paramsAmount = [paramsArray count];

        for (int i = 0; i < paramsAmount; i++) {
            NSArray *keyValuePair = [[paramsArray objectAtIndex:i] ponentsSeparatedByString:@"="];
            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';

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./dannywartnaby/KPWebViewInterceptor

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./iphone/library/documentation/uikit/reference/UIWebViewDelegate_Protocol/Reference/Reference.html

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745138306a4613309.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信