I'm working on a twitter app for the iphone using oAuth/MGTwitterEngine The source it's based of is here: http://icodeblog.com/wp-content/uploads/2010/09/iCodeOauth.zip
But I want the user of the app to be able to go back and change the username and password if for an example the user has more than one twitter account.
Is it possible to give a button an action to open up the page that opens automatically the first time the app is opened. (The sign in page)
After digging into the code and playing with things I found a way to do this that is probably documented wherever you got the framework.
Looking at iCodeOauthViewController.m, inside of viewDidAppear:
you can call isAuthorized
on the engine and it will tell you if you are authenticated or not. If this returns yes, you can then call the clearAccessToken
method on the engine object to clear that authentication. When controllerToEnterCredentialsWithTwitterEngine: delegate:
is next called it will return the view controller for re-entering the user name and password.
edit: in iCodeOauthViewController.m inside fo viewDidAppear: (line 46) you will see this line:
UIViewController *controller = [SA_OAuthTwitterController controllerToEnterCredentialsWithTwitterEngine: _engine delegate: self];
this call returns the login screen that you see if the user is not yet logged in. If the user is logged in, it returns nil. If the controller is nil it jumps directly to the list.
to "log out" a user you could use this method:
- (void)switchUser
{
// log off the existing user if one is validated
if ([_engine isAuthorized])
[_engine clearAccessToken];
// display the login prompt
UIViewController *controller = [SA_OAuthTwitterController controllerToEnterCredentialsWithTwitterEngine: _engine delegate: self];
if (controller)
[self presentModalViewController: controller animated: YES];
}
edit 2: Looks like your problem is inside of your tweet method. You have added the alert code after the tweet attempts to send, and that results in a crash if the user isn't logged in. Here is your code:
-(IBAction)tweet:(id)sender {
[textfield resignFirstResponder];
[_engine sendUpdate:[textfield text]];
[self updateStream:nil];
if([_engine isAuthorized]==NO){UIAlertView *alert = [[UIAlertView alloc]
initWithTitle: @"Please, Sign in"
message: @"You'll have to sign in for this app to work!"
delegate: nil
cancelButtonTitle:@"Ok"
otherButtonTitles:nil];
[alert show];
[alert release];
}
}
change it to look like this:
-(IBAction)tweet:(id)sender {
if([_engine isAuthorized]==NO){
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle: @"Please, Sign in"
message: @"You'll have to sign in for this app to work!"
delegate: nil
cancelButtonTitle:@"Ok"
otherButtonTitles:nil];
[alert show];
[alert release];
}
else {
[textfield resignFirstResponder];
[_engine sendUpdate:[textfield text]];
[self updateStream:nil];
}
}
Notice that we now check to see if we are authenticated before trying to send the tweet, and if we are not authorized then pop up an alert instead. My apologies, I may have misled you with the releasing the alert thing, I misunderstood what you were saying.
I would recommend trying to understand a little more about how objective-c works and get familiar with the debugger. If you run the debugger and your app is crashing, the debugger will stop at the point in the code that is crashing, and you can look through the function calls in the stack to determine what the code is doing wrong. See this stack overflow question (specifically the answers) for more resources on how to get a better start with objective-c. I would recommend some of the online sites like CocoaDevCentral's tutorials. Remember this. You're off to a good start trying to make something your own based on an example. Don't be afraid to make a side project to play around with an idea if it's not immediately working out in your main project, even if it's something as simple as figuring out another way to do 2 + 2. Hope that helps.