生成IPA包

生成ipa包有以下几种方式:
1、自己手工打包
a、找到xcode生成app包
b、新建文件夹Payload,并把app包放进去
c、(可选)准备一张jpeg格式的图(<500*500px),用来显示在iTunes中 d、(可选)命名这张图为iTunesArtwork(无扩展名),并把它放在和Payload同一级目录中 e、将Payload和iTunesArtwork压缩,后缀名改为ipa即可 2、利用itunes a、找到xcode生成app包 b、直接拖到itunes中 c、在itunes的app目录下找到对应ipa即可(无图片) 3、利用xcode a、生成Archive b、在organizer的Archive界面选择share

打开Led闪光灯

#import <AVFoundation/AVFoundation.h>
-(IBAction)btnClicked:(id)sender
{
    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];  
    if ([device hasTorch])
    {  
        if(AVCaptureTorchModeOn==[device torchMode])
        {
            [btn setTitle:@"Turn On" forState:UIControlStateNormal];
            [device lockForConfiguration:nil];  
            [device setTorchMode: AVCaptureTorchModeOff];  
            [device unlockForConfiguration]; 
        }
        else if(AVCaptureTorchModeOff==[device torchMode])
        {
            [btn setTitle:@"Turn Off" forState:UIControlStateNormal];
            [device lockForConfiguration:nil];  
            [device setTorchMode: AVCaptureTorchModeOn];  
            [device unlockForConfiguration]; 
        }
    }  
}

XCode切换ARC状态

project -> build setting -> Apple LLVM Complier 3.0 – Language -> Objective-C Auto Reference Counting ->
NO 关闭
YES 打开

ARC在编译阶段会产生代码来处理对象引用数,所以启用ARC的话,很多retian和release都不需要了
注意,现在xcode默认启用ARC,关闭的话,要自己补上释放对象的代码哟

在Xcode中部分禁用ARC

大家都清楚ARC(Automatic Reference Counting)是Xcode中很有用的新特色,
ARC会自动检查对象计数,从而在引用数为0时释放对象,让大家的工作轻松了很多
ARC工作在编译阶段,而不是运行阶段

ARC让代码中很多的retain和release变成了不再必要,简化了程序书写
但很多第三方库中,会自行控制对象的释放,经常会有
@property (nonatomic, readwrite, assign) __weak

在ARC工程中引用非ARC的库,会出现下面的编译错误
property attributes ‘assign’ and ‘weak’ are mutually exclusive

这时,只需要修改引用到非ARC库的源码编译参数就可以了
target -> Build Phases -> Compile Sources -> 相应.m文件
增加编译参数 -fno-objc-arc 就好啦

为了简化工作,把interface中使用到第三方库的部分放到.m文件中,或直接用反射,会简化整个过程

另外,如果你发下使用非ARC库时,出现对象不能retain的状况,试试在link时,加上参数

-all_load
-ObjC

IOS Cheat List

1.页面切换

//页面切换
MyViewController *controller = [[MyViewController alloc] initWithNibName:@"MyViewController" bundle:nil];
controller.modalTransitionStyle = UIModalTransitionStyleCoverVertical;

[self presentModalViewController:controller animated:YES];
//页面返回
[self dismissModalViewControllerAnimated:YES];

2.获取AppDelegate

MyAppDelegate *appDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];

3.获取配置信息

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *MyTextValue = [defaults objectForKey:TEXT_VALUE_KEY];

BOOL MyBoolValue = [defaults boolForKey:BOOL_VALUE_KEY];

4.读取plist

//本地
NSString *path = [[NSBundle mainBundle] pathForResource:@"filename" ofType:@"plist"];
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path];
//远程
NSStrng * strURL = [[NSString alloc] initWithFormat:@"FORMAT",.....];
NSLog(@"%@",strURL);
NSURL *plistURL = [NSURL URLWithString:strURL];
NSDictionary *myDict = [[NSDictionary alloc] initWithContentsOfURL:plistURL];

5.关闭键盘

[myControl resignFirstResponder];

6.内置功能调用

