从iphone向twitter提交推文时出现问题

从iphone向twitter提交推文时出现问题,iphone,objective-c,cocoa-touch,ios4,Iphone,Objective C,Cocoa Touch,Ios4,MGTwitterEngine.m - (NSString *)username { return [[_username retain] autorelease]; } - (NSString *)password { return [[_password retain] autorelease]; } - (void)setUsername:(NSString *)newUsername password:(NSString *)newPassword { // Set new

MGTwitterEngine.m

- (NSString *)username
{
return [[_username retain] autorelease];
}


- (NSString *)password
{
return [[_password retain] autorelease];
}


- (void)setUsername:(NSString *)newUsername password:(NSString *)newPassword
{


// Set new credentials.


   [_username release];

_username = [newUsername retain];

    [_password release];


_password = [newPassword retain];


if ([self clearsCookies]) {

    // Remove all cookies for twitter, to ensure next connection uses new credentials.

    NSString *urlString = [NSString stringWithFormat:@"%@://%@", 
                           (_secureConnection) ? @"https" : @"http", 
                           _APIDomain];

    NSURL *url = [NSURL URLWithString:urlString];

    NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
    NSEnumerator *enumerator = [[cookieStorage cookiesForURL:url] objectEnumerator];
    NSHTTPCookie *cookie = nil;
    while (cookie == [enumerator nextObject]) {
        [cookieStorage deleteCookie:cookie];
    }
}
}

- (NSString *)sendUpdate:(NSString *)status
{
return [self sendUpdate:status inReplyTo:0];
}


- (NSString *)sendUpdate:(NSString *)status inReplyTo:(unsigned long)updateID
{
if (!status) {
    return nil;
}

NSString *path = [NSString stringWithFormat:@"statuses/update.%@", API_FORMAT];


NSString *trimmedText = status;

if ([trimmedText length] > MAX_MESSAGE_LENGTH) {


    trimmedText = [trimmedText substringToIndex:MAX_MESSAGE_LENGTH];

}

NSMutableDictionary *params = [NSMutableDictionary dictionaryWithCapacity:0];

[params setObject:trimmedText forKey:@"status"];

if (updateID > 0) {

    [params setObject:[NSString stringWithFormat:@"%u", updateID] forKey:@"in_reply_to_status_id"];

}

NSString *body = [self _queryStringWithBase:nil parameters:params prefixed:NO];


return [self _sendRequestWithMethod:HTTP_POST_METHOD path:path 
                    queryParameters:params body:body 
                        requestType:MGTwitterUpdateSendRequest
                       responseType:MGTwitterStatus];


}
- (IBAction)submitTweet{

[tweet resignFirstResponder];

if([[tweet text] length] > 0){


    NSLog(@"%@",[[NSUserDefaults standardUserDefaults] valueForKey:@"TwitterUsername"]);

    NSLog(@"%@",[[NSUserDefaults standardUserDefaults] valueForKey:@"TwitterPassword"]);       


    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];

    [engine sendUpdate:[tweet text]];

}

}



- (void)requestFailed:(NSString *)requestIdentifier withError:(NSError *)error{

NSLog(@"Fail: %@", error);

[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];

UIAlertView *failAlert;

if([error code] == 401){

    failAlert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Incorrect Username & Password." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];

    [failAlert setTag:10];

    [failAlert setDelegate:self];

} 
else 
{

    failAlert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Failed sending status to Twitter." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];

}

[failAlert show];

[failAlert release];

}
TwitterPostViewController.m

- (NSString *)username
{
return [[_username retain] autorelease];
}


- (NSString *)password
{
return [[_password retain] autorelease];
}


- (void)setUsername:(NSString *)newUsername password:(NSString *)newPassword
{


// Set new credentials.


   [_username release];

_username = [newUsername retain];

    [_password release];


_password = [newPassword retain];


if ([self clearsCookies]) {

    // Remove all cookies for twitter, to ensure next connection uses new credentials.

    NSString *urlString = [NSString stringWithFormat:@"%@://%@", 
                           (_secureConnection) ? @"https" : @"http", 
                           _APIDomain];

    NSURL *url = [NSURL URLWithString:urlString];

    NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
    NSEnumerator *enumerator = [[cookieStorage cookiesForURL:url] objectEnumerator];
    NSHTTPCookie *cookie = nil;
    while (cookie == [enumerator nextObject]) {
        [cookieStorage deleteCookie:cookie];
    }
}
}

- (NSString *)sendUpdate:(NSString *)status
{
return [self sendUpdate:status inReplyTo:0];
}


- (NSString *)sendUpdate:(NSString *)status inReplyTo:(unsigned long)updateID
{
if (!status) {
    return nil;
}

NSString *path = [NSString stringWithFormat:@"statuses/update.%@", API_FORMAT];


NSString *trimmedText = status;

if ([trimmedText length] > MAX_MESSAGE_LENGTH) {


    trimmedText = [trimmedText substringToIndex:MAX_MESSAGE_LENGTH];

}

NSMutableDictionary *params = [NSMutableDictionary dictionaryWithCapacity:0];

[params setObject:trimmedText forKey:@"status"];

if (updateID > 0) {

    [params setObject:[NSString stringWithFormat:@"%u", updateID] forKey:@"in_reply_to_status_id"];

}

NSString *body = [self _queryStringWithBase:nil parameters:params prefixed:NO];


return [self _sendRequestWithMethod:HTTP_POST_METHOD path:path 
                    queryParameters:params body:body 
                        requestType:MGTwitterUpdateSendRequest
                       responseType:MGTwitterStatus];


}
- (IBAction)submitTweet{

[tweet resignFirstResponder];

if([[tweet text] length] > 0){


    NSLog(@"%@",[[NSUserDefaults standardUserDefaults] valueForKey:@"TwitterUsername"]);

    NSLog(@"%@",[[NSUserDefaults standardUserDefaults] valueForKey:@"TwitterPassword"]);       


    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];

    [engine sendUpdate:[tweet text]];

}

}



- (void)requestFailed:(NSString *)requestIdentifier withError:(NSError *)error{

NSLog(@"Fail: %@", error);

[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];

UIAlertView *failAlert;

if([error code] == 401){

    failAlert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Incorrect Username & Password." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];

    [failAlert setTag:10];

    [failAlert setDelegate:self];

} 
else 
{

    failAlert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Failed sending status to Twitter." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];

}

[failAlert show];

[failAlert release];

}
它显示了错误用户名和密码的失败弹出窗口

我通过nslog检查了用户名和密码是否正确。
有什么问题吗?

看起来您正在使用的MGTwitterEngine版本正在尝试使用basic auth。这在推特上被关闭,取而代之的是OAuth。获取更新版本的MGTwitterEngine(或支持OAuth的fork)