这是我的答案。这使我的客户端应用程序在后台运行,并显示一个通知,当有来电的用武之地。
AppDelegate.h
@interface CameleonAppDelegate : NSObject <UIApplicationDelegate> {
CrestronClient *cClient;
CrestronControllerValues *CCV;
RootViewController *rootViewController;
CrestronValues *crestronValues;
UIBackgroundTaskIdentifier bgTask;
dispatch_block_t expirationHandler;
UIApplication* app;
BOOL showedCall;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;
@property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;
@property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;
- (void)saveContext;
- (NSURL *)applicationDocumentsDirectory;
-(void)save;
-(void)load;
- (void)backgroundHandler;
@end
AppDelegate.m(只是didFinishLaunchingWithOptions
和applicationDidEnterBackground
)
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
app = [UIApplication sharedApplication];
expirationHandler = ^{
[app endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
bgTask = [app beginBackgroundTaskWithExpirationHandler:expirationHandler];
};
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSArray *keys = [NSArray arrayWithObjects:@"IPaddress", @"PortNumber",@"IPID", nil];
NSArray *objs = [NSArray arrayWithObjects:@"10.8.40.64", @"41794",@"3", nil];
//10.8.30.143 10.8.40.64
NSDictionary *dict = [NSDictionary dictionaryWithObjects:objs forKeys:keys];
[defaults registerDefaults:dict];
CCV = [CrestronControllerValues sharedManager];
[CCV setIpAddress:[defaults stringForKey:@"IPaddress"]];
[CCV setPortNumber:[defaults stringForKey:@"PortNumber"]];
[CCV setIPID:[defaults stringForKey:@"IPID"]];
cClient = [CrestronClient sharedManager];
rootViewController = [[RootViewController alloc]initWithNibName:@"RootViewController" bundle:nil];
self.window.rootViewController = rootViewController;
[self.window makeKeyAndVisible];
return YES;
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
showedCall = FALSE;
BOOL backgroundAccepted = [[UIApplication sharedApplication] setKeepAliveTimeout:600 handler:^{ [self backgroundHandler]; }];
if (backgroundAccepted)
{
NSLog(@"VOIP backgrounding accepted");
}
bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
[app endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}];
// Start the long-running task
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
while (1) {
sleep(4);
//NSLog(@"BGTime left: %f", [UIApplication sharedApplication].backgroundTimeRemaining);
if ([rootViewController isIncomingCall] && showedCall != TRUE) {
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif) {
localNotif.alertBody = [NSString stringWithFormat:@"Incoming Call."];
localNotif.alertAction = NSLocalizedString(@"Accept Call", nil);
localNotif.soundName = @"alarmsound.caf";
localNotif.applicationIconBadgeNumber = 1;
[application presentLocalNotificationNow:localNotif];
[localNotif release];
}
showedCall = TRUE;
}
}
});
}
- (void)backgroundHandler {
NSLog(@"### -->VOIP backgrounding callback");
bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
[app endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}];
// Start the long-running task
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
while (1) {
NSLog(@"BGTime left: %f", [UIApplication sharedApplication].backgroundTimeRemaining);
[rootViewController isIncomingCall];
sleep(1);
}
});
}
如果你可以把你的应用程序放在前台,比哑应用程序可以做到这一点 - 你永远无法让它们消失。这不会缩放。 –
@ B蜥蜴可以请你撤销你做的事。这是问题的答案。不是问题的一部分。如果有什么删除我的后续问题。 –
当然。你应该把后续问题提出一个新的问题。 –