2

I want to call the following javascript code from my Objective - C code. I have found many answers like How to call JavaScript Function in objective C, but none of them met my requirement. Following is my code

var s=document.getElementsByTagName('script')[0];
var sc=document.createElement('script');
sc.type='text/javascript';
sc.async=true;
sc.src='https://api.zaakpay.com/zaakpay.js?random=' + Math.random();
s.parentNode.insertBefore(sc,s);


function encryptField(cardvalue) {
        var out = "";
        for ( var i = 0; i < cardvalue.length; i++) {
                out += (cardvalue.charAt(i).charCodeAt() + key.charAt(i % key.length)
                                .charCodeAt())
                                + ",";
        }
        return out;
}


function prepareCall(){

   var paymenttype = document.getElementById("paymenttype").value;
   if(paymenttype=='CC' || paymenttype=='DC' || paymenttype=='SC'){
      document.getElementById("cvv").value = encryptField(document.getElementById("cvv").value);
   }
   if(paymenttype=='CC' || paymenttype=='DC' ){
          document.getElementById("ccnumber").value = encryptField(document.getElementById("ccnumber").value);
          document.getElementById("expmonth").value = encryptField(document.getElementById("expmonth").value);
          document.getElementById("expyear").value = encryptField(document.getElementById("expyear").value);
   }
}

Can I call the above code directly from my ios code without loading a HTML page? If I need a HTML page to load the script then how I can call the methods as soon as web view is loaded? Also I need to pass some parameters in the prepareCall() method? How I can achieve this?

1
  • 1
    You need to learn basic javascript to do it. Currently it is a too broad question Commented May 16, 2016 at 6:08

2 Answers 2

2

You can load your js using

[_webView loadHTMLString:@"<script src=\"your.js\"></script>" baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] resourcePath]]];

and then in webViewDidFinishLoad you can call function and pass parameters as follows

NSString *javascriptString = [NSString stringWithFormat:@"yourFunction('%@','%@')",firstVar,secondVar];
[_webView stringByEvaluatingJavaScriptFromString:javascriptString];

Hope it Helps :)

Sign up to request clarification or add additional context in comments.

Comments

0

The question you linked is actually pretty much all you need. You can load a UIWebView from a string (see iOS Load UIWebView From String), so you could literally create a string with the code you have above and as long as you put it in <script> tags it would execute when the UIWebView was loaded.

i.e. (the HTML is maybe too minimal to run, this is really just pseudocode

NSString *foo = @"<html><head><script>"
                // Insert your script here
                "</script></head><body></body></html>";

However, you really would be better off putting your globally scoped code in a function and calling that as the body's onload handler.

There is however a bunch of other things that you may need to do ... unless all you want to do is display the result as a web page.

P.S.

[webView loadHTMLString:foo baseURL:nil];

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.