gpt4 book ai didi

iPhone - 我如何知道 URL 是否是资源文件?

转载 作者:行者123 更新时间:2023-12-03 20:46:25 25 4
gpt4 key购买 nike

我在 shouldStartLoadWithRequest 中捕获了一个 url。

我如何知道它是否是我正在尝试打开的项目资源之一(例如使用 [self.webView loadHTMLString:htmlContentFinal baseURL:[NSURL fileURLWithPath:bundlePath]];),或者其他什么?

最佳答案

您可以检查 URL 字符串是否以 file:// 开头:

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
if (request.URL.isFileURL) {
// do some stuff
}
}
<小时/>

How may I ensure that I am the one who loaded that file? The user could wrote that url in the url field himself.

您可以实现 -textFieldDidBeginEditing-textFieldDidEndEditing 并将一些 bool 属性设置为 YES:

- (void)textFieldDidEndEditing:(UITextField *)textField {
if ([[textField.text substringToIndex:7] isEqualToString:@"file://"]) {
self.fileUrlEnteredManually = YES;
} else {
self.fileUrlEnteredManually = NO;
}
}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
if (request.URL.isFileURL) {
if (self.fileUrlEnteredManually) {
// user entered "file://" manually
} else {
// user didn't
}
}
}

- (void)viewDidload {

// ...

fileUrlEnteredManually = NO;
[webView loadRequest:yourLocalRequest];

}

关于iPhone - 我如何知道 URL 是否是资源文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5885913/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com