【问题标题】:iOS How to refresh UIWebView at didReceiveRemoteNotificationiOS 如何在 didReceiveRemoteNotification 刷新 UIWebView
【发布时间】:2014-11-14 16:49:35
【问题描述】:

有什么方法可以在新通知到达时刷新或重新加载 UIWebView ?

我一直在尝试寻找答案,但到目前为止一无所获。

知道的人请帮帮我。

谢谢

我尝试了以下但没有...

在 AppDelegate.m

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{


    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(reloadWebView:)
                                                 name:@"PUSH_NOTIFICATION" object:nil];
}

并将下面的代码放在 MainViewController.m

-(void)receivedNotification:(NSNotification*) notification
{
[_webView reload];
}

-(void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}

我是新手……

【问题讨论】:

    标签: ios uiwebview refresh apple-push-notifications


    【解决方案1】:

    要刷新 UIWebView,可以在实例上调用 reload。但是,我认为您知道这一点,并且需要一种在通知到达时将其称为的方法。我建议在这里使用广播机制: - 当远程通知到达时发送它 - 在拥有 Web 视图的视图控制器中注册广播处理程序并在注册的选择器中刷新 Web 视图

    您可以在许多教程中找到如何使用广播的信息,例如这个http://hayageek.com/nsnotification-nsnotificationcenter-tutorial/

    【讨论】:

      【解决方案2】:

      我解决了……

      我的方案是在新的推送通知到达时刷新 webview。

      在 AppDelegate.m 中

      - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
      {
      
          [[NSNotificationCenter defaultCenter] postNotificationName:@"reloadWebView" object:nil];
      }
      

      在 ViewController.m 中

      - (void)viewDidLoad
      {
      [[NSNotificationCenter defaultCenter] addObserver:self
                                               selector:@selector(reloadWebView:)
                                                   name:@"reloadWebView" object:nil];
      }
      
      -(void)viewDidUnload
      {
      [self unregisterForNotifications];
      }
      
      -(void)reloadWebView:(NSNotification*) notification
      {
      [_webView reload];
      }
      
      -(void)unregisterForNotifications
      {
      [[NSNotificationCenter defaultCenter] removeObserver:self name:@"reloadWebView" object:nil];
      }
      

      效果很好。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-06-18
        • 2015-05-19
        • 2012-11-13
        • 2012-06-07
        • 2011-08-16
        • 2012-08-06
        • 2013-05-28
        相关资源
        最近更新 更多