注销应用程序后如何停止后台服务?

时间:2015-08-19 08:42:26

标签: ios objective-c

我创建了iPhone application,其中包含一些用于数据Background目的的local notification抓取服务流程。现在的问题是我已经注销了我的应用程序然后它已成功从主屏幕移到Login屏幕但现在我仍然收到通知,我的意思是仍在运行background services。我想在点击退出按钮后停止我的应用程序中的所有活动。

我在[{1}}方法

中的来源
Logout button event

1 个答案:

答案 0 :(得分:0)

如果NSTimer invalidate注销时[timer invalidate],如果您已经注册了本地通知,那么您可以在注销时取消它们,例如获取所有通知[[UIApplication sharedApplication] cancelAllLocalNotifications]

更新:您发布了来自logouButtonAction的通知,例如[[NSNotificationCenter defaultCenter] postNotificationName:@"ApplciaitonDidLoggedOut" object:nil]; 你的onlogoutbutton方法应该是这样的

- (void)logoutButtonClick:(id)sender
{
    NSLog(@"Logout Clicked");
    Login *loginview = [[Login alloc]init];
    [self.navigationController pushViewController:loginview animated:YES];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"ApplciaitonDidLoggedOut" object:nil];
}

在app delegate中,在didFinishLaunch [[NSNotificationCenter defaultCenter ] addObserver:self selector:@selector(onLogut) name:@"ApplciaitonDidLoggedOut" object:nil];

中添加此通知的观察者

并在appDelegate类中添加方法

- (void) onLogut
{
  //considering you have a timer property in app delegate,if not add one
  [self.timer invalidate];
  // if you want to cancel local notification
  [[UIApplication sharedApplication] cancelAllLocalNotifications];
}
相关问题