如何在 WebView 中仅加载 .html 页面并在 iOS 的 Safari 中加载所有 www 页面? [英] How to Load Only .html page in WebView and All www Page load in Safari in iOS?
问题描述
我是 iOS 开发的新手我将我的 HTML 数据加载到 WebView 但有时它只包含 href 链接作为 .html 链接,有时网站链接像 www.google.co.in
所以我只想将 html 数据加载到 Webview 中,并且任何网站都加载到 Safari 中,因为我编写了类似
i am newbie in iOS Development I load my HTML Data Into WebView But Some time it Contain only href link as .html link and some time website link like as www.google.co.in
so i want to load only html data in to Webview and any website are load in to Safari for that i write a code like as
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSString *link = [[request URL] relativeString];
if ([link isEqualToString:@"module1learningobjectives.html"])
{
return NO;
}
else
{
[[UIApplication sharedApplication] openURL:[request URL]];
return YES;
}
return YES;
}
然后它在 web 视图中加载 .html 文件,但站点在 safari 和 Webview 中打开,我只想在 safari 中打开站点,请给我解决方案.
then it load .html file in web view but site are open in safari and Webview both i want only site was open in safari please give me solution for that.
推荐答案
我会这样做:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSLog("URL is %@, and has an extension of %@", request.URL, [request.URL pathExtension]);
if ([[request.URL pathExtension] isEqualToString:@".html"])
return YES;
return NO;
}
这就是你要问的吗?
如果您希望它对非文件使用 Safari,而对文件使用您自己的 WebView,那么试试这个:
If you want it to use Safari for non files and your own WebView for files, then try this:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
if ( ! ([request.URL isFileURL]) ) {
[[UIApplication sharedApplication] openURL:[request URL]];
return NO;
}
return YES;
}
这篇关于如何在 WebView 中仅加载 .html 页面并在 iOS 的 Safari 中加载所有 www 页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!