【问题标题】:Twitter engine SA_OAuthTwitterEngine integration?Twitter 引擎 SA_OAuthTwitterEngine 集成?
【发布时间】:2012-05-25 21:17:02
【问题描述】:

我在我的应用程序中成功添加了SA_OAuthTwitterEngine,但我遇到了一个非常奇怪的问题。当我登录 Twitter 时,我的应用会崩溃:

- (void) OAuthTwitterController: (SA_OAuthTwitterController *) controller authenticatedWithUsername: (NSString *) username 
{
    [_engine sendUpdate:[NSString stringWithFormat:@"This Tweet for testing!"]];
    NSLog(@"Authenicated for %@", username);
}

应用程序在[_engine sendUpdate:[NSString stringWithFormat:@"This Tweet for testing!"]]; 上崩溃。如果我改用[_engine sendUpdate:nil];,那么它不会崩溃,但无法从我的帐户发送推文。

当我尝试跟踪 sendUpdate 方法时,我在以下函数中的 [theRequest prepare]; 语句上遇到了实际问题,

- (NSString *)_sendRequestWithMethod:(NSString *)method path:(NSString *)path queryParameters:(NSDictionary *)params body:(NSString *)body requestType:(MGTwitterRequestType)requestType responseType:(MGTwitterResponseType)responseType
{
    NSString *fullPath = path;
    NSString *urlString = [NSString stringWithFormat:@"%@://%@/%@", (_secureConnection) ? @"https" : @"http", _APIDomain, fullPath];
    NSURL *finalURL = [NSURL URLWithString:urlString];
    if (!finalURL) {
        return nil;
    }
OAMutableURLRequest *theRequest = [[OAMutableURLRequest alloc] initWithURL:finalURL consumer:self.consumer  token:_accessToken realm: nil signatureProvider:nil];
    if (method) {
        [theRequest setHTTPMethod:method];
    }
    [theRequest setHTTPShouldHandleCookies:NO];

    // Set headers for client information, for tracking purposes at Twitter.
    [theRequest setValue:_clientName    forHTTPHeaderField:@"X-Twitter-Client"];
    [theRequest setValue:_clientVersion forHTTPHeaderField:@"X-Twitter-Client-Version"];
    [theRequest setValue:_clientURL     forHTTPHeaderField:@"X-Twitter-Client-URL"];

    // Set the request body if this is a POST request.
    BOOL isPOST = (method && [method isEqualToString:@"POST"]);
    if (isPOST) {
        // Set request body, if specified (hopefully so), with 'source' parameter if appropriate.
        NSString *finalBody = @"";
        if (body) {
            finalBody = [finalBody stringByAppendingString:body];
        }
        if (_clientSourceToken) {
            finalBody = [finalBody stringByAppendingString:[NSString stringWithFormat:@"%@source=%@", (body) ? @"&" : @"?" , _clientSourceToken]];
        }

        if (finalBody) {
            [theRequest setHTTPBody:[finalBody dataUsingEncoding:NSUTF8StringEncoding]];
        }
    }
    [theRequest prepare];

    // Create a connection using this request, with the default timeout and caching policy, 
    // and appropriate Twitter request and response types for parsing and error reporting.
    MGTwitterHTTPURLConnection *connection;
    connection = [[MGTwitterHTTPURLConnection alloc] initWithRequest:theRequest delegate:self requestType:requestType responseType:responseType];

    if (!connection) {
        return nil;
    } else {
        [_connections setObject:connection forKey:[connection identifier]];
    }

    return [connection identifier];
}

崩溃报告是,

0  0x01f023ef in CFRelease ()
1  0x0008d04c in -[OAMutableURLRequest URLEncodedString:] (self=0x7bc673b0,       _cmd=0x9ee5f, string=0x0) at /Users/ben/Dropbox/Dev/External Projects/Twitter-OAuth-iPhone/OAuthConsumeriPhoneLib/OAuthConsumerSrc/OAMutableURLRequest.m:287
2  0x0008bbe3 in -[OAMutableURLRequest prepare] (self=0x7bc673b0, _cmd=0x978cc) at /Users/ben/Dropbox/Dev/External Projects/Twitter-OAuth-iPhone/OAuthConsumeriPhoneLib/OAuthConsumerSrc/OAMutableURLRequest.m:131
3  0x00088460 in -[SA_OAuthTwitterEngine _sendRequestWithMethod:path:queryParameters:body:requestType:responseType:] (_cmd=0x96833, method=0xc0250, body=0x7b971210, requestType=<value temporarily unavailable, due to optimizations>) at SA_OAuthTwitterEngine.m:315

