![](/img/trans.png)
[英]Postgres: No payload received when using pg_notify with `node-postgres` Javascript library
[英]iOS - Webview - Notify Javascript when an NSNotification is received
我想知道是否有人知道該怎么做。 我有一堆NSNotifications,我想用Javascript創建監聽器,該監聽器嵌入UIWebView中,當接收到NSNotifications時將執行該監聽器。
我知道可以使用PhoneGap和方法sendPluginResult,但是我想知道是否有另一種無需科爾多瓦的方法。
謝謝
在回調NSNotification方法中調用此方法:
[yourwebview stringByEvaluatingJavaScriptFromString:@"methodName()"];
並在您的JavaScript代碼中創建該“方法名”
在UIWebView
之外創建偵聽器,並將stringByEvaluatingJavaScriptFromString
消息發送到您的Web視圖。
- (void)registerObserver
{
NSArray *names = [NSArray arrayWithObjects:
@"FirstNotification", @"SecondNotification", @"ThirdNotification", nil];
for (NSString *name in names)
{
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(notificationReceived:)
name:@"NotificationName"
object:nil];
}
}
- (void)notificationReceived:(NSNotification *notification)
{
NSString *js = [NSString stringWithFormat:
@"notificationReceived('%@');", notification.name];
[self.webView stringByEvaluatingJavaScriptFromString:js];
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.