将 Twitter 集成到 iPhone 应用程序

发布于 2024-12-25 05:18:50 字数 3479 浏览 2 评论 0原文

我希望将 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:@"发推文"];
      }
      

3.) 其他 delagate 方法

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

应用程序在 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];
}

我该如何解决这个问题?

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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蘸点软妹酱 2025-01-01 05:18:50

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

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;
}
能怎样 2025-01-01 05:18:50

您没有理由这样做,Twitter 集成已经优化。

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文