簡體   English   中英

iOS-Webview-收到NSNotification時通知Javascript

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM