I'm catching an url in shouldStartLoadWithRequest.
How may I know if it's one of my开发者_JS百科 project resources that is trying to be opened (for example with [self.webView loadHTMLString:htmlContentFinal baseURL:[NSURL fileURLWithPath:bundlePath]];
), or something else ?
You can check if URL string starts with 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.
You can implement -textFieldDidBeginEditing
or -textFieldDidEndEditing
and set some boolean properties to 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];
}
Check the [URL schema]
. If it's file
, then it's a local file. It won't be necessarily a bundle resource, though - could be a file in your app's Documents, for example.
To make sure it's in the bundle, match the beginning of the path with the bundle's path, available from the [NSBundle mainBundle]. Although I don't see why.
精彩评论