检查 URL 是否是本地文件?

发布于 2024-12-11 22:56:01 字数 1031 浏览 0 评论 0原文

我正在尝试使用以下代码检查 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 对我来说)每个人的情况都不同,那么我怎样才能进行相同的检查而不需要长代码呢?

I am trying to check the current URL in a UIWebView with the following code:

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

    }
}

But the code in the local URL (652BAC0C-DEAE-4695-9195-BE617B9D5106 for me) isn't going to be the same for everybody, so how can I do the same check but without the long code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

小梨窩很甜 2024-12-18 22:56:01

这将作为确定给定 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 是否是本地文件?)

This would work as a more general solution of determining whether a given URL is from the local file system or is being loaded remotely:-

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...
}

(Which answers the subject of your question How can I check if URL is local file?)

岁月苍老的讽刺 2024-12-18 22:56:01
NSString *loginpage = [[NSBundle mainBundle] pathForResource:@"login" ofType:@"html"];

由于您在应用程序中使用了空格(我建议您不要再这样做),因此您可以使用以下代码来模拟这些 %20 转义字符串:

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

Since you're using spaces in your app's (which I recommend you not to do anymore), you can use the following code to simulate those %20 escape strings:

NSString *loginpage = [[[NSBundle mainBundle] pathForResource:@"login" ofType:@"html"] stringByReplacingOccurrencesOfString:@"%20" withString:@" "];
卖梦商人 2024-12-18 22:56:01

您可以使用这些行来搜索字符串:

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

    // Do your stuff here if found

}

You can use these lines to search in a string:

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

    // Do your stuff here if found

}
享受孤独 2024-12-18 22:56:01

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

You can find using 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]];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文