4  0x0007f673 in -[MGTwitterEngine sendUpdate:inReplyTo:] (self=<value temporarily unavailable, due to optimizations>, _cmd=0x96914, updateID=2073243024) at MGTwitterEngine.m:1019

5  0x0007f496 in -[MGTwitterEngine sendUpdate:] (self=0x7b932d90, _cmd=0x95e02, status=0xc0510) at MGTwitterEngine.m:995

6  0x0007bf3f in -[DMUAppDelegate OAuthTwitterController:authenticatedWithUsername:] (self=0x7b918a20, _cmd=0x96152) at DMUAppDelegate.m:572

7  0x0008503b in -[SA_OAuthTwitterController gotPin:] (_cmd=0x975b4, pin=0x7ae573e0) at SA_OAuthTwitterController.m:125

8  0x00086137 in -[SA_OAuthTwitterController webViewDidFinishLoad:] (self=0x7b944a00, _cmd=0xf070e2, webView=0x7ae46750) at SA_OAuthTwitterController.m:224

9  0x00be036a in -[UIWebView webView:didFinishLoadForFrame:] ()

10 0x00be1956 in -[UIWebViewWebViewDelegate webView:didFinishLoadForFrame:] ()

11 0x01f5c51d in __invoking___ ()

12 0x01f5c437 in -[NSInvocation invoke] ()

13 0x01f8749a in -[NSInvocation invokeWithTarget:] ()

14 0x03462aff in -[_WebSafeForwarder forwardInvocation:] ()
15 0x01f5d0c9 in ___forwarding___ ()
16 0x01f5cce2 in __forwarding_prep_0___ ()

17 0x01f5c51d in __invoking___ ()
18 0x01f5c437 in -[NSInvocation invoke] ()

19 0x04005ae3 in SendMessage ()
20 0x04006115 in HandleDelegateSource ()

21 0x01fca97f in __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ ()
22 0x01f2db73 in __CFRunLoopDoSources0 ()
23 0x01f2d454 in __CFRunLoopRun ()
24 0x01f2cdb4 in CFRunLoopRunSpecific ()
25 0x01f2cccb in CFRunLoopRunInMode ()
26 0x01e60879 in GSEventRunModal ()
27 0x01e6093e in GSEventRun ()

28 0x00a29a9b in UIApplicationMain ()
29 0x00040296 in main (argc=1, argv=0xc003c604) at /Users/dhirenshah/Desktop/Projects/Projects/DMU/DMU/main.m:16
(gdb) 

【问题讨论】:

    标签: iphone objective-c ipad twitter


    【解决方案1】:

    是的,我终于解决了我的问题。我只是替换代码,

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

    修改 SA_OAuthTwitterEngine.m 函数。我从MGTwitterEngine crash in iOS5 获得了帮助,这个链接。

    【讨论】:

    • 目前,我在 iPhone 4s 上使用 iOS 6.1.3。它并没有真正给我一个错误,它只是崩溃,当我跟踪它时,它指向 [theRequest prepare] 方法。我还按照您包含的链接中的固定内容进行操作。
    【解决方案2】:

    我已经对上面的代码进行了更改,因为twitter api已经更改,所以旧的将无法使用。

    对于任何新用户,如果他们在 MGTwitterEnginer 中使用上述格式,它将无法正常工作,您需要替换

    self.requestTokenURL = [NSURL URLWithString:@"https://api.twitter.com/oauth/request_token"]; self.accessTokenURL = [NSURL URLWithString:@"https://api.twitter.com/oauth/access_token"]; self.authorizeURL = [NSURL URLWithString:@"https://api.twitter.com/oauth/authorize"];

    【讨论】:

      【解决方案3】:

      我运行的是 iOS 6.1.3,我的测试设备是 iPhone 4s。我得到了与 xcode 将崩溃指向 [theRequest prepare]; 方法相同的错误/崩溃。

      我的固定是:

              OAMutableURLRequest *theRequest = [[[OAMutableURLRequest alloc]         
                       initWithURL:finalURL
                       consumer:self.consumer
                       token:_accessToken
                       realm: nil
                       signatureProvider:nil] autorelease];
      

      将自动释放放在这一行并在发布推文之前再次检查[_engine isAuthorized]

              if ([_engine isAuthorized])
              {
                   NSLog(@"post Tweet");
                   . . . . . .
              } else {
                   NSLog(@"cant post Tweet");
                   . . . . . .
              }
      

      我无法进一步解释,因为我还不熟悉这个,但这对我有用。

      【讨论】:

      • 酷。感谢您分享您的解决方案以及这个问题。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-27
      • 2011-12-13
      • 2016-08-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-07
      相关资源
      最近更新 更多