首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >当用户名/密码对Twitter不正确时,如何显示操作表?

当用户名/密码对Twitter不正确时,如何显示操作表?
EN

Stack Overflow用户
提问于 2010-01-04 22:42:10
回答 2查看 928关注 0票数 0

我希望我的应用程序在用户按下"Tweet"-Button并且用户名或密码错误时显示一个操作表。对于我的Twitterfunction,我使用Brandon Trebitowski的TwitterRequest.m/h。如果一切正常,用户名/密码正确,这在我的应用程序中就会发生:

代码语言:javascript
代码运行次数:0
运行
复制
        TwitterRequest * t = [[TwitterRequest alloc] init];
        (...);
        [t statuses_update:twittermessage.text delegate:self requestSelector:@selector(status_updateCallback:)];

        loadingActionSheet = [[UIActionSheet alloc] initWithTitle:@"Posting to Twitter..." delegate:nil 
                                                cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
        [loadingActionSheet showInView:self.view];
    }


    - (void) status_updateCallback: (NSData *) content {
        [loadingActionSheet dismissWithClickedButtonIndex:0 animated:YES];
        [loadingActionSheet release];
        NSLog(@"%@",[[NSString alloc] initWithData:content encoding:NSASCIIStringEncoding]);
    }

但是,当用户名/密码错误时,我如何显示其他操作表?下面是TwitterRequest.m:

代码语言:javascript
代码运行次数:0
运行
复制
#import "TwitterRequest.h"

@implementation TwitterRequest

@synthesize username;
@synthesize password;
@synthesize receivedData;
@synthesize delegate;
@synthesize callback;
@synthesize errorCallback;

-(void)friends_timeline:(id)requestDelegate requestSelector:(SEL)requestSelector{
    isPost = NO;
    // Set the delegate and selector
    self.delegate = requestDelegate;
    self.callback = requestSelector;
    // The URL of the Twitter Request we intend to send
    NSURL *url = [NSURL URLWithString:@"http://twitter.com/statuses/friends_timeline.xml"];
    [self request:url];
}

-(void)statuses_update:(NSString *)status delegate:(id)requestDelegate requestSelector:(SEL)requestSelector; {
    isPost = YES;
    // Set the delegate and selector
    self.delegate = requestDelegate;
    self.callback = requestSelector;
    // The URL of the Twitter Request we intend to send
    NSURL *url = [NSURL URLWithString:@"http://twitter.com/statuses/update.xml"];
    requestBody = [NSString stringWithFormat:@"status=%@",status];
    [self request:url];
}

-(void)request:(NSURL *) url {
    theRequest   = [[NSMutableURLRequest alloc] initWithURL:url];

    if(isPost) {
        NSLog(@"ispost");
        [theRequest setHTTPMethod:@"POST"];
        [theRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
        [theRequest setHTTPBody:[requestBody dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]];
        [theRequest setValue:[NSString stringWithFormat:@"%d",[requestBody length] ] forHTTPHeaderField:@"Content-Length"];
    }

    theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

    if (theConnection) {
        // Create the NSMutableData that will hold
        // the received data
        // receivedData is declared as a method instance elsewhere
        receivedData=[[NSMutableData data] retain];
    } else {
        // inform the user that the download could not be made
    }
}

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
    //NSLog(@"challenged %@",[challenge proposedCredential] );

    if ([challenge previousFailureCount] == 0) {
        NSURLCredential *newCredential;
        newCredential=[NSURLCredential credentialWithUser:[self username]
                                                 password:[self password]
                                              persistence:NSURLCredentialPersistenceNone];
        [[challenge sender] useCredential:newCredential
               forAuthenticationChallenge:challenge];

    } else {
        [[challenge sender] cancelAuthenticationChallenge:challenge];
        // inform the user that the user name and password
        // in the preferences are incorrect
        NSLog(@"Invalid Username or Password");
    }

}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    // this method is called when the server has determined that it
    // has enough information to create the NSURLResponse

    // it can be called multiple times, for example in the case of a
    // redirect, so each time we reset the data.
    // receivedData is declared as a method instance elsewhere
    //[receivedData setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    //NSLog([[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
    // append the new data to the receivedData
    // receivedData is declared as a method instance elsewhere
    [receivedData appendData:data];
}

- (void)connection:(NSURLConnection *)connection
  didFailWithError:(NSError *)error
{
    // release the connection, and the data object
    [connection release];
    // receivedData is declared as a method instance elsewhere
    [receivedData release];

    [theRequest release];

    // inform the user
    NSLog(@"Connection failed! Error - %@ %@",
          [error localizedDescription],
          [[error userInfo] objectForKey:NSErrorFailingURLStringKey]);

    if(errorCallback) {
        [delegate performSelector:errorCallback withObject:error];
    }
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    // do something with the data

    if(delegate && callback) {
        if([delegate respondsToSelector:self.callback]) {
            [delegate performSelector:self.callback withObject:receivedData];
        } else {
            NSLog(@"No response from delegate");
        }
    } 

    // release the connection, and the data object
    [theConnection release];
    [receivedData release];
    [theRequest release];
}

-(void) dealloc {
    [super dealloc];
}


@end

对于这个愚蠢的问题,我很抱歉,但我只学习了一周的Objective-C和一般编程,我不知道如何正确地从我的ViewController与其他类进行交互。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-01-04 23:24:37

要实现动作单,您必须首先在头文件中实现UIActionSheetDelegate (在<>之间的@接口定义中包含UIActionSheetDelegate )。

在您的代码中,您将显示动作单并捕获按钮按下时的动作。要显示操作表,请执行以下操作:

代码语言:javascript
代码运行次数:0
运行
复制
UIActionSheet *actionSheet = [[UIActionSheet alloc]
                              initWithTitle:@"Choose one, por favor"
                              delegate:self
                              cancelButtonTitle:@"Cancel"
                              destructiveButtonTitle:nil
                              otherButtonTitles:@"Save Favorite", @"Email", nil];
actionSheet.actionSheetStyle = UIActionSheetStyleBlackTranslucent;
actionSheet.cancelButtonIndex = 2;
[actionSheet showInView:self.view];
[actionSheet release];

要对按钮按下操作,请使用以下方法:

代码语言:javascript
代码运行次数:0
运行
复制
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    // the user clicked one of the OK/Cancel buttons
    NSLog(@"The button index is: %i", buttonIndex);

    switch (buttonIndex) {
        case 0:
            NSLog(@"Button 0");
            [self saveNew];
            break;
        case 1:
            NSLog(@"Button 1");
            [self sendEmail];
            break;
        case 2:
            NSLog(@"Button 2");
            break;
        default:
            break;
    }
}

您的另一个选择是使用警报--对于不正确的用户名/密码,这可能是最好的选择。警报是一个显示在屏幕中央的模式框。要实现警报,请在头文件中实现UIAlertViewDelegate。

示例警报代码如下:

代码语言:javascript
代码运行次数:0
运行
复制
UIAlertView *alert;
alert = [[UIAlertView alloc] initWithTitle:@"Ouch!"
  message:@"Your message is placed here"
  delegate:self 
  cancelButtonTitle:@"OK" 
  otherButtonTitles: nil];
[alert show];   
[alert release];
票数 2
EN

Stack Overflow用户

发布于 2010-01-04 23:30:38

如果您希望在用户收到身份验证请求时做出反应,则需要在didReceiveAuthenticationChallenge内部工作。

具体来说,就在这条线的正上方:

代码语言:javascript
代码运行次数:0
运行
复制
newCredential=[NSURLCredential credentialWithUser:[self username]
                                             password:[self password]
                                          persistence:NSURLCredentialPersistenceNone];

是您希望从用户处获取用户名和密码的位置。

如果您希望处理身份验证失败,则需要利用previousCountFailure if语句中的'else‘子句。

具体地说,在这一行之后是您想要告诉用户他们失败的地方:

代码语言:javascript
代码运行次数:0
运行
复制
[[challenge sender] cancelAuthenticationChallenge:challenge];
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1999930

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档