4

I make any webView And I want to call from javascript any objective c method which return any parameter. I tried many ways but not allowed . Objective c method here:

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    NSURL *URL = [request URL];
    if ([[URL scheme] isEqualToString:@"donordialog"])
    {
        // now we need to figure out the function part
        NSString *functionString = [URL resourceSpecifier];

        if ([functionString hasPrefix:@"bloodTypeChanged"])
        {
            // the blood type has changed, now do something about it.
            NSString *parameter = [functionString stringByReplacingOccurrencesOfString:@"bloodTypeChanged" withString:@""];

            // remove the '(' and then the ')'
            parameter = [parameter stringByReplacingOccurrencesOfString:@"(" withString:@""];
            parameter = [parameter stringByReplacingOccurrencesOfString:@")" withString:@""];

            // log the paramter, as I don't know what to do with it right now
            UIAlertView *alert=[[ UIAlertView alloc] initWithTitle:@"iosdan javascripti"
                                                                           message:@"ddddddddd"
                                                                    delegate:nil
                                                                cancelButtonTitle:@"OK"
                                                               otherButtonTitles:nil];

            [alert show];
            //NSLog(@"%@", parameter);
        }

        return NO;
    }

    return YES;
}

javascript:

function myFunction() {
    url="http://example.com";
    window.location="donordialog:blooTypeChanged("+url+")";
}

html:

<button onclick="myFunction()">click me</button>

comment: I need that: it is obj c method for example. aaa{} . My aaa method must return any parameter. and I have wevView. I loaded any url to this webView: for example www.example.com/forios . I need call this (aaa) method from javascript which it is in html on www.example.com/forios and alert the result of aaa function. understand?. If understand, dont watch to my code. help please yourself anyway. android version of my question:Call Java function from JavaScript over Android WebView I want to alert some parameter returning from method. Help please. Thanks.

Community
  • 1
  • 1
Mehman Bashirov
  • 45
  • 1
  • 1
  • 5
  • Your question is not clear. What you want to achieve? – Piyush Dubey Feb 21 '14 at 07:11
  • Nothing to archieve. I want to alert any parameter witch returning from obj c method in WebView. İ tried all answers from stack and others. But not success. – Mehman Bashirov Feb 21 '14 at 07:17
  • I put this code for example . All answers likes this. – Mehman Bashirov Feb 21 '14 at 07:19
  • When do you call `myFunction()`? Also just so you know it really isn't clear exactly what you want I had to read it a couple of times to fully understand, and I should have to do that it should be clear from the get go. – Popeye Feb 21 '14 at 08:26
  • 1
    @Popeye on any button click. I need that: it is obj c method for example. **aaa{}** . My aaa method must return any parameter. and I have wevView. I loaded any url to this webView: for example www.example.com/forios . I need call this (aaa) method from javascript which it is in html on www.example.com/forios and alert the result of aaa function. :'( understand?. If understand, dont watch to my code. help please yourself anyway. – Mehman Bashirov Feb 21 '14 at 08:45

4 Answers4

3

Here is a quick and easy solution without JavaScriptCore for backwards compatibility (might consider upgrading to Javascriptcore ). Here's an easy implementation.

Note: this solution is for UIwebview only (not UIscrollview)

//viewcontroller.h

@interface ViewController : UIViewController<UIWebViewDelegate>

@property (strong, nonatomic) IBOutlet UIWebView *viewWeb;
@end

//viewcontroller.m

- (void)viewDidLoad
{
    [super viewDidLoad];


    NSString *fullURL = @"http:player.fusion.fm";
    NSURL *url = [NSURL URLWithString:fullURL];
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
    [_viewWeb loadRequest:requestObj];
   _viewWeb.delegate = self;
 }

 -(BOOL)webView:(UIWebView *)_viewWeb shouldStartLoadWithRequest:(NSURLRequest *)request   navigationType:(UIWebViewNavigationType)navigationType {
    NSURL *url = request.URL;
    if ([[url scheme] isEqualToString:@"ios"])
    {
        [self mute]; // <-- YOUR OBJ-C FUNCTION HERE
        NSLog(@"test");
        return YES;
    }else
     {
        return YES;
    }

 }

// The Javascript (within webview)

try
{
  window.location="ios://null"
}
catch(err)
{
}

Source: http://adoptioncurve.net/archives/2012/09/calling-objective-c-methods-from-javascript-in-a-uiwebview/

2

