【问题标题】:Preventing iPhone app for inactive state防止 iPhone 应用程序处于非活动状态
【发布时间】:2013-05-08 16:34:48
【问题描述】:

我需要屏幕开/关回调和语音呼叫回调。但是当我的应用程序处于前台时,我会收到回调。但是当我的应用程序在后台时,我无法获得委托回调。当我的应用在后台时,如何获得阻塞或委托回调?

我通读了苹果文档 http://developer.apple.com/library/ios/#documentation/iphone/conceptual/iphoneosprogrammingguide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html

我发现“后台执行和多任务处理”http://developer.apple.com/library/ios/#documentation/iphone/conceptual/iphoneosprogrammingguide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html#//apple_ref/doc/uid/TP40007072-CH4-SW20

但是对于这个问题没有任何帮助。请帮忙。

【问题讨论】:

  • 你在app delegate中试过这两种方法了吗,- (void)applicationWillResignActive:(UIApplication *)application; - (void)applicationDidBecomeActive:(UIApplication *)application;
  • 不幸的是,屏幕开/关和通话开始/结束事件不能直接用于应用程序。您必须使用现有的应用程序事件并创建解决方法。
  • 我也试过后台执行代码。但它会执行代码,直到线程处于活动状态。当控制超出线程时。应用程序没有响应任何事件(如果应用程序处于后台模式)。

标签: ios iphone objective-c cocoa-touch


【解决方案1】:

尝试使用 UILocaleNotification。它可以在系统中注册一个通知,并且可以在指定的时间执行。

    -(void) viewDidLoad{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(enteredBackground:) name:@"didEnterBackground" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(enteredForeground:) name:@"didEnterForeground" object:nil];
}
 - (void) enteredBackground:(NSNotification*)notification{

        self.notification = [[UILocalNotification alloc] init];

//set the notification property

        [[UIApplication sharedApplication] scheduleLocalNotification:self.notification];


    }


 }
    - (void) enteredForeground:(NSNotification*)notification{
                   //do something
 [[UIApplication sharedApplication] cancelLocalNotification:self.notification];
        }
    } 

在 AppDelegate.m 中

    - (void)applicationDidEnterBackground:(UIApplication *)application
{
            [[NSNotificationCenter defaultCenter] postNotificationName:@"didEnterBackground" object:nil];
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    // 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.
    [[NSNotificationCenter defaultCenter] postNotificationName:@"didEnterForeground" object:nil];
}

【讨论】:

  • 当我的应用程序进入后台或前台状态时,它会给我回调。当我的应用程序在后台运行时,它不会给我回调。当我的应用程序已经在后台模式下运行时,我需要委托回调。
【解决方案2】:

您可以在后台模式下运行应用程序 3 分钟,所有代码都会运行尝试

 func registerBackgroundTask() {
            backgroundTask = UIApplication.shared.beginBackgroundTask { [weak self] in
                self?.endBackgroundTask()
            }
            assert(backgroundTask != UIBackgroundTaskInvalid)
        }
        func endBackgroundTask() {
            print("Background task ended.")
            UIApplication.shared.endBackgroundTask(backgroundTask)
            backgroundTask = UIBackgroundTaskInvalid
        }

只需调用 self.registerBackgroundTask() 即可让应用在后台运行三分钟。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-03-12
    • 2015-03-20
    • 2011-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多