Mars & iPhone 开发 @ 北京 now

Cocoa , Objective-C 编码规范

Posted in obj-c by zmars on 九月 14th, 2010

Apple官方的Cocoa编码规范

http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CodingGuidelines/CodingGuidelines.html

Google的 Objective-c 编码规范

http://google-styleguide.googlecode.com/svn/trunk/objcguide.xml

Error security policy —— 手机上有过期证书导致无法调试

Posted in XCode by zmars on 三月 20th, 2010

今天真机调试程序的时候,发现程序可以安装但就是无法运行起来。
Console 上显示 Error security policy ,查了下是因为手机上安装的一个证书已经过期了,直接删除完后就OK

xcode 3.2 new feature : rename project

Posted in XCode by zmars on 十月 8th, 2009

用3.2有一阵了 ,可是没有认真去看下文档(What’s New in Xcode)

今天偶然翻起,才发现有了个新的 重命名项目的 功能 ,想起当初重命名一个项目时的折腾啊~~。。

XCode->Project->Rename ….

上个图:
screen-shot-2009-10-08-at-123609-pm

Tagged with: ,

哦 。。 我想当然了 。。

Posted in Foundation by zmars on 十月 7th, 2009

看到 UIColor的类方法:
+ (UIColor *)colorWithRed:(CGFloat)red green:(CGFloat)green blue:(CGFloat)blue alpha:(CGFloat)alpha;
代码里有
int nRed ,nGreen,nBlue;
于是直接上:
UIColor *color = [UIColor colorWithRed:nRed/255 green:nGreen/255 blue:nBlue/255 alpha:1];
窃以为方法里的前三个输入参数都是float的,所以 nRed/255 除出来应该是float,可是结果却不对,还以为其它地方有bug.
现在想来,因为 nRed是int ,所以 nRed/255 也会是得到 int 的(对么? ….)
改成:
UIColor *color = [UIColor colorWithRed:(float)nRed/255 green:(float)nGreen/255 blue:(float)nBlue/255 alpha:1];
才对。
;-(

16进制字符转10进制数字

Posted in Foundation by zmars on 十月 7th, 2009

遇到个需要根据 RGB颜色表示如 #FFFF00 字符来得到对应 UIColor 的需求,顺便复习了下 C里stdlib中的转换,拣个说一下:
strtoul(将字符串转换成无符号长整型数)
相关函数
atof,atoi,atol,strtod,strtol
定义函数
unsigned long int strtoul(const char *nptr,char **endptr,int base);
函数说明
strtoul()会将参数nptr字符串根据参数base来转换成无符号的长整型数。参数base范围从2至36,或0。参数base代表采用的进制方 式,如base值为10则采用10进制,若base值为16则采用16进制数等。当base值为0时则是采用10进制做转换,但遇到如’0x’前置字符则 会使用16进制做转换。一开始strtoul()会扫描参数nptr字符串,跳过前面的空格字符串,直到遇上数字或正负符号才开始做转换,再遇到非数字或 字符串结束时(”)结束转换,并将结果返回。若参数endptr不为NULL,则会将遇到不合条件而终止的nptr中的字符指针由endptr返回。

比如:
NSString *s = @”FF”;
unsigned long red = strtoul([s UTF8String],0,16); 就可以了 ;-)

NSTimeZone 时区转换

Posted in Foundation by zmars on 十月 6th, 2009

有的时候为了在系统中统一时间,需要在服务器和客户端统一交换的时间时区,比如都用GMT。
iPhone上转换的代码如下:

NSDate *now = [NSDate date];
NSLog(@”%@”,[now description]);
NSTimeZone *defaultTimeZone = [NSTimeZone defaultTimeZone];
NSTimeZone *tzGMT = [NSTimeZone timeZoneWithName:@"GMT"];
[NSTimeZone setDefaultTimeZone:tzGMT];
NSLog(@”%@”,[now description]); //已经是 GMT表示了
[NSTimeZone setDefaultTimeZone:defaultTimeZone]; // 设置会用户默认的

console输出log:
2009-10-06 16:25:24 +0800
2009-10-06 08:25:24 +0000

Tagged with: , ,

如何让某个ViewController支持屏幕旋转(Rotate)

Posted in UIKit by zmars on 九月 29th, 2009

整个程序需要支持横竖屏切换得时候,会比较简单,在每个ViewController 的

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

方法中,return YES; 就好。

可如果只是要某个VC( = View Controller)支持横竖屏切换呢?单独在那个view controller中像上面那样做是没有效果的。
这个时候我们可以取 UIDevice的 Orientation来判定:
1、在VC中注册 UIDevice 的 UIDeviceOrientationDidChangeNotification 通知:

NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center addObserver:self selector:@selector(doRotate:) name:UIDeviceOrientationDidChangeNotification object:nil];

2、在自己的 doRotate函数里面处理:

- (void)doRotate:(NSNotification *)notification{
//
UIDevice *myDevice = [UIDevice currentDevice];
UIDeviceOrientation deviceOrientation = [myDevice orientation];
UIApplication *app = [UIApplication sharedApplication];
[app setStatusBarOrientation:deviceOrientation];
}

3、- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 也是要 return YES;

这样就可以了 ;-)

改变UIImage大小的简单方法

Posted in UIKit by zmars on 九月 29th, 2009

代码是作为UIImage得 Category 实现得:

- (UIImage *)transformToSize:(CGSize)newSize{
UIGraphicsBeginImageContext(newSize);
[self drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return scaledImage;
}

iPhone URL Schemes

Posted in Foundation by zmars on 九月 28th, 2009

全文来自 http://wiki.akosma.com/IPhone_URL_Schemes
//
1、Safari
Any URL starting with http:// which does not point to maps.google.com or www.youtube.com is sent to Safari:
NSString *stringURL = @”http://wiki.akosma.com/”;
NSURL *url = [NSURL URLWithString:stringURL];
[[UIApplication sharedApplication] openURL:url];

2、Maps
URLs starting with http://maps.google.com open up the “Maps” application automatically:
NSString *title = @”title”;
float latitude = 35.4634;
float longitude = 9.43425;
int zoom = 13;
NSString *stringURL = [NSString stringWithFormat:@"http://maps.google.com/maps?q=%@@%1.6f,%1.6f&z=%d", title, latitude, longitude, zoom];
NSURL *url = [NSURL URLWithString:stringURL];
[[UIApplication sharedApplication] openURL:url];

//

程序中如果只有地址想获得该地址的经纬度,则可以通过google maps http request来得到 ,http://code.google.com/apis/maps/documentation/geocoding/index.html

//

3、Phone
The phone links start with “tel:” but must not contain spaces or brackets (it can contain dashes and “+” signs, though):
NSMutableString *phone = [[@"+ 12 34 567 89 01" mutableCopy] autorelease];
[phone replaceOccurrencesOfString:@" "
withString:@""
options:NSLiteralSearch
range:NSMakeRange(0, [phone length])];
[phone replaceOccurrencesOfString:@"("
withString:@""
options:NSLiteralSearch
range:NSMakeRange(0, [phone length])];
[phone replaceOccurrencesOfString:@")"
withString:@""
options:NSLiteralSearch
range:NSMakeRange(0, [phone length])];
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"tel:%@", phone]];
[[UIApplication sharedApplication] openURL:url];

iPhone 文件读写

Posted in Foundation by zmars on 九月 26th, 2009

对于一个运行在iPhone得app,它只能访问自己根目录下得一些文件(所谓sandbox).

一个app发布到iPhone上后,它得目录结构如下:

iphoneappdir
1、其中得 app root 可以用 NSHomeDirectory() 访问到;
2、Documents 目录就是我们可以用来写入并保存文件得地方,一般可通过:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *documentsDirectory = [paths objectAtIndex:0];

得到。

3、tmp 目录我们可以在里面写入一些程序运行时需要用得数据,里面写入得数据在程序退出后会没有。可以通过

NSString *NSTemporaryDirectory(void); 方法得到;

4、文件一些主要操作可以通过NSFileManage 来操作,可以通过 [NSFileManger defaultManger] 得到它得实例。

相关得一些操作:

创建一个目录:比如要在Documents下面创建一个test目录,

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *documentsDirectory = [paths objectAtIndex:0];

NSLog(@”%@”,documentsDirectory);
NSFileManager *fileManage = [NSFileManager defaultManager];
NSString *myDirectory = [documentsDirectory stringByAppendingPathComponent:@“test”];
BOOL ok = [fileManage createDirectoryAtPath:myDirectory attributes:nil];
取得一个目录下得所有文件名:(如上面的myDirectory)可用
NSArray *file = [fileManager subpathsOfDirectoryAtPath: myDirectory error:nil];

NSArray *files = [fileManager subpathsAtPath: myDirectory ];

读取某个文件:

NSData *data = [fileManger contentsAtPath:myFilePath];//myFilePath是包含完整路径的文件名

或直接用NSData 的类方法:

NSData *data = [NSData dataWithContentOfPath:myFilePath];

保存某个文件:

可以用 NSFileManager的

- (BOOL)createFileAtPath:(NSString *)path contents:(NSData *)data attributes:(NSDictionary *)attr;

或 NSData 的

- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFile;

- (BOOL)writeToFile:(NSString *)path options:(NSUInteger)writeOptionsMask error:(NSError **)errorPtr;