【问题标题】:Effective automatic refresh of UITableview without user interaction (IOS 5)无需用户交互的UITableview的有效自动刷新(IOS 5)
【发布时间】:2012-04-22 17:12:08
【问题描述】:

我从未使用推送通知编写过应用程序,我有一个非常笼统的问题。 我想在没有“拉刷新”或任何其他用户交互的情况下自动刷新 UITableview 的数据(来自网络服务)。是否可以使用推送通知来通知我的应用程序发生数据更改,以避免每 xx 秒轮询一次而浪费带宽和电池?

换句话说,当我的应用程序在前台运行时(也许)我可以处理消息并且用户不会收到弹出窗口,但否则会发生什么?

还有其他方法可以达到同样的效果吗?

我打算用IOS 5.0+

提前致谢

【问题讨论】:

    标签: xcode ios5 push-notification


    【解决方案1】:

    您可以创建一个服务器 HTTP 请求,该请求仅在发生更改时才返回。然后,如果此响应不为空,您可以重新加载数据。您可以通过 NSTimer 调用的函数。

    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(get) userInfo:nil repeats:YES];
    

    和

    -(void)get {
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://example.com/app.php"]];
        [[NSURLConnection alloc] initWithRequest:request delegate:self];
    }
    - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
        NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        if(![responseString isEqualToString:@""]) {
        // reload
        }
    }
    

    【讨论】:

    • 或将其与 didReceiveLocalNotification 结合使用(上面由 Stephen Darlington 回答)
    • 谢谢!这很有帮助。不幸的是,我无法投票,因为我没有 15 点声望点!
    • 你说你想避免轮询!仅仅因为正文中没有数据并不意味着无线电已关闭或没有数据传输。
    【解决方案2】:

    当您的应用程序运行时,您可以注册以接收推送通知。然后只需实现该方法,使其更新您的 tableview。

    - (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notif {
    

    还不错documented by Apple。

    【讨论】:

      猜你喜欢
      • 2014-02-03
      • 2021-09-17
      • 2012-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-01
      • 2012-11-29
      • 1970-01-01
      相关资源
      最近更新 更多