Reputation: 4178
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 :)
Upvotes: 1
Views: 2467
Reputation: 2440
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
}
Upvotes: 2