Objective c 检查URL是否为本地文件?

Objective c 检查URL是否为本地文件?,objective-c,ios,cocoa-touch,uiwebview,Objective C,Ios,Cocoa Touch,Uiwebview,我正在尝试使用以下代码检查UIWebView中的当前URL: - (void)webViewDidFinishLoad:(UIWebView *)webView { NSString *currentURL = webView.request.URL.absoluteString; NSLog(@"Current URL: %@", currentURL); NSString *loginpage = @"file:///var/mobile/Application

我正在尝试使用以下代码检查
UIWebView
中的当前URL:

- (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!");

    }
}
但是本地URL中的代码(
652BAC0C-DEAE-4695-9195-BE617B9D5106
对我来说)对每个人来说都不一样,所以如果没有长代码,我如何进行相同的检查

NSString *loginpage = [[NSBundle mainBundle] pathForResource:@"login" ofType:@"html"];
由于您正在应用程序中使用空格(我建议您不要再这样做),因此可以使用以下代码模拟那些
%20
转义字符串:

NSString *loginpage = [[[NSBundle mainBundle] pathForResource:@"login" ofType:@"html"] stringByReplacingOccurrencesOfString:@"%20" withString:@" "];

您可以使用以下行在字符串中搜索:

if ([your_complete_string rangeOfString:@"YOUR_SEARCH_STRING"].location != NSNotFound){

    // Do your stuff here if found

}

这将是确定给定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...
}

(这回答了您问题的主题我如何检查URL是否为本地文件?

您可以使用
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]];
}

注释掉
NSLog(…)
行,并将以下行放在
NSString*loginpage…
下:
NSLog(@“URL:[%@]:[%@]”,currentURL,loginpage)虽然这不是他真正问的问题,但它确实可以很好地解决他的问题。在给出答案后阅读适当的问题。URL检查是否为零的问题,检查URL是否为本地文件路径。