升級到iOS5後ASIHttpRequest庫問題及解決方案

來源:互聯網
上載者:User

由於正式版的iOS5出來了,所以我也試著去升級了。於是下載了最新的Xcode,才1.7G左右,比以往的安裝包要小許多。

升級Xcode後,開啟以前建立的工程, 運氣好,一個錯誤都沒有,程式也能正常跑起來。由於我程式中用了ASIHttpRequest這個庫,讓我發現了一個小問題,就是

ASIAuthenticationDialog這個內建對話方塊在網路有代理的情況下出現,然後無論點cancle或是login都不能dismiss。在4.3的SDK中完全沒問題,在5.0的SDK中就會在Console中看到輸出:

Unbalanced calls to begin/end appearance transitions for <ASIAutorotatingViewController:>

很明顯示在sdk5中, 用這個庫有問題,還有在停止調式的時候,程式會有異常產生。

於是很明顯示是SDK5的變化影響了ASIHttpRequest的正常使用。於是我要fix這個問題,經過我研究發現,dismiss不起作用是由於UIViewController的parentViewController不再返回正確值了,返回的是nil,而在SDK5中被presentingViewController取代了。於是在ASIAuthenticationDialog.m中找到+(void)dismiss這個方法並修改為:

 
  1. + (void)dismiss   
  2. {   
  3. #if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_4_3    
  4.     UIViewController *theViewController = [sharedDialog presentingViewController];   
  5.     [theViewController dismissModalViewControllerAnimated:YES];   
  6. #else    
  7.     UIViewController *theViewController = [sharedDialog parentViewController];   
  8.     [theViewController dismissModalViewControllerAnimated:YES];   
  9. #endif    
  10. }   

這樣編譯出來的程式能在ios5裝置上正確運行,但是在ios5以下的裝置則會crash。因為是庫,所以要考慮到相容不同版本,於是進一步修改為:

 
  1. + (void)dismiss   
  2. {   
  3. #if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_4_3    
  4.     if ([sharedDialog respondsToSelector:@selector(presentingViewController)])    
  5.     {   
  6.         UIViewController *theViewController = [sharedDialog presentingViewController];   
  7.         [theViewController dismissModalViewControllerAnimated:YES];   
  8.     }   
  9.     else   
  10.     {   
  11.         UIViewController *theViewController = [sharedDialog parentViewController];   
  12.         [theViewController dismissModalViewControllerAnimated:YES];   
  13.     }   
  14. #else    
  15.     UIViewController *theViewController = [sharedDialog parentViewController];   
  16.     [theViewController dismissModalViewControllerAnimated:YES];   
  17. #endif    
  18. }   

還有上面那個Console的錯誤提示,解決方案是,在ASIAuthenticationDialog.m中找到-(void)show這個方法,並把最後一行代碼

 
  1. [[self presentingController] presentModalViewController:self animated:YES];   

修改為:

 
  1. UIViewController *theController = [self presentingController];   
  2. #if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_4_3    
  3.     SEL theSelector = NSSelectorFromString(@"presentModalViewController:animated:");   
  4.     NSInvocation *anInvocation = [NSInvocation invocationWithMethodSignature:[[theController class] instanceMethodSignatureForSelector:theSelector]];   
  5.     [anInvocation setSelector:theSelector];   
  6.     [anInvocation setTarget:theController];         
  7.     BOOL               anim = YES;   
  8.     UIViewController   *val = self;   
  9.     [anInvocation setArgument:&val atIndex:2];   
  10.     [anInvocation setArgument:&anim atIndex:3];   
  11.     [anInvocation performSelector:@selector(invoke) withObject:nil afterDelay:1];   
  12. #else          
  13.     [theController presentModalViewController:self animated:YES];   
  14. #endif   

這下就可以正常運行了喲, 我的問題也解決了。關於ASIHttpRequest的其它方面,到目前為止還沒發現問題。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.