我正在尝试使用以下代码检查UIWebView
中的当前网址:
- (void)webViewDidFinishLoad:(UIWebView *)webView {
NSString *currentURL = webView.request.URL.absoluteString;
NSLog(@"Current URL: %@", currentURL);
NSString *loginpage = @"file:///var/mobile/Applications/652BAC0C-DEAE-4695-9195-BE617B9D5106/School%20VLE%20Business.app/login.html";
if([currentURL isEqualToString:loginpage])
{
NSLog(@"Inputting user details...");
NSString *result;
result = [webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"$('#usernamefield').val('%@');", username]];
result = [webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"$('#passwordfield').val('%@');", password]];
result = [webView stringByEvaluatingJavaScriptFromString:@"$('#submitbutton').click();"];
NSLog(@"Logged in!");
}
}
但本地网址(652BAC0C-DEAE-4695-9195-BE617B9D5106
对我来说)中的代码对于每个人来说都不一样,所以我怎样才能进行同样的检查但没有长代码?
答案 0 :(得分:9)
这可以作为确定给定URL是来自本地文件系统还是远程加载的更通用的解决方案: -
NSString *str = @"file://var/mobile/Applications/652BAC0C-DEAE-4695-9195-BE617B9D5106/School%20VLE%20Business.app/login.html";
NSURL *url = [NSURL URLWithString:str];
if ([[url scheme] isEqualToString:@"file"]) {
// URL is a (local) file...
}
(回答问题的主题如何检查网址是否为本地文件?)
答案 1 :(得分:3)
NSString *loginpage = [[NSBundle mainBundle] pathForResource:@"login" ofType:@"html"];
由于您在应用中使用了空格(我建议您不要再使用空格),您可以使用以下代码来模拟那些%20
转义字符串:
NSString *loginpage = [[[NSBundle mainBundle] pathForResource:@"login" ofType:@"html"] stringByReplacingOccurrencesOfString:@"%20" withString:@" "];
答案 2 :(得分:2)
您可以使用这些行搜索字符串:
if ([your_complete_string rangeOfString:@"YOUR_SEARCH_STRING"].location != NSNotFound){
// Do your stuff here if found
}
答案 3 :(得分:-1)
您可以使用fileReferenceUrl
//if local file
NSURL *url = [NSURL fileURLWithPath:songsList[index]];
if(url.fileReferenceURL != nil){
//is local URL
}else{
//Is live
url = [NSURL URLWithString:songsList[index]];
}