這是我的答案。這使我的客戶端應用程序在後臺運行,並顯示一個通知,當有來電的用武之地。
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蜥蜴可以請你撤銷你做的事。這是問題的答案。不是問題的一部分。如果有什麼刪除我的後續問題。 –
當然。你應該把後續問題提出一個新的問題。 –