Tag: uibackgroundtask

快速停止后台任务

我用这种方式build立了我的应用程序,后台任务的pipe理: var updateTimer: NSTimer? var backgroundTask: UIBackgroundTaskIdentifier = UIBackgroundTaskInvalid override func viewDidLoad() { super.viewDidLoad() // listen for UIApplicationDidEnterBackgroundNotification NSNotificationCenter.defaultCenter().addObserver(self, selector: "appDidEnterBackground:", name: UIApplicationDidEnterBackgroundNotification, object: nil) } func reinstateBackgroundTask() { if updateTimer != nil && (backgroundTask == UIBackgroundTaskInvalid) { registerBackgroundTask() } } func registerBackgroundTask() { backgroundTask = UIApplication.sharedApplication().beginBackgroundTaskWithExpirationHandler { [unowned self] in self.endBackgroundTask() } assert(backgroundTask != […]

只保留一个后台任务

我正在开发一个应用程序,使用后台任务来保持每20秒跟踪用户位置。 一切都很好,只是当我在后台input应用程序时,会创build一个新的后台任务,这样我可以在最后的多个后台任务中运行。 我试着添加[[UIApplication sharedApplication] endBackgroundTask:bgTask]; 在applicationWillEnterForeground ,但是什么都不做。 关键是我想在进入应用程序时使所有正在运行的后台任务无效化,并在后台input时创build一个新的后台任务,或者只保留一个后台任务。 – (void)applicationDidEnterBackground:(UIApplication *)application { [self runBackgroundTask:10]; } -(void)runBackgroundTask: (int) time{ //check if application is in background mode if ([UIApplication sharedApplication].applicationState == UIApplicationStateBackground) { __block UIBackgroundTaskIdentifier bgTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{ [[UIApplication sharedApplication] endBackgroundTask:bgTask]; bgTask = UIBackgroundTaskInvalid; }]; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{ NSTimer* t = [NSTimer scheduledTimerWithTimeInterval:time target:self selector:@selector(startTracking) […]

几秒钟后,ios7中的操作系统杀死了应用程序

我正在为iOS 7创build基于导航的应用程序,因为我正在使用用户位置数据,使用CoreLocation框架, 应用程序的要求是在特定的时间开始获取用户在后台的位置,为此,我已经使用didReceiveRemoteNotification fetchCompletionHandler:方法实现了无声Pushnotification, 我已经成功地实现了这个使用无声Pushnotification及其调用startUpdatingLocation ,我能够获取委托方法中的位置数据: 使用这个有效载荷: {"aps" : {"content-available" : 1},"SilentPush" : "4"} 我已启用背景模式的location和remote notification : – (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))handler { __block UIBackgroundTaskIdentifier bgTask =0; UIApplication *app = [UIApplication sharedApplication]; bgTask = [app beginBackgroundTaskWithExpirationHandler:^{ [self.locationManager startUpdatingLocation]; }]; didUpdateLocations – (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations { lastLoc=[locations lastObject]; [logFile addLocObject:[NSString stringWithFormat:@"Loc: %@",lastLoc]]; […]

XTubeManager在后台崩溃?

我在XTubeManager中遇到大量的崩溃(似乎是CFNetwork内部)。 不幸的是,控制台日志不可用,只有调用堆栈(见下文)。 问题: 我可以想象,我的应用程序在后台崩溃,因此没有控制台日志被写入,你认为这是一种可能性? 我必须以不同的方式处理backgroundTask过期,例如取消所有的NSURLRequests ? (见下面的代码) 背景 我经常在后台醒来(或通过后台推送)并运行如下所示的后台任务: NSString *myTaskName = @"some.random.task.name"; __block UIBackgroundTaskIdentifier taskID = [UIApplication.sharedApplication beginBackgroundTaskWithName:myTaskName expirationHandler:^{ [UIApplication.sharedApplication endBackgroundTask:taskID]; taskID = UIBackgroundTaskInvalid; }]; dispatch_queue_t q = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); dispatch_async(q, // doing some NSURLRequests stuff here [UIApplication.sharedApplication endBackgroundTask:taskID]; taskID = UIBackgroundTaskInvalid; }); 这通常在applicationDidEnterBackground调用 调用堆栈 Thread : Crashed: com.apple.NSURLConnectionLoader 0 libobjc.A.dylib 0x183599b90 objc_msgSend + […]

通过BackgroundTask在后台维护Multipeer连接会话?

我试图保持一个MultipeerConnectivity“会话”,当应用程序暂时进入后台,所以我想使用一个后台任务,因为我在这里看过几次…问题是我不知道如何“维护”与UIBackgroundTask会议,可以有人请张贴 我不关心广告客户/浏览器,可以阻止它们,但是我希望会话不会因为重新连接超级错误而断开连接。

通过静默推送通知startUpdatingLocationManager

我正在创build需要在特定时间在后台唤醒的应用程序。 我努力了 : UILocalNotification:但我不想使用UILocalNotification,因为它需要用户交互来点击通知,并且只有应用程序会唤醒并启动位置pipe理器。 我也用过[locationManager startUpdatingLocation]; 启用背景模式位置更新,这是工作,但它会占用大量的电池。 因此,使用新的iOS 7functionSilent pushNotification和Silent pushNotification didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:方法我需要在后台启动位置pipe理器, -(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{ NSLog(@"Receive RemoteNotification in fetchCompletionHandler with UserInfo:%@",userInfo); application.applicationIconBadgeNumber=0; __block UIBackgroundTaskIdentifier bgTask = [application beginBackgroundTaskWithExpirationHandler:^{ [self.locationManager startUpdatingLocation]; [application endBackgroundTask:bgTask]; bgTask = UIBackgroundTaskInvalid; }]; } 无声推送通知工作正常, PushNotification的有效载荷: {"aps" : {"content-available" : 1},"SilentPushId" : "07"} 但这不会启动位置经理,请有人帮助我。 编辑: 如果这是不可能的,请给我一些build议。

iOS 9:在timout之前,beginBackgroundTaskWithExpirationHandler被调用

我正在进行VOIP呼叫,并添加了对iOS <10的支持。对于应用程序处于后台的传入VOIP呼叫,我正在使用UILocalNotification(在iOS 10中不推荐使用)。 要拨打电话60秒(或1分钟),我使用此代码 count = 0; apnTimer = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(showIncomingCall:) userInfo:userInfo repeats:YES]; self.backgroundTask = [application beginBackgroundTaskWithExpirationHandler:^{ NSLog(@"ALVOIP : BACKGROUND_HANDLER_NO_MORE_TASK_RUNNING."); [application endBackgroundTask:self.backgroundTask]; self.backgroundTask = UIBackgroundTaskInvalid; }]; -(void)showIncomingCall:(NSTimer *)timer { if (count < 60) { UIApplication *application = [UIApplication sharedApplication]; [application presentLocalNotificationNow:localNotification]; NSLog(@"Time Remaining: %f", [[UIApplication sharedApplication] backgroundTimeRemaining]); count = count + 3; return; […]