Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8866611
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T16:49:54+00:00 2026-06-14T16:49:54+00:00

I’m working on a project for learning obj-c by building a small application thas

  • 0

I’m working on a project for learning obj-c by building a small application thas talkt to a PHP/MySQL api.

Every time a user does something, a new auth-key is set for this user and is sent to the client. The client receives this value and should set it to NSUserDefaults. This is for not letting the user logon every time the program starts; if this key exsists it will try to authenticate with this key and then login automatically.

This works, some times… I have two functions, one for recieving and one for setting. They looks like this:

//GET
-(NSString*)GetValue:(NSString *)Name
{
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    NSString *get = [prefs stringForKey:Name];

    if(get)
        return get;
    else
        return FALSE;
}
//SET
-(void)SetValue:(NSString*)Name:(NSString*)Value
{
    //Add new...    
    [[NSUserDefaults standardUserDefaults] setObject:Value forKey:Name];    
}

This works, almost.
The problem is like this:

1) The auth-key value is not set (in NSUserDefaults)
2) I try to login the user, the value sets (like “aaaaaaaaaaaaaaaa”) and everything’s fine. I restart the program, it takes this stored value, tries to login, gets a positive result back from PHP along with a new key wich has to be set when the answer returns… like this…

if(NewKey.length > 0)
{
    NSLog(@"TRY SET KEY: %@", NewKey);
    [self SetValue:@"auth_key":NewKey];
    NSLog(@"TRY GET KEY: %@", [self GetValue:@"auth_key"]);
}

3) The NSLog above says “TRY SET KEY: bbbbbbbbbbbbbbbb” and “TRY GET KEY: bbbbbbbbbbbbbbbb”. Looking perfect!
4) Fine, i kill the program and tries again, now it should have stored “bbb…”, right? Wrong! When i now NSLogs this value, it’s still “aaa…”
5) I try login again, the NSLog says “TRY SET KEY: ccc…” and “TRY GET KEY: ccc…” – it says this value is stored. But it’s not…
6) I kill program, restart and gets “aaa…”.
7) I kill program, in the AppDelegate i manually sets this value (With the same function) to like “xxx”. I run the program and it sets the key to something like “xx”. I delete this line, restarts program, the “xxx” is stored properly and now i’m back on #2; I can do one login, it stores this value properly and then i can do one auto-login but on that login it stops saving it…

So, what is wrong here? I’ve tried [[NSUserDefaults standardUserDefaults] removeObjectForKey:Name]; as first line in SetValue but it does not helps.

It is like it sets, and then beeing forgotten anyway… What am i doing wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T16:49:55+00:00Added an answer on June 14, 2026 at 4:49 pm

    NSUserDefaults doesn’t write the changes to file at the moment when you set a value. It does so periodically, once in a while. If you want it to immediately persist the changes to disk, you should synchronize it:

    [[NSUserDefaults standardUserDefaults] setObject:Value forKey:Name];
    [[NSUserDefaults standardUserDefaults] synchronize];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I used javascript for loading a picture on my website depending on which small
this is what i have right now Drawing an RSS feed into the php,
I have a small JavaScript validation script that validates inputs based on Regex. I
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I would like to count the length of a string with PHP. The string
We're building an app, our first using Rails 3, and we're having to build

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.