2012-03-30 59 views
0

我正在开发一个应用程序,它具有evernote集成,一切都很完美,但我的问题是,如果我用我的帐户登录并向上注册或向我的aPP注释注释,然后我从Evernote登出并登录作为另一个用户,它仍然下行并注意到上一个帐户,但登录成功。 我把NsuserDefault用户名和密码textfileds和syncoronize并singlton共享class.my登录看起来像这样NSUserDefault无法正常工作

-(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类通过它在其中的Evernote分享实例看起来像这样 在这里我通过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 ,,感谢 编辑

但是当我重新构建应用程序,我从用户名获得正确的音符

回答

0
NSUserDefaults *Test = [NSUserDefaults standardUserDefaults]; 

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

也许你应该有删除对象和再次同步它,以便它将删除所有对象。

+0

我在哪里注销了这段代码? – stackiphone 2012-03-30 05:51:38

+0

是在注销按钮clik上。 – 2012-03-30 05:54:56

+0

它不是worrkingggg – stackiphone 2012-03-30 05:57:36

0

添加“[[NSUserDefaults standardUserDefaults] synchronize];”无论你设置值还是从NSUserDefault中删除密钥。

可能是同步问题。

+0

haii,但每次我重新启动应用程序,我会得到正确的音符,你正确的我认为我有在注销problm,用户没有删除...让我chk – stackiphone 2012-03-30 07:13:43

+0

运气不好,其不工作 – stackiphone 2012-03-30 07:20:31

+0

此代码之后: [[NSUserDefaults standardUserDefaults] setObject:authToken forKey:@“usernameevernotetocken”]; [[NSUserDefaults standardUserDefaults] setObject:[user username] forKey:@“usernameevernoteshow”]; 是否添加了“[[NSUserDefaults standardUserDefaults] synchronize];”上面的代码后? – 2012-03-30 08:24:24