0

I want my iOS app to do something special when a call comes in and the app itself is running in background. I Googled and found a function named applicationWillResignActive, but it is called when an application is running and is interrupted. This interruption could be due to an incoming call or an sms or when the user quits the application. But in my case I want my app to be in the background waiting for an incoming call and I would also like the app to read the number that called and look up that number in the contacts list of the phone. As suggested by other people that there was another similar question at this link but that says that Apple would reject the App as a breach of privacy. But in my case I am not extracting the numbers who called. The number will be used only while the call is coming after call is either received or cancelled I would clear the variable storing that value. Please let me know how this can be achieved and if doing this would be fine with Apple.

Community
  • 1
  • 1
MK Singh
  • 706
  • 1
  • 13
  • 36
  • 3
    You may use CoreTelephony framework to be notified about incoming calls. To get notified while your app is in background state, you need to keep your app in background mode. – Bilal Saifudeen May 25 '14 at 06:26
  • @Bilal thanks. I would also like to know how to keep my app always or most of the time in background state which starts at startup of the phone. – MK Singh May 25 '14 at 06:34
  • Read what Apple says on [Implementing Long-Running Background Tasks](https://developer.apple.com/library/ios/documentation/iphone/conceptual/iphoneosprogrammingguide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html) – Bilal Saifudeen May 25 '14 at 06:39
  • @BilalSaifudeen - I read the doc and it only states that when app goes in background its ask if it is allowed to run. If yes, then it monitors events. No where did I see a sample code saying "how" that is done. well how is it done?! – Sam B May 25 '14 at 06:48
  • 2
    You may need to use Location Services(very efficiently) to keep your app working in background. Most of the apps like Dropbox, TrueCaller etc use this to make some features works in the background. See [Dropbox explanation](https://www.dropbox.com/help/209/en) – Bilal Saifudeen May 25 '14 at 07:03

1 Answers1

0
[[NSNotificationCenter defaultCenter] addObserver:self  selector:@selector(appHasGoneInBackground) name:UIApplicationDidEnterBackgroundNotification
                                               object:nil];

Check for application enter in background....

-(void)appHasGoneInBackground
    {

        [[NSNotificationCenter defaultCenter] removeObserver:self];
        UIApplication *app = [UIApplication sharedApplication];
        //create new uiBackgroundTask
        __block UIBackgroundTaskIdentifier bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
            [app endBackgroundTask:bgTask];
            bgTask = UIBackgroundTaskInvalid;
        }];

        //and create new timer with async call:
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            //run function methodRunAfterBackground
            t = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(updateCounter) userInfo:nil repeats:YES];
            [[NSRunLoop currentRunLoop] addTimer:t forMode:NSDefaultRunLoopMode];
            [[NSRunLoop currentRunLoop] run];
        });
    }



    -(void)updateCounter
    { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(call) name:AVAudioSessionInterruptionNotification object:nil];

    }
Vibha Singh
  • 623
  • 4
  • 9
  • Your answer is nothing but code and it's not even properly formatted code. Hardly up to Stackoverflow standards. – dandan78 May 27 '14 at 13:29
  • It's not possible to track incoming call in iOS. Apple has restricted this feature. – Mohit May 27 '14 at 17:28
  • 1
    Recently I used this feature in my one of application and its working fine without any interruption from apple. – Vibha Singh May 28 '14 at 08:18