plist保存问题

问题描述
今天做Denizen Box,需要收集用户资料,用[NSDictionary writeToFile: atomically:]方法保存。模拟上此函数成功,返回true;真机上此函数失败,返回false。

问题分析
代码如下:

NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"data" ofType:@"plist"];
bool result=[addDictionary writeToFile:plistPath atomically:YES];

代码没有变化,而真机就是写入不了,猜想是权限问题。

问题解决
把代码改为:

NSArray *storeFilePath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *doucumentsDirectiory = [storeFilePath objectAtIndex:0];
NSString *plistPath=[doucumentsDirectiory stringByAppendingPathComponent:@"data.plist"];
bool result=[addDictionary writeToFile:plistPath atomically:YES];

问题解决了。

事后发现,路径不一样:
修改前:

/var/mobile/Applications/A528A4DE-F391-4247-8C5D-9386DC415A00/Box.app/data.plist

修改后:

/var/mobile/Applications/A528A4DE-F391-4247-8C5D-9386DC415A00/Documents/data.plist

不同SDK对pathForResource函数兼容问题

问题描述
前些天做AD iPad版,程序在4.2版本的iPad上运行良好,但是安装到3.2版本的iPad上有些功能点load不出。

问题分析
设置断点,一步一步跟踪,查找到:

// init position
NSString *plistPath = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%d/feature_point",[self getPage]] ofType:@"plist"];
_positions=[[NSDictionary alloc] initWithContentsOfFile:plistPath];

以上代码plistPath值为空,查找这句中所有的语句的SDK支持,都只要SDK2.0,而我们是3.2,完全符合。

问题解决
一步一步排除,最后焦点在pathForResource: ofType:函数,查找SDK的时候发现另有一个函数是pathForResource: ofType: inDirectory:函数,把该句改为:

NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"feature_point" ofType:@"plist" inDirectory:[NSString stringWithFormat:@"%d",[self getPage]]];

问题解决了!!!

Project Settings的设置bug

问题描述
帮客户的iPad安装AD demo,机器SDK是4.2.1,客户iPad SDK是3.2,直接developer debug不成功。

问题分析
把“Project Settings”的"Build"选项卡里的"Base SDK"设置为"Latest iOS"(最高),并把"iOS Deployment Target"设置为"iOS 3.0"(最低)。

问题解决
网上查了下,看到一老外的解决办法,如下:

I believe the only requirement is that you edit and resave the info.plist file. I editted to 1.7, saved, and then back to 1.0 and it worked fine.

It may be that XCode is binding in additional details from the build settings at the time of packaging and since the info.plist hadn't changed those additional bindings did not get attached. If this is the case I would consider it a bug.

我也认为是一个iOS的Bug,只有修改info.plist才会导致project settings设置生效。

UIImageView frame设置顺序问题

问题描述:
今天做AD ipad版展示功能点,背景不显示。

问题分析:
检查代码,其他都没什么问题。发现设置frame在设置image之前。

代码如下:

UIImage *backgroundImage=[UIImage imageNamed:BACKGROUND_FILE_NAME];
_backgroundImageView=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, backgroundImage.size.width, backgroundImage.size.height)];
_backgroundImageView.image=backgroundImage;

问题解决:
把设置frame代码放置在设置frame之后,一切安逸了。

代码如下:

_backgroundImageView=[[UIImageView alloc] init];
UIImage *backgroundImage=[UIImage imageNamed:BACKGROUND_FILE_NAME];
_backgroundImageView.image=backgroundImage;
_backgroundImageView.frame=CGRectMake(0, 0, backgroundImage.size.width, backgroundImage.size.height);

UIButton click不触发问题

问题描述:
今天做AD的ipad版展示功能点,为UIButton加入点击事件后,没有调用效果。

问题分析:
代码如下:

[button addTarget:self action:@selector(groupClicked:) forControlEvents:UIControlEventTouchUpInside];

- (void)objectClicked:(id)vSender
{
}

objectClicked方法没有被调用。查了下,addSubView等都没问题。突然想到父类是UIView,frame没有设定过。

问题解决:
设定frame:self.frame=CGRectMake(0,0,768,961);

一切安逸了。

不同iOS SDK对事件调用支持不同

问题描述:

今天做AD杂志(iPad)LEGO功能点,需要在UIScrollView中让UIView移动。加了正确代码,结果却不好。

结果是这样的:点击不移动,运行touchesBegan和touchesEnded,一切正常。但是点下移动释放,touchesBegan会100%运行,然后三两个touchesMoved,move效果也跟不上,touchesEnded也没有触发。

在UIView用了如下代码实现:

#pragma mark --

#pragma mark touch

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

NSLog(@"touchesBegan");

UITouch *touch = [touches anyObject];

UIScrollView *parentView = (UIScrollView *)[[[touch view] superview]superview];

parentView.scrollEnabled=NO;

// Calculate and store offset, and pop view into front if needed

CGPoint pt = [[touches anyObject] locationInView:self];

startLocation = pt;

}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event

{

NSLog(@"touchesMoved");

// Calculate offset

CGPoint pt = [[touches anyObject] locationInView:self];

float dx = pt.x - startLocation.x;

float dy = pt.y - startLocation.y;

CGPoint newcenter = CGPointMake(self.center.x + dx, self.center.y + dy);

// Bound movement into parent bounds

float halfx = CGRectGetMidX(self.bounds);

newcenter.x = MAX(halfx, newcenter.x);

newcenter.x = MIN(self.superview.bounds.size.width - halfx, newcenter.x);

float halfy = CGRectGetMidY(self.bounds);

newcenter.y = MAX(halfy, newcenter.y);

newcenter.y = MIN(self.superview.bounds.size.height - halfy, newcenter.y);

// Set new location

self.center = newcenter;

}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event

{

NSLog(@"touchesEnded");

UITouch *touch = [touches anyObject];

UIScrollView *parentView = (UIScrollView *)[[[touch view] superview]superview];

parentView.scrollEnabled=YES;

}

问题分析:

最后一段一段检查,应该是parentView.scrollEnabled=NO;这句有问题。

可能是iOS的一个Bug。

问题解决:

在另一台Mac上做了一个Demo,一切OK。细细检查,唯一不同就是SDK版本不一样。出问题的那台是4.1,而做Demo一切OK的那台是4.2.1。把出问题的代码copy给4.2.1,一切安逸了。