簡體   English   中英

將Twitter集成到iPhone應用程序

[英]Integrate twitter to IPhone application

我希望將Twitter集成到我的應用程序中。 我的項目是在ARC支持下創建的。 當我添加Twitter框架時,我還用-fno-objc-arc標記了.m文件。

我可以登錄Twitter(意味着身份驗證過程正常),並且在身份驗證之后,當我單擊tweet按鈕時應用程序崩潰。

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中, username名為null 為什么是這樣 ?

- (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");
}

該應用程序在MGTwitterEngine.m中的return語句中崩潰。

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

我該如何解決?

檢查您的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;
}

沒有理由讓您這樣做,Twitter集成已得到優化。

檢查此鏈接http://www.raywenderlich.com/5519/beginning-twitter-in-ios-5

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM