简体   繁体   English

如何从WKWebView加载本地化的html文件?

[英]How to load localized html file from WKWebView?

1st StackOverflow question for me... 我的第一个StackOverflow问题...

!! !! Objective-C !! 目标C!

I'm in the process of updating an old app and am working on help screens. 我正在更新旧的应用程序,并且正在使用帮助屏幕。 I chose to display some useful How-tos through some html mainly for the ease of formatting the text (bullet points, colors, bold, underline etc...). 我选择通过一些html显示一些有用的操作方法,主要是为了简化文本的格式(项目符号,颜色,粗体,下划线等)。 Since the app is a bit complex, The help screens are in a total of 3: A main info webpage that links to 2 other detailed pages. 由于该应用程序有点复杂,因此帮助屏幕共有3个:一个主要信息网页,可链接到其他2个详细页面。

Tha app is localized in English (Base) and French. Tha应用程序已本地化为英语(基础)和法语。

Once the setup was complete, and with everything working fine, I went ahead and localized the base html files. 安装完成后,一切正常,我继续将本地html文件本地化。

Now, they are neatly placed in the Base.lproj and fr.lproj files. 现在,它们被整齐地放置在Base.lproj和fr.lproj文件中。

But when when I tap on a link to another page from the main WebView, nothing happens ! 但是,当我点击从WebView主页面到另一个页面的链接时,什么也没发生!

Any ideas ? 有任何想法吗 ?

Thanks 谢谢

Here are some bits of code I'm using: In the .h file : 这是我正在使用的一些代码:在.h文件中:

@interface howToViewController : UIViewController <WKUIDelegate>  
{  
    WKWebView *infoText;  
    NSString *uRLString;  
}  
@property (nonatomic, retain) WKWebView *infoText;

In the .m file: (had to programmatically create the WKWebView because of the known Xcode 9 bug when creating it through Storyboards) 在.m文件中:(由于通过情节提要板创建XK 9时已知的bug,因此必须以编程方式创建WKWebView)

- (void)viewDidLoad  
{  
    [super viewDidLoad];  
WKWebViewConfiguration *theConfiguration = [[WKWebViewConfiguration alloc] init];
WKWebView *aWebView = [[WKWebView alloc] initWithFrame:self.view.frame  
                                         configuration:theConfiguration];  
aWebView.UIDelegate = self;  
self.infoText = aWebView;  

NSURL *url = [[NSBundle mainBundle] URLForResource:@"infoHTML" withExtension:@"html"];  
NSString *html = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];  
NSURL *baseUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];  

[infoText loadHTMLString:html baseURL:baseUrl];  

[self.view addSubview:infoText];  
}  

In the html file, the links that no longer work once the files are localized: 在html文件中,一旦文件本地化,链接将不再起作用:

<p>3/ More information is available for the respective tabs by pressing the following links :</p>  
    <ul><li><a href="pageOne.html">Limitations</a>  
    </li></ul>  
    <ul><li><a href="pageTwo.html">Fueling</a>  
    </li></ul>  

Working code from one of my apps. 来自我的一个应用程序的工作代码。 I'm using NSURLRequest and WKWebView's loadRequest instance method. 我正在使用NSURLRequest和WKWebView的loadRequest实例方法。 I'm not sure what's going wrong with your use of loadHTMLString:baseURL: but I think it may have something to do with the value you're passing for the baseURL argument. 我不确定您使用loadHTMLString:baseURL怎么了:但是我认为这可能与您为baseURL参数传递的值有关。 Have you cleared the console, then single-stepped over that line of code to see if you're getting any message in the console? 您是否已清除控制台,然后单步执行该行代码以查看控制台中是否收到任何消息?

NSURL *url = [[NSBundle mainBundle] URLForResource: pageString withExtension: @"html"]; 
NSURLRequest *request = [NSURLRequest requestWithURL: url];
[webView loadRequest: request];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM