刚刚碰到个很奇怪的问题,iPad发出的网络请求无法获取User-Agent,而使用浏览器去请求就可以正常获取。
iPad中,是使用ASIHttpRequest去发出请求的,NSLog了一下User-Agent,发现其中有中文,因为如果没有指定ASIHttpRequest发出的请求的User-Agent的话,ASIHttpRequest会默认的建立一个User-Agent。
结构是:appName, appVersion, deviceName, OSName, OSVersion, locale
其中appName默认是CFBundleDisplayName,这个在中文程序中一般为中文。所以只要修改这里就可以了。
在ASIHttpRequest.m的第4091行可以看到+ (NSString *)defaultUserAgentString 这个getter,修改其中的代码即可。怎么修改就不提了吧?
今天下午发现,在UITabBar的子显示对象的- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 有点问题,屏幕旋转了,只是调用了UITabBar的shouldAutorotateToInterfaceOrientation,容器内部的显示对象并不会做出反应。那么只能检测屏幕的旋转事件了再做出相应的动作了
// Do any additional setup after loading the view from its nib.
//----- SETUP DEVICE ORIENTATION CHANGE NOTIFICATION -----
UIDevice *device = [UIDevice currentDevice]; //Get the device object
[device beginGeneratingDeviceOrientationNotifications]; //Tell it to start monitoring the accelerometer for orientation
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter]; //Get the notification centre for the app
[nc addObserver:self //Add yourself as an observer
selector:@selector(orientationChanged:)
name:UIDeviceOrientationDidChangeNotification
object:device];
- (void)orientationChanged:(NSNotification *)note
{
UIView *ftView = [self.view viewWithTag:200];
if([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeLeft || [[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeRight)
{
[ftView setFrame:CGRectMake(0, 0, 480, 200)];
}else
{
[ftView setFrame:CGRectMake(0, 0, 320, 360)];
}
}
ref:http://www.ios-developer.net/iphone-ipad-programmer/development/notifications/orientation-change-notification
对于这个问题查了不少地方,有好多种解决方案,后来发现原来在iOS 4.1 之后,Apple 提供了UIKeyboardTypeDecimalPad这种键盘模式。
只要设置UITextField的keyboardType即可。
特别需要注意的是,只适合iOS 4.1之后的版本。
如果要兼容之前的版本,那么请参考:http://blog.devedup.com/index.php/2010/03/13/iphone-number-pad-with-a-decimal-point/
前几天介绍了一下ASIHttpRequest 是神马东东。
今天再来说说,这玩意儿要怎么在项目中使用。
一、从Github(http://github.com/pokeb/asi-http-request/tree)下,把压缩包下过来。然后解压。
二、把Classes里的文件,拖到XCode中,还有把External/Reachbility也拖进去,这是Apple 官方用来检查网络链接是否正常的类库
三、再导入几个框架包。分别是SystemConfiguration.framework, MobileCoreServices.framework, CoreGraphics.framework,CFNetwork.framework和 libz.1.2.3.dylib
四、另外,还要把在头文件搜索路径中加入 libxml2的路径
至此,已经可以再项目中使用 ASIHttpRequest了。
ASIHttpRequest 真的是个objective-c上一个非常不错的网络连接(CFNetwork API)封装库。在几个小型的练手App上用下来,很爽!
ASIHttpRequest 封装了很多的复杂逻辑,让开发者不必纠结于某些具体细节,而注重于业务逻辑处理上。ASIHttpRequest可以被用于Mac OS X 和iOS程序项目中。
ASIHttpRequest 的官网是:http://allseeing-i.com/ASIHTTPRequest/
项目托管在Github 上:http://github.com/pokeb/asi-http-request/tree
基于BSD 开源协议。

