Фоновая работа на iOS9

Я создал тест push-уведомлений. Я создал код, который, когда приложение переходит в фоновый режим, каждые 10 секунд приложение показывает локальное push-уведомление. Код работает хорошо через несколько минут. Через несколько минут работа над фоном прекращается. Я думаю, что через какое-то время iOS убила работу, работающую в фоновом режиме. Это? Есть ли способ, которым iOS не останавливает мою работу в фоновом режиме?

Это мой код: ViewController:

import UIKit

class ViewController: UIViewController {

var timer = NSTimer()
 var backgroundTaskIdentifier: UIBackgroundTaskIdentifier?

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    notificatioAppGoToBackground()

    setupNotificationSettings()

    //call handle notifications
    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ViewController.handleModifyListNotification), name: "modifyListNotification", object: nil)
    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ViewController.handleDeleteListNotification), name: "deleteListNotification", object: nil)

    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ViewController.handleStopTimerNotification), name: "stopTimer", object: nil)


    backgroundTaskIdentifier = UIApplication.sharedApplication().beginBackgroundTaskWithExpirationHandler({
        UIApplication.sharedApplication().endBackgroundTask(self.backgroundTaskIdentifier!)
    })


}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func alert(title:String, message:String ){

    let alert = UIAlertController(title: title, message: message, preferredStyle: .Alert)
    alert.addAction(UIAlertAction(title: "OK", style: .Default) { _ in })
    self.presentViewController(alert, animated: true){}
}

func handleStopTimerNotification(){
    timer.invalidate()
    print("acao foreground")
    UIApplication.sharedApplication().cancelAllLocalNotifications() //cancela a notificacao

}

func handleModifyListNotification() {
    timer.invalidate()
    print("acao editar")
    //alert("Atenção", message: "Modificação ativada, notificação cancelada")
    UIApplication.sharedApplication().cancelAllLocalNotifications() //cancela a notificacao

}

func handleDeleteListNotification() {
    print("executou a exclusao")
}

func setupNotificationSettings() {

    let notificationSettings: UIUserNotificationSettings! = UIApplication.sharedApplication().currentUserNotificationSettings()

    if (notificationSettings.types == UIUserNotificationType.None){

        let notificationTypes: UIUserNotificationType = [.Alert, .Sound, .Badge]

        let modifyListAction = UIMutableUserNotificationAction()
        modifyListAction.identifier = "editList"
        modifyListAction.title = "Edit list"
        modifyListAction.activationMode = UIUserNotificationActivationMode.Foreground
        modifyListAction.destructive = false
        modifyListAction.authenticationRequired = true

        let trashAction = UIMutableUserNotificationAction()
        trashAction.identifier = "trashAction"
        trashAction.title = "Delete list"
        trashAction.activationMode = UIUserNotificationActivationMode.Background
        trashAction.destructive = true
        trashAction.authenticationRequired = true

        let actionsArray = NSArray(objects: modifyListAction, trashAction)
        let actionsArrayMinimal = NSArray(objects: trashAction, modifyListAction)

        // Specify the category related to the above actions.
        let shoppingListReminderCategory = UIMutableUserNotificationCategory()
        shoppingListReminderCategory.identifier = "shoppingListReminderCategory"
        shoppingListReminderCategory.setActions(actionsArray as? [UIUserNotificationAction], forContext: UIUserNotificationActionContext.Default)
        shoppingListReminderCategory.setActions(actionsArrayMinimal as? [UIUserNotificationAction], forContext: UIUserNotificationActionContext.Minimal)


        let categoriesForSettings = NSSet(objects: shoppingListReminderCategory)


        // Register the notification settings.
        let newNotificationSettings = UIUserNotificationSettings(forTypes: notificationTypes, categories: categoriesForSettings as? Set<UIUserNotificationCategory>)
        UIApplication.sharedApplication().registerUserNotificationSettings(newNotificationSettings)
    }

}

func setActions(actions: [AnyObject]!, forContext context: UIUserNotificationActionContext){}


