天天看点

ASIHTTPRequest 在xcode4.2下的使用

在Xcode4.2下直接编译的时候,会报一个警告。

Xcode4.2下编译“ASIHTTPRequest”开源项目的代码时,出现“Declaration of 'struct sockaddr_in' will not be visible outside of this function”

在XCode4下没有出现此问题

解决办法:#import <netinet/in.h>

顺便还看见一个问题,先记录了。

引用:http://blog.csdn.net/favormm/article/details/6893465

由于正式版的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的其它方面,到目前为止还没发现问题。

继续阅读