我想爲iOS構建一個應用程序。這個想法是當呼叫狀態是Connected
時啓動UILocalNotification
。我嘗試在方法applicationDidEnterBackground
中使用dispatch_async
塊,並且我在此塊中放入了一個循環while(!isConnected)
以知道何時應該啓動UILocalNotificacion
。循環檢查呼叫狀態並決定何時啓動通知。當我的應用程序進入後臺時,可以將一個循環放入dispatch_async嗎?
我需要知道的是,這是解決我的問題的正確方法嗎?還是有人有更好的主意?
好吧,這是我的代碼:
- (void)applicationDidEnterBackground:(UIApplication *)application
{
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
isAlive = YES;
NSLog(@"BackgroundTimerRemaining %f", [application backgroundTimeRemaining]);
// verufy status of call
bgTask = [application beginBackgroundTaskWithExpirationHandler:^{
// Clean up any unfinished task business by marking where you
// stopped or ending the task outright.
[application endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}];
[self readLocalVariableCalling];
__block int connected = 0;
__block int callingTemp = 1;//((NSNumber*)[self.datos objectForKey:@"calling"]).intValue;
NSLog(@"callingTemp: %d", callingTemp);
__strong typeof(self) weakSelf = self;
// Start the long-running task and return immediately.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
@autoreleasepool
{
//isAlive = YES;
// Do the work associated with the task, preferably in chunks.
while (callingTemp == 1)
{
if (callCenter != nil)
{
callCenter.callEventHandler = ^(CTCall* call)
{
NSLog(@"Loop - Call EventHandler state: %@", call.callState);
if([call.callState isEqualToString:CTCallStateDisconnected])
{
NSLog(@"disconnected");
[[UIApplication sharedApplication] cancelAllLocalNotifications];
callingTemp = 0;
connected = 0;
[weakSelf.datos setObject:[NSNumber numberWithInt:0] forKey:@"calling"];
[weakSelf saveLocalVariableCalling];
}
if ([call.callState isEqualToString:CTCallStateDialing] &&
connected == 0)
{
NSLog(@"connected - Call EventHandler state : %@", call.callState);
connected = 1;
if (callingTemp == 1
&& weakSelf.timerTableViewController.currentTimer)
{
TimeDto *currentTimer = weakSelf.timerTableViewController.currentTimer;
NSInteger seconds = currentTimer.value.intValue;
NSLog(@"minutos a segundos = %d", seconds);
NSDate *alertTime = [[NSDate date] dateByAddingTimeInterval:seconds];
UIApplication* app = [UIApplication sharedApplication];
if (weakSelf.notifyAlarm)
{
weakSelf.notifyAlarm.fireDate = alertTime;
weakSelf.notifyAlarm.timeZone = [NSTimeZone defaultTimeZone];
weakSelf.notifyAlarm.repeatInterval = 0;
weakSelf.notifyAlarm.soundName = @"road.caf";
NSString *messageTimer = NSLocalizedString(@"messageTimer", nil);
weakSelf.notifyAlarm.alertBody = [NSString stringWithFormat:messageTimer, ((int)currentTimer.value.intValue/60) , (currentTimer.value.intValue % 60)];
[app scheduleLocalNotification:weakSelf.notifyAlarm];
}
}
}
};
}
else
{
NSLog(@"callCenter is null");
}
//NSLog(@"loop call state");
}
NSLog(@"Call Finished");
[application endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}
});
}
我真的不喜歡忙等待的想法。檢查http://stackoverflow.com/questions/3319805/detecting-call-state-in-ios4 –
好的,但這是在iOS 4,現在我們有iOS 6,我想我可能是一個可能的解決方案 – kakashy
其實,我的值得關注的是'while(!isConnected)'會浪費處理器的週期。 –