Dir*_*nry 9 html mobile html5 uiwebview ios
我想在iOS应用程序中集成一个完整的HTML框架(即HTML/CSS/JavaScript),并使负责运行HTML内容的UIWebView能够与其余的原生Objective-C源代码进行通信.
方向1:从UI-View内的Objective-C到HTML
使其余源代码向HTML内容发送消息的方法非常简单:我可以调用stringByEvaluatingJavaScriptFromString:Objective-C方面并以正确的方式实现JavaScript方法.
方向2:从UIWebView中的HTML到Objective-C
这是我无法弄清楚的方式.到目前为止,我唯一的想法是让我的应用程序成为本地Web服务器并向其发送HTML请求.但我不知道怎么做,虽然我认为它可能是骨头,因为我相信诸如Things,VLC或1Password之类的应用可能会使用这种功能.
任何想要使这个方向2工作或任何新的观点,以使HTML内容中的事件发送到Objective-C代码是值得欢迎的.
Ric*_*III 19
我使用jQuery和UIWebViewDelegate完成了这个:
JavaScript(jQuery mobile):
$("#bloodType").change(function() {
url = $("#bloodType option:selected").text();
url = "donordialog:bloodTypeChanged(" + url + ")";
window.location = url;
});
Run Code Online (Sandbox Code Playgroud)
因此,生成的URL如下所示: donordialog:bloodTypeChanged(typeAB-)
在我的objc代码中:
-(BOOL) webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSURL *URL = [request URL];
if ([[URL scheme] isEqualToString:@"donordialog"])
{
// now we need to figure out the function part
NSString *functionString = [URL resourceSpecifier];
if ([functionString hasPrefix:@"bloodTypeChanged"])
{
// the blood type has changed, now do something about it.
NSString *parameter = [functionString stringByReplacingOccurrencesOfString:@"bloodTypeChanged" withString:@""];
// remove the '(' and then the ')'
parameter = [parameter stringByReplacingOccurrencesOfString:@"(" withString:@""];
parameter = [parameter stringByReplacingOccurrencesOfString:@")" withString:@""];
// log the paramter, as I don't know what to do with it right now
NSLog(@"%@", parameter);
}
return NO;
}
return YES;
}
Run Code Online (Sandbox Code Playgroud)
这段代码是从我目前正在处理的项目中逐字复制的,并且可以验证这是否有效.