Do I need to manage interruptions?

Discussion in 'iOS Programming' started by Alhazred, Jul 11, 2011.

  1. Alhazred macrumors member

    Jul 5, 2011
    I haven't understood if interruptions such as phone calls must be always managed or not.
    My app do this:
    - it shows a text field where the user insert a keyword
    - tapping a button a request is sent to a php script on a webserver
    - the app receives a text and a link to a picture from the php script
    - the app shows the text and the picture (loading it from a website)
    - the user can edit the text and send it to another php script which will save the edited text into a database

    This is all what my app does, do you see in any of these operation the need to save data or to manage anything else if an interruption occurs?
  2. chown33, Jul 11, 2011
    Last edited: Jul 11, 2011

    chown33 macrumors 604

    Aug 9, 2009
    Sailing beyond the sunset
    Underlined is where user-entered text should be preserved.

    Also, there is program state (accepting keyword, retrieving picture and text, editing text) that should be preserved. You can tell the state is significant because the different states aren't interchangeable. You can't retrieve a picture and text until after a keyword is entered. You can't edit the text until after it's retrieved for editing.
  3. Alhazred thread starter macrumors member

    Jul 5, 2011
    Thanks, I'm managing the interruption by using NSUserDefaults.

    I have a question:
    as I've understood I have to put the code to save the state inside the delegate method applicationDidEnterBackground() contained in the AppDelegate class, but how can I read from here the data in use on a view?

    In the specific on a view I have 1 picture, 2 editable labels.
    I can't use
    self.label1.text to read the label value from the AppDelegate, so how should I do?
  4. jnoxx macrumors 65816


    Dec 29, 2010
    Aartselaar // Antwerp // Belgium
    Why would u do self? because that's the AppDelegate.. You could refer to a pointer of your controller? or keep a singleton class, where u save everything. there are alot of possibilities..
  5. Alhazred thread starter macrumors member

    Jul 5, 2011
    I think I got what you mean, but not how to put in practice.
    I've done like this:
    MyViewController *detail = [[MyViewController alloc] init];
    [[NSUserDefaults standardUserDefaults] setObject:detail.label1.text forKey:@"savedLabel1"];
    [[NSUserDefaults standardUserDefaults] setObject:detail.label2.text forKey:@"savedLabel2"];
    but the values are null.
    Can you explain me how to do?
  6. jiminaus macrumors 65816


    Dec 16, 2010
    The code in red above is wrong. It's creating a new MyViewController object, that's why the values are null. You need to use a pointer to the current MyViewController object. Do you have a pointer to that object stored anywhere?
  7. PhoneyDeveloper macrumors 68040


    Sep 2, 2008
    The view controller should save/restore its own state. It needs to register for the appropriate notification (UIApplicationWillResignActiveNotification for example). In the callback method just save the state however you like.
  8. Alhazred thread starter macrumors member

    Jul 5, 2011
    Thanks, I've made it work with it. :)
  9. jiminaus macrumors 65816


    Dec 16, 2010
    That's neat and tidy.

Share This Page