0

I have a button which when pressed creates an object thing. It then sleeps for ten seconds and then calls thing.go which gets the application state like

UIApplicationState state = [[UIApplication sharedApplication] applicationState];

Then following this question's answer and comments I check if the app is in the background, and if it is, it displays a local notification.

So I press the button and immediately press the home key to go to the main screen (thereby putting the app in the background)

However, I can tell that the if statement is returning false and is therefore not executing any code within (by putting an NSLog inside the if statement).

So my next thought process was to somehow display the date in the console using NSLog(state). Obviously I cannot do this.

So how can I display the applicationState to resolve my issue? Or is there another way I can check to see if the app is running in the background within this class's method?

Here is the go function which is called inside the IBAction when the button is pressed

-(void)go {
    if (state == UIApplicationStateBackground) {
        NSLog(@"App is in background");
    }
}

Here is the IBAction which is linked to the button, i.e. runs when button is pressed

- (IBAction)button_help {
    myclass* thingy = [[myclass alloc] init];
    sleep(10);
    thingy.go;
}
Community
  • 1
  • 1
E.Cross
  • 2,087
  • 5
  • 31
  • 39
  • code added just for you Zaph :) – E.Cross Oct 26 '13 at 02:50
  • 4
    FYI - it's bad practice to call methods using property notation, even if it works. Call the `go` method like: `[thingy go];`. – rmaddy Oct 26 '13 at 03:37
  • Can you not just use the `applicationDidEnterBackground` method to do what your wanting to do? https://developer.apple.com/library/ios/documentation/uikit/reference/UIApplicationDelegate_Protocol/Reference/Reference.html#//apple_ref/occ/intfm/UIApplicationDelegate/applicationDidEnterBackground: – Popeye Oct 30 '13 at 12:02

2 Answers2

1

You should pretty much NEVER use sleep. Forget it exists. It locks up your app, and nothing changes, including transitions to the background.

What you want to do is to set a timer for 10 seconds that then invokes thing.go.

However, that's going to be complicated by the fact that normally timers don't run in the background. In fact, unless you take special steps, your app doesn't get any processor time in the background. You get told that you are going to the background, and then the next call you get is the return-to-foregraound.

When your app gets a message that it is being sent to the background, it will need to make the system call that asks for background processing time. (I don't remember the call off the top of my head, and don't have Xcode running at the moment.)

Apple changed the rules for background processing in iOS 7, but you say this is iOS 6, so those changes don't apply.

Duncan C
  • 128,072
  • 22
  • 173
  • 272
0

If I understand correctly, you're checking to see if the app is returning from a local notification or if it was already active?

if (state == UIApplicationStateActive) {
   // do something
   NSLog(@"application was active ");
} else {
   NSLog(@"sent from notification");
}
Edward
  • 319
  • 4
  • 13
  • https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/IPhoneOSClientImp.html Look up application didReceiveLocalNotification: and application didFinishLaunchingWithOptions. – Edward Oct 26 '13 at 02:55
  • I did look at that. But that is not what I am asking. – E.Cross Oct 26 '13 at 02:59
  • Okay - you just said 'that's right' to my question, and that's what answers it. Upon re-reading your question to try and work out what you mean, I'm assuming you want to schedule the notification in the background, which means you need to be looking at https://developer.apple.com/library/ios/documentation/iphone/conceptual/iphoneosprogrammingguide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html#//apple_ref/doc/uid/TP40007072-CH4-SW20 , specifically background execution. – Edward Oct 26 '13 at 03:07
  • **you're checking to see if the app is returning from a local notification** What that doesn't make sense do you mean if it has become active or not? – Popeye Oct 30 '13 at 12:01