func notificatioAppGoToBackground(){
    let notificationCenter = NSNotificationCenter.defaultCenter()
    notificationCenter.addObserver(self, selector: #selector(appMovedToBackground), name: UIApplicationWillResignActiveNotification, object: nil)
}


func appMovedToBackground() {
    print("App moved to background!!!")
    listenerSchedule()
}

func listenerSchedule() {
    timer = NSTimer.scheduledTimerWithTimeInterval(10.0, target: self, selector: #selector(ViewController.startNotication), userInfo: nil, repeats: true)
}

func startNotication(){
    let date = NSDate()
    let dateComponets: NSDateComponents = NSCalendar.currentCalendar().components([.Day, .Month, .Year, .Hour, .Minute, .Second], fromDate: date)

    if(dateComponets.second < 20){
        print("nao notifica  \(dateComponets.second)" )
    }else{
        print("aguardando notificação  \(dateComponets.second)" )
        let localNotification = UILocalNotification()

        localNotification.alertBody = "Teste de notificação"
        localNotification.alertAction = "View List"
        localNotification.category = "shoppingListReminderCategory"
        localNotification.applicationIconBadgeNumber = dateComponets.second
        localNotification.soundName = UILocalNotificationDefaultSoundName


        UIApplication.sharedApplication().scheduleLocalNotification(localNotification)
    }


}


}

AppDelegate:

import UIKit

  @UIApplicationMain
  class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?


func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.
    return true
}

func application(application: UIApplication, didRegisterUserNotificationSettings notificationSettings: UIUserNotificationSettings) {

    print(notificationSettings.types.rawValue)
}

//executa a noficacao agendada com a app aberta
func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) {
    // Do something serious in a real app.
    print("Received Local Notification:")
    print(notification.alertBody!)
    application.applicationIconBadgeNumber = 0 //limpa o badge ao carregar a app
    application.cancelAllLocalNotifications() //limpa o badge ao carregar a app
    NSNotificationCenter.defaultCenter().postNotificationName("stopTimer", object: nil)
}
//Essa funcao exibe a notificacao quando a app esta parada ou em segundo plano
func application(application: UIApplication, handleActionWithIdentifier identifier: String?, forLocalNotification notification: UILocalNotification, completionHandler: () -> Void) {

    if identifier == "editList" {
        NSNotificationCenter.defaultCenter().postNotificationName("modifyListNotification", object: nil)
    }
    else if identifier == "trashAction" {
        NSNotificationCenter.defaultCenter().postNotificationName("deleteListNotification", object: nil)
    }

    completionHandler()
}




func applicationWillResignActive(application: UIApplication) {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

func applicationDidEnterBackground(application: UIApplication) {
    // 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.
}

func applicationWillEnterForeground(application: UIApplication) {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

func applicationDidBecomeActive(application: UIApplication) {
    NSNotificationCenter.defaultCenter().postNotificationName("stopTimer", object: nil)
    application.applicationIconBadgeNumber = 0 //limpa o badge ao carregar a app
    application.cancelAllLocalNotifications() //limpa o badge ao carregar a app
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

func applicationWillTerminate(application: UIApplication) {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}


 }

person Alexandre C. do Carmo    schedule 21.07.2016    source источник
comment
вы включили фоновые режимы   -  person Sunil Sharma    schedule 21.07.2016
comment
Хм, я только создал код. Где я это делаю?   -  person Alexandre C. do Carmo    schedule 21.07.2016
comment
Нажмите на проект в Навигаторе проектов -> Цель проекта -> Возможности -> Прокрутите вниз до Фоновые режимы и включите переключатель. Вы должны включить любую службу, которая лучше всего подходит для вашего приложения, для работы в фоновом режиме. Для получения дополнительной помощи проверьте эту ссылку stackoverflow.com/ вопросы/8943214/   -  person Sunil Sharma    schedule 21.07.2016


Ответы (2)


Если вы хотите, чтобы ваше приложение работало в фоновом режиме и в режиме уничтожения (прекращения), оно будет отправлять локальные уведомления каждые 10 секунд.

С APNS ваше приложение не будет вызываться в фоновом режиме. он будет вызываться только при нажатии на уведомление и вызове didReceiveLocalNotification.

Тогда вам нужно использовать библиотеку push kit. когда полезная нагрузка push-кита поставляется с молчанием, она будет вызывать ваше приложение в фоновом режиме в течение 30 секунд, даже если оно находится в прекращенном режиме.

Таким образом, вы можете запланировать 3 раза на каждые 10 секунд локального уведомления. затем через 30 секунд вы снова должны получить полезную нагрузку push kit.

person Hasya    schedule 22.07.2016

Благодаря помощи. Я читал документацию iOS, и обычно приложения, которые работают в постоянном фоновом режиме, — это музыкальные проигрыватели, например, здоровье. Другие приложения убиваются через несколько минут, iOS делает это, потому что фоновые приложения потребляют заряд батареи и обработку, поэтому только некоторые приложения имеют разрешение на работу в фоновом режиме. Но я могу использовать удаленное уведомление. Я попробую.

person Alexandre C. do Carmo    schedule 22.07.2016