升級到iOS5后ASIHttpRequest庫問題及解決方法
由于正式版的iOS5出來了,所以我也試著去升級了。于是下載了最新的Xcode,才1.7G左右,比以往的安裝包要小許多。
升級Xcode后,打開以前創(chuàng)建的工程, 運氣好,一個錯誤都沒有,程序也能正常跑起來。由于我程序中用了ASIHttpRequest這個庫,讓我發(fā)現(xiàn)了一個小問題,就是
ASIAuthenticationDialog這個內(nèi)置對話框在網(wǎng)絡有代理的情況下出現(xiàn),然后無論點cancle或是login都不能dismiss。在4.3的SDK中完全沒問題,在5.0的SDK中就會在Console中看到輸出:
Unbalanced calls to begin/end appearance transitions for <ASIAutorotatingViewController:>
很明顯示在sdk5中, 用這個庫有問題,還有在停止調(diào)式的時候,程序會有異常產(chǎn)生。
于是很明顯示是SDK5的變化影響了ASIHttpRequest的正常使用。于是我要fix這個問題,經(jīng)過我研究發(fā)現(xiàn),dismiss不起作用是由于UIViewController的parentViewController不再返回正確值了,返回的是nil,而在SDK5中被presentingViewController取代了。于是在ASIAuthenticationDialog.m中找到+(void)dismiss這個方法并修改為:
- + (void)dismiss
- {
- #if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_4_3
- UIViewController *theViewController = [sharedDialog presentingViewController];
- [theViewController dismissModalViewControllerAnimated:YES];
- #else
- UIViewController *theViewController = [sharedDialog parentViewController];
- [theViewController dismissModalViewControllerAnimated:YES];
- #endif
- }
這樣編譯出來的程序能在ios5設備上正確運行,但是在ios5以下的設備則會crash。因為是庫,所以要考慮到兼容不同版本,于是進一步修改為:
- + (void)dismiss
- {
- #if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_4_3
- if ([sharedDialog respondsToSelector:@selector(presentingViewController)])
- {
- UIViewController *theViewController = [sharedDialog presentingViewController];
- [theViewController dismissModalViewControllerAnimated:YES];
- }
- else
- {
- UIViewController *theViewController = [sharedDialog parentViewController];
- [theViewController dismissModalViewControllerAnimated:YES];
- }
- #else
- UIViewController *theViewController = [sharedDialog parentViewController];
- [theViewController dismissModalViewControllerAnimated:YES];
- #endif
- }
還有上面那個Console的錯誤提示,解決方法是,在ASIAuthenticationDialog.m中找到-(void)show這個方法,并把最后一行代碼
- [[self presentingController] presentModalViewController:self animated:YES];
修改為:
- UIViewController *theController = [self presentingController];
- #if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_4_3
- SEL theSelector = NSSelectorFromString(@"presentModalViewController:animated:");
- NSInvocation *anInvocation = [NSInvocation invocationWithMethodSignature:[[theController class] instanceMethodSignatureForSelector:theSelector]];
- [anInvocation setSelector:theSelector];
- [anInvocation setTarget:theController];
- BOOL anim = YES;
- UIViewController *val = self;
- [anInvocation setArgument:&val atIndex:2];
- [anInvocation setArgument:&anim atIndex:3];
- [anInvocation performSelector:@selector(invoke) withObject:nil afterDelay:1];
- #else
- [theController presentModalViewController:self animated:YES];
- #endif
這下就可以正常運行了喲, 我的問題也解決了。關于ASIHttpRequest的其它方面,到目前為止還沒發(fā)現(xiàn)問題。