//网站
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://网址"]];
//打电话
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel://电话号码"]];
//发送邮件
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"mailto://邮箱地址"]];
//发送短信
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"sms://电话号码"]];

7.导航

//左按钮
UIBarButtonItem *lBtn = [[UIBarButtonItem alloc] initWithTitle:@"名称" style:UIBarButtonItemStylePlain target:self action:@selector(lBtnPressed:)];
self.navigationItem.leftBarButtonItem = lBtn;
 [lBtn release];

//入栈
MyViewController *nextController = [[MyViewController alloc] init];
nextController.title = @"MyViewControllerName";
[self.navigationController pushViewController:nextController animated:YES];

//出栈
[self.navigationController popViewControllerAnimated:YES];

8.返回一个TableViewCell

static NSString *tableViewCellIdentifier = @"MyCellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:tableViewCellIdentifier];
if(cell==nil)
{
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tableViewCellIdentifier] autorelease];
}
cell.textLabel.text = @"TEXT"
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;

9.UIAlertView异步的哟

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"HI" message:@"Hello" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
[alert show];
[alert release];

10.注册推送通知

[[UIApplication sharedApplication] registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert |UIRemoteNotificationTypeSound];

11.注册网络状态

[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(reachabilityChanged:) name: KEY_ATS_NETWORK_CHANGE_NOTIFICATION object: nil];

NSString* token = [deviceToken description];
deviceTokenId = [[[token stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]] stringByReplacingOccurrencesOfString:@" "withString:@""] retain];

PushedMsg *newMsg = [[PushedMsg alloc] init];
newMsg.msgContent = [[[userInfo objectForKey:@"aps"] objectForKey:@"alert"] copy];

12.本地通知

//增加
UILocalNotification *notification=[[UILocalNotification alloc] init];
NSDate *now1=[NSDate date];
notification.timeZone=[NSTimeZone defaultTimeZone];
notification.repeatInterval=NSDayCalendarUnit;
notification.applicationIconBadgeNumber = 1;
notification.alertAction = NSLocalizedString(@"显示", nil);

notification.fireDate=[now1 dateByAddingTimeInterval:10];
notification.alertBody=@"通知";

[notification setSoundName:UILocalNotificationDefaultSoundName];
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
[NSString stringWithFormat:@"%d",myswitch.tag],KEY_ATS_LOCAL_NOTIFICATION, nil];
[notification setUserInfo:dict];
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
//取消
UILocalNotification *myUILocalNotification=[myArray objectAtIndex:i];
if ([[[myUILocalNotification userInfo] objectForKey:KEY_ATS_LOCAL_NOTIFICATION] intValue]==myswitch.tag)
{
[[UIApplication sharedApplication] cancelLocalNotification:myUILocalNotification];
}

13.计算MD5

#import <CommonCrypto/CommonDigest.h>

//生成指定字符串的MD5
+ (NSString*)CalcMD5:(NSString *)InString
{
    //生成MD5
    const char *ptr = [InString UTF8String];
    unsigned char md5Buffer[CC_MD5_DIGEST_LENGTH];
    CC_MD5(ptr, strlen(ptr), md5Buffer);
   
    //转为NSString
    NSMutableString *output = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
    for(int i = 0; i < CC_MD5_DIGEST_LENGTH; i++)
    {
        &#91;output appendFormat:@"%02x",md5Buffer&#91;i&#93;&#93;;
    }
    return output;
}
&#91;/code&#93;

<strong>14.获取位置</strong>
[code lang="objc"]
CLLocationManager* locationManager = [[CLLocationManager alloc]init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];

15.本地化

NSLocalizedString(@"KEY",@"DEFAULT");

16.获取图片

UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentModalViewController:picker animated:YES];
[picker release];

17.加速器

UIAccelerometer *accel = [UIAccelerometer sharedAccelerometer];
accel.delegate =self;
accel.updateInterval = UPDATE_INTERVAL;

18.声音

NSString *path=[[NSBundle mainBundle] pathForResource:@"文件名" ofType:@"wav"];
AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:path], &soundID);

AudioServicesPlaySystemSound(soundID);