简体   繁体   English

oAuth / MGTwitterEngine改变iPhone的用户?

[英]oAuth/MGTwitterEngine change user for the iPhone?

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 我正在使用oAuth / MGTwitterEngine开发适用于iPhone的Twitter应用程序,其基于的源位于: 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. 但我希望应用程序的用户能够返回并更改用户名和密码,例如,如果用户拥有多个Twitter帐户。

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. 查看viewDidAppear:内部的iCodeOauthViewController.m viewDidAppear:您可以在引擎上调用isAuthorized ,它会告诉您是否已通过身份验证。 If this returns yes, you can then call the clearAccessToken method on the engine object to clear that authentication. 如果返回“是”,则可以在引擎对象上调用clearAccessToken方法以清除该身份验证。 When controllerToEnterCredentialsWithTwitterEngine: delegate: is next called it will return the view controller for re-entering the user name and password. 下一次调用controllerToEnterCredentialsWithTwitterEngine: delegate: ,它将返回视图控制器以重新输入用户名和密码。

edit: in iCodeOauthViewController.m inside fo viewDidAppear: (line 46) you will see this line: 编辑:在viewDidAppear里面的iCodeOauthViewController.m中:(第46行)你会看到这一行:

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. 如果用户尚未登录,此调用将返回您看到的登录屏幕。如果用户已登录,则返回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. 编辑2:看起来你的问题在你的推文方法中。 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 . 我建议尝试更多地了解objective-c的工作原理并熟悉调试器 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. 有关如何更好地开始使用objective-c的更多资源,请参阅此堆栈溢出问题 (特别是答案)。 I would recommend some of the online sites like CocoaDevCentral's tutorials. 我会推荐一些在线网站,如CocoaDevCentral的教程。 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. 如果它不能立即在你的主项目中运作,不要害怕做一个侧面项目来玩一个想法,即使它是一个简单的事情,如找出另一种做2 + 2的方式。希望有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM