Integrate twitter to IPhone application

假如想象 提交于 2019-12-08 12:45:44

问题


I am hoping to integrate Twitter to my application. My project is created with ARC support. When i added the Twitter framework i also flagged the .m files with -fno-objc-arc.

I could login to twitter (meaning, the authentication process works), and after the authentication that application crashes when i click the tweet button.

1.) This is what gets called first, twitter authentication works fine.

-(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 - i have declared this is the .h file, but did not synthesize it in the .m

    1. This is where we are going to tweet

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

3.) other delagate methods

#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

In the NSLog username is null. Why is this ?

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

The application crashes in the MGTwitterEngine.m at the return statement.

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

How can i resolve this ?


回答1:


Check your SA_OAuthTwitterEngine.m and try to implement the changes below. You should change http to 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;
}



回答2:


There's no reason for you to do that, twitter integration has been optimized.

Check this link http://www.raywenderlich.com/5519/beginning-twitter-in-ios-5



来源:https://stackoverflow.com/questions/8762765/integrate-twitter-to-iphone-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!