0

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

我通读了苹果文档 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

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

4

2 回答 2

0

尝试使用 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];
}
于 2013-05-14T12:08:15.643 回答
0

您可以在后台模式下运行应用程序 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() 即可使应用程序在后台运行三分钟。

于 2017-04-10T09:00:49.230 回答