为什么我的ios应用程序在从xcode运行时才会继续执行后台任务 [英] Why does it seem that my ios app only continues background task when running from xcode

查看:204
本文介绍了为什么我的ios应用程序在从xcode运行时才会继续执行后台任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是设置:

我正在构建一个使用60分钟计时器的iOS应用程序。一旦启动,我希望它通知用户无论手机的状态(睡觉或查看我的应用程序)在60分钟的时间内做某事。

I'm building an iOS app that uses a 60 minute timer. Once started I want it to notify the user regardless of the state of the phone (sleeping or viewing my app) to do something when that 60 mins is up.

要做这个我的本地通知设置在我的定时器方法中在相应的ViewController.m文件中执行:

To do this I have my local notification setup to be executed in my timers method within the appropriate ViewController.m file:

// what happens when the timer ends
    if (secondsCount == 0) {
        [countdownTimer invalidate];// stops the countdown
        countdownTimer = nil;
        hourCountDown.text = [NSString stringWithFormat:@"60:00"];// sets the timer to 60 mins

        // shows and hides correct UI elements
        nowBtn.hidden = YES;
        countDown.hidden = YES;
        countDown.hidden = YES;
        startBtn.hidden = NO;

        // shows notification that it's time to do the task
        UILocalNotification *itsTime = [[UILocalNotification alloc] init];

        itsTime.fireDate = [NSDate dateWithTimeIntervalSinceNow:secondsCount];
        itsTime.alertBody = @"Time to do it";

        itsTime.timeZone = [NSTimeZone defaultTimeZone];
        [[UIApplication sharedApplication] scheduleLocalNotification:itsTime];
        itsTime.soundName = UILocalNotificationDefaultSoundName;

        // sets the page label
        labelState.text = [NSString stringWithFormat:@"Do it!"];

    }

在AppDelegate.m文件中,我有以下代码:

In the AppDelegate.m file I have the following code:

- (void)applicationDidEnterBackground:(UIApplication *)application

{
//允许计时器在应用程序未启动时在后台运行

{ // allows timer to work in background when app isn't up

UIBackgroundTaskIdentifier bgTask =0;
UIApplication  *app = [UIApplication sharedApplication];
bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
    [app endBackgroundTask:bgTask];
}]; 

}

我注意到了什么它似乎只显示我的手机插入我的MBP并通过Xcode的模拟器运行应用程序时的通知。我按下我的应用程序上的右键,等待一个小时,然后显示通知。如果我拔掉手机,运行我的应用程序并按下相同的按钮,等待一小时......没有显示任何通知。

What I've noticed is it seems to only show the notification when my phone is plugged into my MBP and is running the app via Xcode's simulator. I push the right buttons on my app, wait an hour and the notification is shown. If I unplug the phone, run my app and push the same buttons, wait an hour... no notification is shown.

我的问题是为什么?这是因为我正在使用通过Xcode模拟器添加到我的设备的应用程序,而不是我像真正的应用程序一样安装的存档文件?如果是这样,我该怎么办?

My question is why? Is this because I'm using the app that was added to my device via the Xcode simulator and not an archive file that I installed like a real app? And if so, what the heck can I do about?

谢谢!

推荐答案

在模拟器中运行时,允许后台任务无限时间。连接手机后,您可以在手机上运行。在手机上,操作系统可以让您短时间运行,当时间结束时,您的应用程序会进入睡眠状态。在iOS 7上,您在后台获得30秒。在iOS 6及更低版本中,10分钟。也不会给你你想要的东西。计算并注册将来的本地通知,或使用推送通知。

When running in the simulator, background tasks are allowed infinite time. When your phone is connected, you run on the phone. On your phone, the OS gives you a short time to run and when the time ends, your app goes to sleep. On iOS 7, you are given 30 seconds in the background. In iOS 6 and below, 10 minutes. Neither will give you what you want. Either calculate and register the local notification in the future, or use push notifications.

这篇关于为什么我的ios应用程序在从xcode运行时才会继续执行后台任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