无需用户交互即可有效自动刷新 UITableview (IOS 5)



我从未使用推送通知编写过应用程序,我有一个非常普遍的问题。我想自动刷新 UITableview 的数据(来自 Web 服务),而无需"拉取刷新"或任何其他用户的交互。是否可以使用推送通知通知我的应用程序数据更改,以避免每xx秒轮询一次浪费带宽和电池?

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

有没有其他方法可以达到相同的结果?

我将使用 IOS 5.0+

提前致谢

你可以创建一个服务器HTTP-Request,它只在有变化时才返回一些东西。然后,如果此响应不为空,则可以重新加载数据。可以通过 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
    }
}

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

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

苹果公司对此有很好的记录。

最新更新