Search code examples
iphonetwittermgtwitterengine

How to keep given password in mgtwitterengine api


I want to keep password which is given by user in a variable. Where to get password value. I look in code but it is containing only username. I am using MGTwitterengine api in my application. Here is the function which is printing data on console.

-(void)setUsername:(NSString *)newUsername password:(NSString *)newPassword
{
     NSLog(@"uset sername %@ and password %@",newUsername,newPassword);
     ...
}

I debug in all function but I saw this function is running after loggedin. But this is printing null in password value although username is printing well. Actually I I have to trace all tweet value of user. I went through MGTwitterEngine. There is a block of code but it is necessary to write username and password.

Here is the code

MGTwitterEngine *twitterEngine = [[MGTwitterEngine alloc] initWithDelegate:self]; 

[twitterEngine setUsername:@"username" password:@"password"]; 
// Get updates from people the authenticated user follows. 

NSString *connectionID = [twitterEngine getFollowedTimelineFor:nil since:nil startingAtPage:0];

Please help me how to keep password ?


Solution

  • Read the developer documentation, it provides all the info you need for something as easy as this. Back to the question, there're a couple of ways how you can save this data.

    NSUserDefaults: You can save the username and password using this class like this:

    [[NSUserDefaults sharedUserDefaults] setObject:username forKey:@"username"];
    [[NSUserDefaults sharedUserDefaults] setObject:username forKey:@"password"];
    [[NSUserDefaults sharedUserDefaults] synchronize];
    

    Once you need this data again, call it:

    NSString*username = [[NSUserDefaults standardUserDefaults] objectForKey:@"username"];
    NSString*password = [[NSUserDefaults standardUserDefaults] objectForKey:@"password"];
    

    OR:

    NSDictionary: If you don't want to rely on NSUserDefaults, you can save the login data in a NSDictionary and save it to your sandbox. This works like this:

    NSMutableDictionary*loginDictionary = [[NSMutableDictionary alloc] init];
    [loginDictionary setObject:username forKey:@"username"];
    [loginDictionary setObject:password forKey:@"password"];
    [loginDictionary writeToFile://Your Path Here atomically:NO];
    
    [loginDictionary release];
    

    Then, once you need the data again, read from file:

    NSMutableDictionary*loginDictionary = [[NSMutableDictionary alloc] initWithContentsOfFile://Your Path];
    
    NSString*username = [loginDictionary objectForKey:@"username"];
    NSString*password = [loginDictionary objectForKey:@"password"];
    
    [loginDictionary release];
    

    Hope it helps!