【问题标题】:iOS - Webview - Notify Javascript when an NSNotification is receivediOS - Webview - 收到 NSNotification 时通知 Javascript
【发布时间】:2014-03-17 13:08:53
【问题描述】:

我想知道是否有人知道如何做到这一点。我有一堆 NSNotifications,我想在 JavaScript 中创建侦听器,嵌入在 UIWebView 中,当收到 NSNotifications 时会执行。

我知道使用 PhoneGap 和方法 sendPluginResult 可以做到这一点,但我想知道是否有另一种不使用cordova 的方法。

谢谢

【问题讨论】:

    标签: javascript ios iphone uiwebview


    【解决方案1】:

    UIWebView 之外创建您的监听器并将stringByEvaluatingJavaScriptFromString 消息发送到您的网络视图。

    - (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];
    }
    

    【讨论】:

      【解决方案2】:

      在回调 NSNotification 方法中调用这个:

      [yourwebview stringByEvaluatingJavaScriptFromString:@"methodName()"];
      

      并在您的 javascript 代码中创建该“方法名”

      【讨论】:

        猜你喜欢
        • 2012-02-18
        • 2019-08-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-08-19
        相关资源
        最近更新 更多