将Twitter集成到iPhone应用程序

时间:2012-01-06 18:40:41

标签: iphone objective-c twitter twitter-oauth mgtwitterengine

我希望将Twitter集成到我的应用程序中。我的项目是使用ARC支持创建的。当我添加Twitter框架时,我还使用-fno-objc-arc标记了.m文件。

我可以登录到twitter(意思是,身份验证过程有效),并且在我点击推文按钮后应用程序崩溃的身份验证之后。

1。)这是首先调用的,twitter认证工作正常。

-(void)loginTwitterUser{
    if(_engine) return;


    _engine = [[SA_OAuthTwitterEngine alloc] initOAuthWithDelegate:self];  
    _engine.consumerKey = @"xxxxxx";
    _engine.consumerSecret = @"xxxxxx";

    if (![_engine isAuthorized]) {
        UIViewController *controller = [SA_OAuthTwitterController controllerToEnterCredentialsWithTwitterEngine:_engine delegate:self];
        if (controller) {
            [self presentModalViewController:controller animated:YES];
        }

    }
  • _engine - 我已声明这是.h文件,但没有在.m

    中合成它
    1. 这是我们要推文的地方

      -(void)tweetThis:(id)sender{
      [_engine sendUpdate:@"Tweet something"];
      }
      

3.)其他解决方法

#pragma mark SA_OAuthTwitterEngineDelegate
- (void) storeCachedTwitterOAuthData: (NSString *) data forUsername: (NSString *) username {
    NSUserDefaults   *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject: data forKey: @"authData"];
    [defaults synchronize];
}

- (NSString *) cachedTwitterOAuthDataForUsername: (NSString *) username {

    return [[NSUserDefaults standardUserDefaults] objectForKey: @"authData"];
}


#pragma mark TwitterEngineDelegate
- (void) requestSucceeded: (NSString *) requestIdentifier {

    NSLog(@"Request %@ succeeded", requestIdentifier);
}

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

    NSLog(@"Request %@ failed with error: %@", requestIdentifier, error);
}

#pragma mark SA_OAuthTwitterController Delegate

在NSLog中usernamenull。这是为什么?

- (void) OAuthTwitterController: (SA_OAuthTwitterController *) controller authenticatedWithUsername: (NSString *) username {

    NSLog(@"Authenticated with user %@", username);

}


- (void) OAuthTwitterControllerFailed: (SA_OAuthTwitterController *) controller  {

    NSLog(@"Authentication Failure");
}

- (void) OAuthTwitterControllerCanceled: (SA_OAuthTwitterController *) controller {

    NSLog(@"Authentication Canceled");
}

应用程序在return语句的MGTwitterEngine.m崩溃。

- (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];
}

我该如何解决这个问题?

2 个答案:

答案 0 :(得分:1)

检查您的SA_OAuthTwitterEngine.m并尝试执行以下更改。您应该将http更改为https。

- (SA_OAuthTwitterEngine *) initOAuthWithDelegate: (NSObject *) delegate {
if (self = (id) [super initWithDelegate: delegate]) 
    {
        self.requestTokenURL = [NSURL URLWithString: @"https://twitter.com/oauth/request_token"];
        self.accessTokenURL = [NSURL URLWithString: @"https://twitter.com/oauth/access_token"];
        self.authorizeURL = [NSURL URLWithString: @"https://twitter.com/oauth/authorize"];
    }
    return self;
}

答案 1 :(得分:0)

你没有理由这样做,推特整合已经过优化。

点击此链接http://www.raywenderlich.com/5519/beginning-twitter-in-ios-5