0

iOS 用のアプリケーションを作成したいと考えています。UILocalNotificationアイデアは、通話状態が のときにを起動することですConnecteddispatch_asyncメソッドでブロックを使用しようとしましたapplicationDidEnterBackgroundが、このブロックにループを入れて、while(!isConnected)いつ起動する必要があるかを知りましたUILocalNotificacion。ループは通話状態をチェックし、いつ通知を開始するかを決定します。

私が知る必要があるのは、その方法が私の問題を解決する正しい方法ですか? または誰かがより良いアイデアを持っていますか?

わかりました、これは私のコードです:

- (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;
    }

});
}
4

1 に答える 1

3

作業のスケジュール方法に関係なく、アプリケーションがバックグラウンドに入ると、すべてのアクティビティが停止する前に、一定量の処理のみが許可されます。しようとしてdispatch_asyncも、魔法のように永続的なマルチタスクを購入することはできません。

いいえ、これで問題が解決するとは思いません。

于 2013-06-06T00:06:00.387 に答える