检查 URL 是否是本地文件?
我正在尝试使用以下代码检查 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这将作为确定给定 URL 是来自本地文件系统还是远程加载的更通用的解决方案:-(
这回答了您问题的主题我如何检查 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:-
(Which answers the subject of your question How can I check if URL is local file?)
由于您在应用程序中使用了空格(我建议您不要再这样做),因此您可以使用以下代码来模拟这些
%20
转义字符串: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:您可以使用这些行来搜索字符串:
You can use these lines to search in a string:
您可以使用
fileReferenceUrl
找到You can find using
fileReferenceUrl