1

OK, I'm making application for iPhone (Still learning, getting to know Objective-C and Xcode) and it's going pretty well. Application that I'm making is already made for Android. There is a part where application is opening "hardcoded" webpage for Android and that's part where I have problem. JavaScript on that webpage has part of the code that looks something like this:

<a onclick="Android.setCategory('US');" class="navigator_item cat_US">US</a>

That's the only thing I'm interested in. I would just like to GET that "onclick" part as a string for example. So, whenever that part is clicked, I want to get that as a string so I can do my own logic after that, call some method or anything. I tried using

shouldStartLoadWithRequest

but I wasn't able to log out anything on click. Am I doing something wrong here? Maybe my lack of javascript knowledge is problem and I don't understand basics, but I think it should be possible just to get that part from onclick quotes to string. That's all I need.

I forgot one very important thing. I CAN NOT TOUCH that javaScript, that is used for Android and that's how Android app works right now. That's why I want to do this "string" thing.

Is there anything I can do with stringByEvaluatingJavaScriptFromString and document.getElementsByClassName JavaScript method?

Cheers peps :)

1 Answer 1

2

If you have the freedom to change the onclick target, you can do something like:

<a onclick="setCategory('US');">US</a>

in your webpage, and then for your UIWebView do something like:

[uiWebView stringByEvaluatingJavaScriptFromString:  @"function setCategory(category) {alert("Category is " + category);};"];

If you have iOS logic you want to execute upon setting the category the technique I use is custom URL schemes. For example, setCategory might be:

setcategory://US/

You would change your JavaScript to

[uiWebView stringByEvaluatingJavaScriptFromString:  @"function setCategory(category) {window.location = 'setcategory://'+category+'/';};"];

and then in your shouldStartLoadWithRequest method:

  NSString* scheme = [[request URL]scheme];
  NSString* host   = [[request URL]host]; // "host" is actually the category

  if ([scheme isEqualToString:@"setcategory"]) {
    // Do so logic to set the category
  }
Sign up to request clarification or add additional context in comments.

4 Comments

I am really sorry, while I was thinking how to explain what I need, I forgot to mention the main problem... I CAN NOT touch that javaScript. It is working with Android like that right now and that's the main problem... That's why I want to get that onclick "content" into string. Just so I can make difference between clicks. That's how I want to check what is clicked.
There are tricks you can do with Javascript's arguments.callee.name property to get the name of the function you are in, but if you cannot touch the JavaScript then that does you no good. It sounds like what you are going to have to do is actually parse the DOM of the page you have loaded in your UIWebView and override the JavaScript, onclick is going to try to run JavaScript, you can't intercept that and then decide to do something else, you're going to have to provide runnable JavaScript in your app.
There's an easier way. Use stringByEvaluatingJavaScriptFromString: to make the WebView create a Javascript object called Android with the appropriate values: window.Android = { setCategory: function() { ... }, ... };
@duskwuff, is there any way that You can write me one simple working example? I don't actually have idea how to write that down.

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.