The most usual approach that I used to interact from javascript within obj-c is to changing hash. On required event in your js write window.location.hash = '#cmd_alertMessage'; after this your - (BOOL)webView: shouldStartLoadWithRequest: navigationType: will be called:

   - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    NSArray * compenents = [request.URL.absoluteString componentsSeparatedByString:@"#"];
    if (compenents.count > 1) {
        NSString * cmd = compenents[1];
        if ([cmd rangeOfString:@"cmd_alertMessage"].location != NSNotFound) {
            // Call your obj-c method and get appropriated param
            NSString * jsFunction = [NSString stringWithFormat:@"setParameterFromAAAMethod('%@')", [self aaa]];
            // Return this param back to js
            NSString * alertMessage = [webView stringByEvaluatingJavaScriptFromString:jsFunction];
        }
    }


    return YES;
}

- (NSString *)aaa
{
    return @"This is special parameter that you need";
}

So, it will work in 3 steps:

  1. Invoke hash changing to request parameter from obj-c code in js;
  2. Handle hash changed (parameter request) and return in back to js in obj-c;
  3. Handle recieved parameter in js again
Mykola Denysyuk
  • 1,935
  • 1
  • 15
  • 15
  • error:No visible interface for 'NSString' declares the selector 'isContainString' – Mehman Bashirov Feb 21 '14 at 08:57
  • 1
    Thanks for answer. but I want vise versa. I did that o android. android version of my question:http://stackoverflow.com/questions/10389572/call-java-function-from-javascript-over-android-webview – Mehman Bashirov Feb 21 '14 at 09:17
  • you undertand my question right . How I'll alert this param in javascript? Full write please. – Mehman Bashirov Feb 21 '14 at 10:26
  • I'm not really familiar with js, but: `function setParameterFromAAAMethod(parameter) { alert(parameter); }` – Mykola Denysyuk Feb 21 '14 at 11:10
  • not called. Please check your answer again. please please. I'm in difficulty. – Mehman Bashirov Feb 21 '14 at 12:00
  • may the problem is in - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType ? – Mehman Bashirov Feb 21 '14 at 12:06
  • did you set _webView's delegate? – Mykola Denysyuk Feb 21 '14 at 12:17
  • i mean,do you have code like `webView.delegate = self`? – Mykola Denysyuk Feb 21 '14 at 12:25
  • so `- (BOOL)webView: shouldStartLoadWithRequest: navigationType:` will never be called. You must to set webView.delegate = , where `SelfOrOther` must be an instance of class that has implementation of `- (BOOL)webView: shouldStartLoadWithRequest: navigationType:` method. Also, read [this article](https://developer.apple.com/library/ios/documentation/uikit/reference/UIWebView_Class/Reference/Reference.html#//apple_ref/occ/instp/UIWebView/delegate) – Mykola Denysyuk Feb 21 '14 at 12:32
  • where must put this code part? Iam not understand . – Mehman Bashirov Feb 21 '14 at 12:42
  • thanks for answers and comments. but now my problem is my problem yet. – Mehman Bashirov Feb 21 '14 at 14:27
  • I recommend you read [**DOCUMENTATION**](https://developer.apple.com/library/ios/documentation/uikit/reference/UIWebView_Class/Reference/Reference.html#//apple_ref/occ/instp/UIWebView/delegate) first, to know how use UIWebView propperly – Mykola Denysyuk Feb 21 '14 at 14:53
1

You can set a hidden iframe's location to something specific to your app, and then intercept that request in Obj-C.

Here's an overview and some examples: http://blog.techno-barje.fr//post/2010/10/06/UIWebView-secrets-part3-How-to-properly-call-ObjectiveC-from-Javascript/

Janis Kirsteins
  • 2,128
  • 16
  • 17
0

Well, I am not sure whether I got your question correct or not, but if you want to call any javascript method, then you can do this way:-

[yourWebView stringByEvaluatingJavaScriptFromString:@"yourMethodName()"];

You can also pass parameters in above method.

Piyush Dubey
  • 2,416
  • 1
  • 24
  • 39
  • 2
    Thanks for answer. But I want vise versa. I mean, write any method in obj c, whitch return any string or int parameter. And alert this parameter in javascript (webview). call this method from javascript whitch included to webview. – Mehman Bashirov Feb 21 '14 at 07:28
  • android version of my question:http://stackoverflow.com/questions/10389572/call-java-function-from-javascript-over-android-webview – Mehman Bashirov Feb 21 '14 at 09:19