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

发布于 2024-11-05 15:58:42 字数 196 浏览 0 评论 0原文

我在 shouldStartLoadWithRequest 中捕获了一个 url。

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

I'm catching an url in shouldStartLoadWithRequest.

How may I know if it's one of my project resources that is trying to be opened (for example with [self.webView loadHTMLString:htmlContentFinal baseURL:[NSURL fileURLWithPath:bundlePath]];), or something else ?

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

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

发布评论

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

评论(2

习ぎ惯性依靠 2024-11-12 15:58:42

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

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

我如何确保我是加载该文件的人?用户可以自己在 url 字段中写入该 url。

您可以实现 -textFieldDidBeginEditing-textFieldDidEndEditing 并将一些布尔属性设置为 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];

}

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];

}
皓月长歌 2024-11-12 15:58:42

检查[URL 架构]。如果它是file,那么它就是一个本地文件。不过,它不一定是捆绑资源 - 例如,它可能是应用程序文档中的文件。

为了确保它位于捆绑包中,请将路径的开头与捆绑包的路径(可从 [NSBundle mainBundle] 获取)相匹配。虽然我不明白为什么。

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.

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