How to perform javascript only on certain URLS in a UIWebView
In my webViewDidFinishLoad method inside the UIWebViewController I have been trying to separate javascript calls so that they will only be performed on certain URLS. So far I have been trying a simple if statement that takes the current URL and tries to match the two strings. This is however not allowing the if statement to run despite the urls matching. Any help would be appreciated.
NSString *currentURL = homePage.request.URL.absoluteString;
if (currentURL == @"www.google.com"){
NSLog(@"hi");
[webView stringByEvaluatingJavaScriptFromString:injectedEmail];
[webView stringByEvaluatingJavaScriptFromString:injectedPassword];
[开发者_如何学PythonhomePage stringByEvaluatingJavaScriptFromString:@"var field = document.getElementById('loginSubmit');"
"document.forms[0].submit();"];
}
You should take a look at NSURL Class Reference, which will tell you that -(NSString *)absoluteString
returns an RFC 1808 absolute URL. These animals look like http://www.google.com/search?q=NSURL
, and don't have just a host name.
Also, when you're comparing strings in Objective-C, using ==
will compare the string pointers, not the strings. You should be using something like:
if ([currentURL isEqualToString:@"http://www.google.com"]) {
// do stuff
}
But you may not care about the complete absoluteURL
, in which case I would suggest:
if ([currentURL hasPrefix:@"http://www.google.com"]) {
// do stuff
}
精彩评论