你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> iOS解決應用進入後台後計時器和位置更新停止的問題

iOS解決應用進入後台後計時器和位置更新停止的問題

編輯:IOS開發綜合

在這裡將為大家介紹的是IOS解決應用進入後台後計時器和位置更新停止的問題的說明,具體操作請看面的內容

由於IOS系統為“偽後台”運行模式,當按下HOME鍵時,如程序不做任何操作,應用會有5秒的執行緩沖時間,隨機程序被掛起,所有任務終端,包括計時器和位置更新等操作,但程序打開後台模式開關後,部分任務可以再後台執行,如音頻,定位,藍牙,下載,VOIP,即便如此,程序的後台運行最多可以延長594秒(大概是10分鐘)。不幸的是,程序在聲明後台模式後很有可能在app上架時被拒。基於此,我研究出了不用申明後台模式就能讓計時器和定位在app進入前台時繼續運行的方法。

實現原理如下:

利用IOS的通知機制,在程序進入後台和再次回到前台時發送通知,並記錄進入後台的當前時間和再次回到前台的當前時間,算出兩者的時間間隔,在程序任何需要的地方添加通知監聽者,在監聽方法中執行代碼塊,代碼塊內參數為通知對象和計算出的時間間隔。以計時器為例,程序再進入後台後,計時器停止運行,此時運用上述方法,在程序再次回到前台時執行代碼塊中內容,將程序進入後台時計時器的當前時間間隔加上代碼塊的時間間隔參數就能使計時器准確無誤地計時。廢話不多說,上代碼:

在AppDelegate.m實現文件中:

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    [[NSNotificationCenter defaultCenter]postNotificationName:UIApplicationDidEnterBackgroundNotification object:nil];
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
    [[NSNotificationCenter defaultCenter]postNotificationName:UIApplicationWillEnterForegroundNotification object:nil];
}

代碼說明:程序進入後台後,利用系統通知機制通知程序進入後台和再次回到前台,監聽對象為所有對象。

之後定義一個處理程序進入後台的類YTHandlerEnterBackground

//
//  YTHandlerEnterBackground.h
//  分時租賃
//
//  Created by 柯其譜 on 17/2/24.
//  Copyright © 2017年 柯其譜. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

/** 進入後台block typedef */
typedef void(^YTHandlerEnterBackgroundBlock)(NSNotification * _Nonnull note, NSTimeInterval stayBackgroundTime);

/** 處理進入後台並計算留在後台時間間隔類 */
@interface YTHandlerEnterBackground : NSObject

/** 添加觀察者並處理後台 */
+ (void)addObserverUsingBlock:(nullable YTHandlerEnterBackgroundBlock)block;
/** 移除後台觀察者 */
+ (void)removeNotificationObserver:(nullable id)observer;

@end

在YTHandlerEnterBackground.m實現文件中:

//
//  YTHandlerEnterBackground.m
//  分時租賃
//
//  Created by 柯其譜 on 17/2/24.
//  Copyright © 2017年 柯其譜. All rights reserved.
//

#import "YTHandlerEnterBackground.h"

@implementation YTHandlerEnterBackground

+ (void)addObserverUsingBlock:(YTHandlerEnterBackgroundBlock)block {
    __block CFAbsoluteTime enterBackgroundTime;
    [[NSNotificationCenter defaultCenter]addObserverForName:UIApplicationDidEnterBackgroundNotification object:nil queue:nil usingBlock:^(NSNotification * _Nonnull note) {
        if (![note.object isKindOfClass:[UIApplication class]]) {
            enterBackgroundTime = CFAbsoluteTimeGetCurrent();
        }
    }];
    __block CFAbsoluteTime enterForegroundTime;
    [[NSNotificationCenter defaultCenter]addObserverForName:UIApplicationWillEnterForegroundNotification object:nil queue:nil usingBlock:^(NSNotification * _Nonnull note) {
        if (![note.object isKindOfClass:[UIApplication class]]) {
            enterForegroundTime = CFAbsoluteTimeGetCurrent();
            CFAbsoluteTime timeInterval = enterForegroundTime-enterBackgroundTime;
            block? block(note, timeInterval): nil;
        }
    }];
}

+ (void)removeNotificationObserver:(id)observer {
    if (!observer) {
        return;
    }
    [[NSNotificationCenter defaultCenter]removeObserver:observer name:UIApplicationDidEnterBackgroundNotification object:nil];
    [[NSNotificationCenter defaultCenter]removeObserver:observer name:UIApplicationWillEnterForegroundNotification object:nil];
}

@end

該類實現了用來添加通知監聽者並處理後台和移除通知監聽者的方法,需要注意的是,在addObserverUsingBlock方法中,必須有if (![note.object isKindOfClass:[UIApplication class]])的判斷,否則addObserverForName方法中的代碼塊會執行多次,此代碼執行了兩次。addObserverUsingBlock方法是在viewWillAppear方法中調用添加通知監聽者,在viewWillDisappear方法中調用移除通知監聽者。

例如,在使用了計時器NSTimer控制器中:

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [YTHandlerEnterBackground addObserverUsingBlock:^(NSNotification * _Nonnull note, NSTimeInterval stayBackgroundTime) {
        self.rentTimerInterval = self.rentTimerInterval-stayBackgroundTime;
    }];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.timer invalidate];
    [YTHandlerEnterBackground removeNotificationObserver:self];
}

我定義了一個倒計時5分鐘的計時器對象timer屬性,並定義了一個計時器當前倒計時時間間隔rentTimerInterval屬性,在添加通知監聽者代碼塊中,rentTimerInterval等於進入後台時的倒計時時間間隔減去程序停留在後台的時間間隔,當計時器再次回到前台時,計時器此時的時間間隔是持續的。雖然計時器並未在後台持續運行,但是使用了此方法,同樣實現了計時器的正確即時。

同樣的,當程序存在位置更新功能時,當程序進入後台,位置服務對象會自動停止更新,此時的作法依然是調用上述兩個處理進入後台的方法,使得程序進入後台後,再次開始定位:

在需要位置更新的類中:

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.locService.delegate = self;
    [self.locService startUserLocationService];
    //進入後台再進入前台重新開始定位
    [YTHandlerEnterBackground addObserverUsingBlock:^(NSNotification * _Nonnull note, NSTimeInterval stayBackgroundTime) {
        [self.locService startUserLocationService];
    }];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    //停止定位
    self.locService.delegate = nil;
    [self.locService stopUserLocationService];
    //移除後台監聽
    [YTHandlerEnterBackground removeNotificationObserver:self];
}

此處使用的是百度地圖SDK

利用這種方法,像是計時器和位置更新等需要在後台運行的任務都可以實現相應的需求,只是麻煩的是,在任何需要的類中都要調用這兩種方法,你可以根據自己的需求,在程序進入後台和再次回到前台時添加別的參數(通知對象參數是必須的),例如保存進入後台前的操作等等。或是定義不同的添加通知監聽者的方法以實現不同的需求。

通本學習您是不是更了解ios開發了呢.感謝關注本站

【iOS解決應用進入後台後計時器和位置更新停止的問題】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved