从UIWebView中的javascript向ObjectiveC发送通知 [英] send a notification from javascript in UIWebView to ObjectiveC
问题描述
我需要知道如何在 UIWebView
中的HTML中使用JavaScript来通知Objective-C发生了什么事情?
I need to know what should be done to use JavaScript in a HTML sitting in UIWebView
to notify Objective-C that something has happened?
更准确地说,我正在用HTML播放一些JavaScript动画,我需要提醒动画结束的Objective-C代码。
To be more exact, I'm playing some JavaScript animation in HTML and I need to alert the Objective-C code that the animation has ended.
推荐答案
似乎没有官方方法可以做到这一点。但是, 标准 解决方法涉及读取和解析传入的URL请求,基本上滚动自己的序列化消息传递协议。消息处理应在 webView:shouldStartLoadWithRequest:navigationType
视图控制器的方法。
There seems to be no official method of doing this. However, the standard workaround involves reading and parsing incoming URL requests, basically rolling your own serialized messaging protocol. The message handling should be done in the webView:shouldStartLoadWithRequest:navigationType
method of your view controller.
注意:有几个免费库( PhoneGap , QuickConnect , JS-to-Cocoa Bridge )包含此功能(另外还有更多功能)。要重新发明轮子(或者知道为什么它是圆的,可以这么说),请继续阅读。
Note: there are several free libraries (PhoneGap, QuickConnect, JS-to-Cocoa Bridge) which wrap this functionality (plus do a whole lot more). To reinvent the wheel (or know why it's round, so to speak), read on.
从JavaScript中,您将通过尝试导航来调用回调到新网址:
From JavaScript, you will invoke the callback by attempting to navigate to a new URL:
// In JavaScript
window.location = 'myapp:myaction:param1:param2'; // etc...
在Objective-C中,实现 UIWebViewDelegate
.h
文件中的协议:
In Objective-C, implement the UIWebViewDelegate
protocol in your .h
file:
// In your header file
@interface MyAppViewController : UIViewController <UIWebViewDelegate> {
...
}
@end
接下来,在 .m
文件中实施该方法:
Next, implement the method in your .m
file:
// In your implementation file
-(BOOL)webView:(UIWebView *)webView2
shouldStartLoadWithRequest:(NSURLRequest *)request
navigationType:(UIWebViewNavigationType)navigationType
{
// Break apart request URL
NSString *requestString = [[request URL] absoluteString];
NSArray *components = [requestString componentsSeparatedByString:@":"];
// Check for your protocol
if ([components count] > 1 &&
[(NSString *)[components objectAtIndex:0] isEqualToString:@"myapp"])
{
// Look for specific actions
if ([(NSString *)[components objectAtIndex:1] isEqualToString:@"myaction"])
{
// Your parameters can be found at
// [components objectAtIndex:n]
// where 'n' is the ordinal position of the colon-delimited parameter
}
// Return 'NO' to prevent navigation
return NO;
}
// Return 'YES', navigate to requested URL as normal
return YES;
}
两个重要说明:
-
上下文:导航到
myapp:无论
在任何其他情况下都会失败(当然) 。如果您正在加载跨平台页面,请记住这一点。
Context: navigating to
myapp:whatever
will (of course) fail under any other context. Keep this in mind if you're loading cross-platform pages.
计时:如果第二个 window.location =
在第一次返回之前进行调用,它将丢失。因此,要么将你的调用混为一谈,要么手动延迟执行,要么实现一个将上述内容与 JS查询Objective-C对象。
Timing: if a second window.location =
call is made before the first returns, it will get 'lost.' So, either lump your calls together, manually delay execution, or implement a queue which combines the above with JS queries into Objective-C objects.
这篇关于从UIWebView中的javascript向ObjectiveC发送通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!