0

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)

PeeHaa
  • 71,436
  • 58
  • 190
  • 262
Tapy
  • 1,044
  • 6
  • 17
  • 30

1 Answers1

2

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.

Community
  • 1
  • 1
slycrel
  • 4,275
  • 2
  • 30
  • 30
  • Sorry for asking but i'm still learning objective-c. Would it be possible for you to provide me with the code? – Tapy Dec 20 '10 at 17:17
  • I figured out where this screen came up and where the authorization is happening by using the debugger. You may want to try this out and see if you can figure it out, even with the code above. Start with putting a breakpoint at the top of the viewDidAppear: method. – slycrel Dec 20 '10 at 19:50
  • Thanks!I also managed to give a button the action to go to the page. But there's one thing I couldn't figure out in debugger. You see if you've clicked on cancel without you've typed in username and password and try to post something the app crashes. Would it be possible to display an alert or something telling the user that he/she is not signed in? – Tapy Dec 20 '10 at 21:30
  • in the code where they try to post the tweet you could check the same [_engine isAuthorized] call and if they are not authorized pop up an alert and don't actually send the tweet. – slycrel Dec 20 '10 at 22:00
  • Ok but how do I tell the engine that if it's not authorized? The first thing I thought of was if ([_engine isNotAuthorized]) But that didn't work. – Tapy Dec 20 '10 at 23:13
  • [_engine isAuthorized] returns a boolean value. true if it is authorized, false if it is not. No need to change the call. – slycrel Dec 21 '10 at 00:15
  • Look at this link for UIAlertView: http://getsetgames.com/2009/12/02/iphonedev-advent-tip-2-how-to-show-an-alert-with-uialertview/ – slycrel Dec 21 '10 at 19:16
  • And for the check you can do if([_engine isAuthorized] == NO) { // do your alert } else { // continue as normal } – slycrel Dec 21 '10 at 19:17
  • Hmm, the app starts etc but when I click on the tweet button without signing in the app crashes and the alert is not displayed – Tapy Dec 21 '10 at 21:44
  • 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 { // continue as normal } – Tapy Dec 21 '10 at 21:45
  • That's the code I've added, is there something wrong with the code or have I forgotten something? – Tapy Dec 21 '10 at 21:46
  • do not release the alert, that is causing your crash. I believe that once the OK button is pressed it will get released. Additionally you may want to return at the end of that block of code and take out the else statement. – slycrel Dec 22 '10 at 02:53
  • It still crashes without displaying the alert. Should I upload the code so you can have a look at it when you got time? – Tapy Dec 22 '10 at 07:57
  • Well I did upload it so if you could have a look at it when you got time then that would be great! http://www.mediafire.com/?v044xrgwjnin1jj – Tapy Dec 22 '10 at 13:03
  • Thank you so much for all your help! I've learnt alot just by this and I'm definitely going to check out those websites. – Tapy Dec 23 '10 at 21:27