NSUserDefault不起作用

时间:2012-03-30 05:29:45

标签: iphone

我正在开发一个具有evernote集成的应用程序,一切都很完美但我的问题是,如果我使用我的帐户登录并上传或下载笔记到我的aPP,那么我从evernote注销并以另一个用户身份登录,它仍然下载和上传到以前的帐户,但登录成功。 我把NsuserDefault用于用户名和密码textfileds和syncoronize它并在evernote singlton共享类中传递它。我的登录看起来像这样

-(IBAction)_clickevernotelogin:(id)sender
{


    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    // Keep this key private
    NSString *consumerKey = [[[NSString alloc]
                              initWithString: @"myapp" ] autorelease];
    [[NSUserDefaults standardUserDefaults] setObject:consumerKey forKey:@"consumerkeyevrnote"];
    NSString *consumerSecret = [[[NSString alloc]
                                 initWithString: @"agft5623636"] autorelease];

    NSURL *userStoreUri = [[[NSURL alloc]
                            initWithString: @"https://www.evernote.com/edam/user"] autorelease];
    NSString *noteStoreUriBase = [[[NSString alloc]
                                   initWithString: @"https://www.evernote.com/edam/note/"] autorelease];

    // These are for test purposes. At some point the user will provide his/her own.
    NSString *username = [[[NSString alloc]
                           initWithString: _txtevernoteUsername.text] autorelease];
    NSString *password = [[[NSString alloc]
                           initWithString: _txtevernotepasswrd.text] autorelease];
        [[NSUserDefaults standardUserDefaults] setObject:_txtevernoteUsername.text forKey:@"usernameever"];
        [[NSUserDefaults standardUserDefaults] setObject:_txtevernotepasswrd.text forKey:@"passwrdevernote"];
         [[NSUserDefaults standardUserDefaults]synchronize];

    THTTPClient *userStoreHttpClient = [[[THTTPClient alloc]
                                         initWithURL:userStoreUri] autorelease];
    TBinaryProtocol *userStoreProtocol = [[[TBinaryProtocol alloc]
                                           initWithTransport:userStoreHttpClient] autorelease];
    EDAMUserStoreClient *userStore = [[[EDAMUserStoreClient alloc]
                                       initWithProtocol:userStoreProtocol] autorelease];
    EDAMNotebook* defaultNotebook = NULL;

    BOOL versionOk = [userStore checkVersion:@"Cocoa EDAMTest" :
                      [EDAMUserStoreConstants EDAM_VERSION_MAJOR] :
                      [EDAMUserStoreConstants EDAM_VERSION_MINOR]];

    if (versionOk == YES)
    {

        @try {

            NSError *error = nil;
            NSString *unameever = _txtevernoteUsername.text;

            NSString *pwdever   = _txtevernotepasswrd.text;


            [[NSUserDefaults standardUserDefaults] setObject:unameever forKey:@"usernameever"];
            BOOL flag = [SFHFKeychainUtils storeUsername:unameever andPassword:pwdever forServiceName:@"mybibleappever" updateExisting:TRUE error:&error];
            EDAMAuthenticationResult* authResult =
            [userStore authenticate:username :password
                                   :consumerKey :consumerSecret];
            EDAMUser *user = [authResult user];
            NSString *authToken = [authResult authenticationToken];
            NSLog(@"Authentication was successful for: %@", [user username]);
            NSLog(@"Authentication token: %@", authToken);
             [[NSUserDefaults standardUserDefaults] setObject:authToken forKey:@"usernameevernotetocken"];
            [[NSUserDefaults standardUserDefaults] setObject:[user username] forKey:@"usernameevernoteshow"]; 
            NSURL *noteStoreUri =  [[[NSURL alloc]
                                     initWithString:[NSString stringWithFormat:@"%@%@",
                                                     noteStoreUriBase, [user shardId]] ]autorelease];
        }
        @catch (EDAMUserException * e) {
            NSString * errorMessage = [NSString stringWithFormat:@"Please enter valid username & password", [e errorCode]];
            UIAlertView *alertDone = [[UIAlertView alloc] initWithTitle: @"Evernote" message: errorMessage delegate: self cancelButtonTitle: @"Ok" otherButtonTitles: nil];
            _acteverloginimage.hidden = YES;
            _acteverlogin.hidden =YES;
            [alertDone show];
            [alertDone release];
            return;
        }


        [pool drain];


}

我的退出看起来像这样

 [_btnusernameshow setTitle:nil forState:UIControlStateNormal];
            NSError *error = nil;
            NSString *usernameever = [[NSUserDefaults standardUserDefaults] objectForKey:@"usernameever"];
            BOOL flag = [SFHFKeychainUtils deleteItemForUsername:usernameever andServiceName:@"mybibleappever" error:&error];
            [[NSUserDefaults standardUserDefaults ]removeObjectForKey:@"usernameevernote"];
            [[NSUserDefaults standardUserDefaults ]removeObjectForKey:@"passwrdevernote"];
            [[NSUserDefaults standardUserDefaults ]removeObjectForKey:@"usernameevernotetocken"];
            [[NSUserDefaults standardUserDefaults ]removeObjectForKey:@"usernameever"];

我的evernote singlton类共享实例看起来像这样 在这里,我传递nsuerdefault值以将身份验证信息更改为此类

- (void) connect {

    if (authToken == nil) 
    {      
        // In the case we are not connected we don't have an authToken
        // Instantiate the Thrift objects
        NSURL * NSURLuserStoreUri = [[[NSURL alloc] initWithString: userStoreUri] autorelease];

        THTTPClient *userStoreHttpClient = [[[THTTPClient alloc] initWithURL:  NSURLuserStoreUri] autorelease];
        TBinaryProtocol *userStoreProtocol = [[[TBinaryProtocol alloc] initWithTransport:userStoreHttpClient] autorelease];
        EDAMUserStoreClient *userStore = [[[EDAMUserStoreClient alloc] initWithProtocol:userStoreProtocol] autorelease];


        // Check that we can talk to the server
        bool versionOk = [userStore checkVersion: applicationName :[EDAMUserStoreConstants EDAM_VERSION_MAJOR] :    [EDAMUserStoreConstants EDAM_VERSION_MINOR]];

        if (!versionOk) {
            // Alerting the user that the note was created
            UIAlertView *alertDone = [[UIAlertView alloc] initWithTitle: @"Evernote" message: @"Incompatible EDAM client protocol version" delegate: self cancelButtonTitle: @"Ok" otherButtonTitles: nil];

            [alertDone show];
            [alertDone release];

            return;
        }


       NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
          [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(evrloginnotif:) name:@"evrloginnotiff" object:nil]; 
        //Checks if there is a saved User Name      
        NSError *error = nil;
        if([defaults objectForKey:@"usernameever"]&&[defaults objectForKey:@"passwrdevernote"])
        {


            username = [defaults objectForKey:@"usernameever"];
            password  = [defaults objectForKey:@"passwrdevernote"];

             //BOOL flag = [SFHFKeychainUtils storeUsername:username andPassword:password forServiceName:@"mybibleappever" updateExisting:TRUE error:&error];
            // Returned result from the Evernote servers after authentication
            EDAMAuthenticationResult* authResult =[userStore authenticate:username :password : consumerKey :consumerSecret];

            // User object describing the account
            self.user = [authResult user];
            // We are going to save the authentication token
            self.authToken = [authResult authenticationToken];
            // and the shard id
            self.shardId = [user shardId];

            // Creating the user's noteStore's URL
            noteStoreUri =  [[[NSURL alloc] initWithString:[NSString stringWithFormat:@"%@%@", noteStoreUriBase, shardId] ] autorelease];

            // Creating the User-Agent
            UIDevice *device = [UIDevice currentDevice];
            NSString * userAgent = [NSString stringWithFormat:@"%@/%@;%@(%@)/%@", applicationName,applicationVersion, [device systemName], [device model], [device systemVersion]]; 


            // Initializing the NoteStore client
            THTTPClient *noteStoreHttpClient = [[[THTTPClient alloc] initWithURL:noteStoreUri userAgent: userAgent timeout:15000] autorelease];
            TBinaryProtocol *noteStoreProtocol = [[[TBinaryProtocol alloc] initWithTransport:noteStoreHttpClient] autorelease];
            noteStore = [[[EDAMNoteStoreClient alloc] initWithProtocol:noteStoreProtocol] retain];

        }
    }
}

如何解决这个bug,谢谢 的修改

但是当我重建应用时,我会从用户名中获得正确的备注

2 个答案:

答案 0 :(得分:0)

NSUserDefaults *Test = [NSUserDefaults standardUserDefaults];

[Test removeObjectForKey:@"usernameevernote"];
[Test removeObjectForKey:@"passwrdevernote"];
[Test removeObjectForKey:@"usernameevernotetocken"];
[Test removeObjectForKey:@"usernameever"];
[Test synchronize];

也许您应该删除对象并再次进行同步,以便删除所有对象。

答案 1 :(得分:0)

添加“[[NSUserDefaults standardUserDefaults] synchronize];”在任何地方设置值或从NSUserDefault中删除密钥。

可能是同步问题。