1擷取系統語言設定 NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults]; NSArray *languages = [userDefault objectForKey:@"AppleLanguages"]; NSString *preferredLang = [languages objectAtIndex:0];2緩衝路徑下檔案大小- (unsigned long long int) cacheFolderSize { NSFileManager *_manager = [NSFileManager defaultManager]; NSArray *_cachePaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); NSString *_cacheDirectory = [_cachePaths objectAtIndex:]; NSArray *_cacheFileList; NSEnumerator *_cacheEnumerator; NSString *_cacheFilePath; unsigned long long int _cacheFolderSize = ; _cacheFileList = [ _manager subpathsAtPath:_cacheDirectory]; _cacheEnumerator = [_cacheFileList objectEnumerator]; while (_cacheFilePath = [_cacheEnumerator nextObject]) { NSDictionary *_cacheFileAttributes = [_managerfileAttributesAtPath: [_cacheDirectory stringByAppendingPathComponent:_cacheFilePath] traverseLink:YES]; _cacheFolderSize += [_cacheFileAttributes fileSize]; }// 單位是位元組 return _cacheFolderSize;}3Popover push 時 Frame無法改變解決辦法在popover中的ViewController中實現:- (void)viewWillAppear:(BOOL)animated { CGSize size = CGSizeMake(320, 480); // size of view in popover self.contentSizeForViewInPopover = size; [super viewWillAppear:animated]; }4tableview滑動導致NSTimer和委託回調停止解決辦法/ /請求回調NSURLRequest * 請求 = ...scheduleInRunLoop :[ NSRunLoop currentRunLoop ] forMode :NSRunLoopCommonModes ] [ 串連開始] / /定時器回調NSTimer * updateTimer = [NSTimer scheduledTimerWithTimeInterval:0.01f目標:自我選擇:選擇(updatePencent)的UserInfo:無重複:是];* NSRunLoop主要= [NSRunLoop currentRunLoop] [主要addTimer:updateTimer forMode:NSRunLoopCommonModes];5手勢識別類UIGestureRecognizer6SFHFKeychainUtils 儲存資訊蘋果SDK內建的就有密碼保護,使用方法很簡單,如下:1、引入Security.frameWork架構。2、引入標頭檔:SFHKeychainUtils.h.3、存密碼:[SFHFKeychainUtils storeUsername:@"dd" andPassword:@"aa"forServiceName:SERVICE_NAMEupdateExisting:1 error:nil];[SFHFKeychainUtils deleteItemForUsername:@"dd" andServiceName:SERVICE_NAME error:nil];4、取密碼:NSString *passWord = [SFHFKeychainUtils getPasswordForUsername:@"dd"andServiceName:SERVICE_NAMEerror:nil];7missing required architecture i386 in file 解決辦法在TargetInfo裡面修改 Framework Search Pasths 刪除裡面內容就可以了。8view 放大縮小動畫效果//建立縮小了的視圖myWeiBoImageVC = [[UIViewController alloc] init];myWeiBoImageVC.view.clipsToBounds = YES;myWeiBoImageVC.view.alpha = 0.0;myWeiBoImageVC.view.frame = CGRectMake(64, 0, 1024-64, 768-20);[self.view addSubview:myWeiBoImageVC.view]; CGAffineTransform newTransform = CGAffineTransformScale(myWeiBoImageVC.view.transform, 0.1, 0.1);[myWeiBoImageVC.view setTransform:newTransform];myWeiBoImageVC.view.center = CGPointMake(670, 100); [self performSelector:@selector(imageViewControllerBigAnimation)];//放大剛剛建立縮小後的視圖- (void)imageViewControllerBigAnimation{ [UIView beginAnimations:@"imageViewBig" context:nil]; [UIView setAnimationDuration:0.5]; CGAffineTransform newTransform = CGAffineTransformConcat(myWeiBoImageVC.view.transform, CGAffineTransformInvert(myWeiBoImageVC.view.transform)); [myWeiBoImageVC.view setTransform:newTransform]; myWeiBoImageVC.view.alpha = 1.0; myWeiBoImageVC.view.center = CGPointMake(416, 510); [UIView commitAnimations]; }//縮小視圖 隱藏- (void)imageViewControllerSmallAnimation{ [UIView beginAnimations:@"imageViewSmall" context:nil]; [UIView setAnimationDuration:0.5]; CGAffineTransform newTransform = CGAffineTransformScale(myWeiBoImageVC.view.transform, 0.1, 0.1); [myWeiBoImageVC.view setTransform:newTransform]; myWeiBoImageVC.view.center = CGPointMake(670, 100); [UIView commitAnimations]; }9UIScrollView 控制View縮放allImageScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 768, 1024)];allImageScrollView.minimumZoomScale = 0.3;allImageScrollView.maximumZoomScale = 1.0;allImageScrollView.backgroundColor = [UIColor clearColor];allImageScrollView.delegate = self;[self.view addSubview:allImageScrollView];mPicStatusesViewController = [[PicStatusesViewController alloc] init];[allImageScrollView addSubview:mPicStatusesViewController.view];//UIScrollView Delegete 實現- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{ return mPicStatusesViewController.view; //返回ScrollView上添加的需要縮放的視圖}- (void)scrollViewDidZoom:(UIScrollView *)scrollView{ //縮放操作中被調用}- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale{ //縮放結束後被調用 }10、iOS3.2 播放視頻NSString *urlString = [NSString stringWithString:@"視頻url"];NSURL *movieUrl = [[NSURL alloc] initWithString:urlString]; MPMoviePlayerController *myMoviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:movieUrl];myMoviePlayer.view.frame = CGRectMake(250, 250, 350, 350);[self.view addSubview:myMoviePlayer.view]; myMoviePlayer.shouldAutoplay = YES;myMoviePlayer.scalingMode= MPMovieScalingModeAspectFit; [myMoviePlayer play];11、Google地圖翻起動畫效果 CATransition *animation = [CATransition animation]; [animation setDelegate:self]; [animation setDuration:0.35]; [animation setTimingFunction:UIViewAnimationCurveEaseInOut]; if (!curled){ animation.type = @"pageCurl"; animation.fillMode = kCAFillModeForwards; animation.endProgress = 0.40; } else { animation.type = @"pageUnCurl"; animation.fillMode = kCAFillModeBackwards; animation.startProgress = 0.30; } [animation setRemovedOnCompletion:NO]; [self.view exchangeSubviewAtIndex:0 withSubviewAtIndex:1]; [self.view.layer addAnimation:animation forKey:@"pageCurlAnimation"];12、給View添加陰影 和邊框UIImageView *imgvPhoto = [UIImageView alloc] init];//添加邊框 CALayer *layer = [_imgvPhoto layer]; layer.borderColor = [[UIColor whiteColor] CGColor]; layer.borderWidth = 5.0f;//添加四個邊陰影 _imgvPhoto.layer.shadowColor = [UIColor blackColor].CGColor; _imgvPhoto.layer.shadowOffset = CGSizeMake(0, 0); _imgvPhoto.layer.shadowOpacity = 0.5; _imgvPhoto.layer.shadowRadius = 10.0;//添加兩個邊陰影 _imgvPhoto.layer.shadowColor = [UIColor blackColor].CGColor; _imgvPhoto.layer.shadowOffset = CGSizeMake(4, 4); _imgvPhoto.layer.shadowOpacity = 0.5; _imgvPhoto.layer.shadowRadius = 2.0;13、使用NSTimer與UIView動畫實現飄雪效果viewDidLoad事件中,增加一個圖片及定時器並啟動,這裡的pic請在標頭檔中定義。-(void)viewDidLoad{ [super viewDidLoad]; self.pic = [UIImage imageNamed:@"snow.png"];//初始化圖片 //啟動定時器,實現飄雪效果 [NSTimer scheduledTimerWithTimeInterval:(0.2) target:self selector:@selector(ontime) userInfo:nil repeats:YES];}然後再實現定時器定時調用的ontime方法:-(void)ontime{ UIImageView *view = [[UIImageView alloc] initWithImage:pic];//聲明一個UIImageView對象,用來添加圖片 view.alpha = 0.5;//設定該view的alpha為0.5,半透明的 int x = round(random()20);//隨機得到該圖片的x座標 int y = round(random()20);//這個是該圖片移動的最後座標x軸的 int s = round(random())+10;//這個是定義雪花圖片的大小 int sp = 1/round(random()0)+1;//這個是速度 view.frame = CGRectMake(x, -50, s, s);//雪花開始的大小和位置 [self.view addSubview:view];//添加該view [UIView beginAnimations:nil context:view];//開始動畫 [UIView setAnimationDuration:10*sp];//設定速度 view.frame = CGRectMake(y, 500, s, s);//設定該雪花最後的消失座標 [UIView setAnimationDelegate:self]; [UIView commitAnimations];}14、配置Xcode 看程式崩潰資訊1、在xcode中的左側目錄中找到Executables 開啟2、雙擊和工程名一樣的檔案。3、在開啟的檔案中的Arguments選項,在下面的框中加入Name: NSZombieEnabled 設定value為YES。15、程式中發送郵件和檢測設備信箱是否被配置-(void)addEmail{Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));if (mailClass != nil){ if ([mailClass canSendMail]){ [self displayComposerSheet]; }else{ [self launchMailAppOnDevice]; }}else{ [self launchMailAppOnDevice]; }}-(void)displayComposerSheet{MFMailComposeViewController *controller = [[MFMailComposeViewController alloc] init];controller.navigationBar.tag = 1002;[self.navigationController.navigationBar setNeedsDisplay];controller.mailComposeDelegate = self;[controller setSubject:@"意見反饋"];[controller setToRecipients:[[NSArray alloc] initWithObjects:@"555@cifco.net.cn",nil]];NSString *emailBody = nil;[controller setMessageBody:emailBody isHTML:YES];[self presentModalViewController:controller animated:YES];[controller release];}#pragma mark mailComposeDelegate ----- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {if (result == MFMailComposeResultSent) {[self dismissModalViewControllerAnimated:YES];}if (result == MFMailComposeResultSaved) {[self dismissModalViewControllerAnimated:YES];}if (result == MFMailComposeResultFailed) {Emailalert = [[UIAlertView alloc] initWithTitle:@"" message:@"發送失敗" delegate:selfcancelButtonTitle:@"知道了" otherButtonTitles:nil];[Emailalert show];}if (result == MFMailComposeResultCancelled) {[self dismissModalViewControllerAnimated:YES];}}- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{if(alertView == Emailalert){if (buttonIndex == ) {[self dismissModalViewControllerAnimated:YES];}}else {if (buttonIndex == ) {//[self dismissModalViewControllerAnimated:YES];}else {NSString *recipients = @"mailto:theonelgq@gmail.com?cc=theone_liuguoqing@163.com&subject=text";NSString *body = @"&body=text!";NSString *email = [NSString stringWithFormat:@"%@%@", recipients, body];email = [email stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];[[UIApplication sharedApplication] openURL:[NSURL URLWithString:email]];}}}#pragma mark -#pragma mark Workaround-(void)launchMailAppOnDevice{isEmailalert = [[UIAlertView alloc] initWithTitle:@"警告" message:@"請配置您的郵箱" delegate:selfcancelButtonTitle:@"取消" otherButtonTitles:@"好的",nil];[isEmailalert show];}16、程式啟動畫面大小 iOS裝置現在有三種不同的解析度:iPhone 320x480、iPhone 4 640x960、iPad 768x1024。以前程式的啟動畫面(圖片)只要準備一個 Default.png 就可以了,但是現在變得複雜多了。下面就是 CocoaChina 會員做得總結 如果一個程式,既支援iPhone又支援iPad,那麼它需要包含下面幾個圖片:Default-Portrait.png iPad專用豎向啟動畫面 768x1024或者768x1004Default-Landscape.png iPad專用橫向啟動畫面 1024x768或者1024x748Default-PortraitUpsideDown.png iPad專用豎向啟動畫面(Home按鈕在螢幕上面),可省略 768x1024或者768x1004Default-LandscapeLeft.png iPad專用橫向啟動畫面,可省略 1024x768或者1024x748Default-LandscapeRight.png iPad專用橫向啟動畫面,可省略 1024x768或者1024x748Default.png iPhone預設啟動圖片,如果沒有提供上面幾個iPad專用啟動圖片,則在iPad上運行時也使用Default.png(不推薦) 320x480或者320x460Default@2x.png iPhone4啟動圖片640x960或者640x920 為了在iPad上使用上述的啟動畫面,你還需要在info.plist中加入key: UISupportedInterfaceOrientations。同時,加入值UIInterfaceOrientationPortrait, UIInterfacOrientationPortraitUpsideDown, UIInterfaceOrientationLandscapeLeft, UIInterfaceOrientationLandscapeRight17、ASIHTTPRequest實現斷點下載- (IBAction)URLFetchWithProgress:(id)sender{[startButton setTitle:@"Stop" forState:UIControlStateNormal];[startButton addTarget:self action:@selector(stopURLFetchWithProgress:)forControlEvents:UIControlEventTouchUpInside];NSString*tempFile = [[[[NSBundle mainBundle] bundlePath]stringByDeletingLastPathComponent]stringByAppendingPathComponent:@"MemexTrails_1.0b1.zip.download"];if ([[NSFileManager defaultManager] fileExistsAtPath:tempFile]) {[[NSFileManager defaultManager] removeItemAtPath:tempFile error:nil];}[self resumeURLFetchWithProgress:self];}- (IBAction)stopURLFetchWithProgress:(id)sender{networkQueue = [[ASINetworkQueue alloc] init];timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:selfselector:@selector(updateBandwidthUsageIndicator) userInfo:nil repeats:YES];timer = nil;[startButton setTitle:@"Stop" forState:UIControlStateNormal];[startButton addTarget:self action:@selector(URLFetchWithProgress:)forControlEvents:UIControlEventTouchUpInside];[networkQueue cancelAllOperations];[resumeButton setEnabled:YES];}- (IBAction)resumeURLFetchWithProgress:(id)sender {[resumeButton setEnabled:NO];[startButton setTitle:@"Start" forState:UIControlStateNormal]; [startButton addTarget:self action:@selector(stopURLFetchWithProgress:)forControlEvents:UIControlEventTouchUpInside];[networkQueue cancelAllOperations];[networkQueue setShowAccurateProgress:YES];[networkQueue setDownloadProgressDelegate:progressIndicator];[networkQueue setDelegate:self];[networkQueue setRequestDidFinishSelector:@selector(URLFetchWithProgressComplete:)];ASIHTTPRequest*request=[[[ASIHTTPRequest alloc] initWithURL:[NSURLURLWithString:@"http://9991.net/blog/mp3/2.mp3"]] autorelease];[request setDownloadDestinationPath:[[[[NSBundle mainBundle] bundlePath]stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"MemexTrails_1.0b1.mp3"]];[request setTemporaryFileDownloadPath:[[[[NSBundle mainBundle] bundlePath]stringByDeletingLastPathComponent]stringByAppendingPathComponent:@"MemexTrails_1.0b1.zip.down"]];[request setAllowResumeForFileDownloads:YES];[networkQueue addOperation:request];[networkQueue go];}- (void)URLFetchWithProgressComplete:(ASIHTTPRequest *)request{if ([request error]) {fileLocation.text=[NSString stringWithFormat:@"An error occurred:%@",[[[requesterror] userInfo] objectForKey:@"Title"]];} else {fileLocation.text=[NSString stringWithFormat:@"File downloaded to %@",[requestdownloadDestinationPath]];}[startButton setTitle:@"Start" forState:UIControlStateNormal];[startButton addTarget:self action:@selector(URLFetchWithProgress:)forControlEvents:UIControlEventTouchUpInside];}- (IBAction)throttleBandwidth:(id)sender{if ([(UIButton *)sender state] ==YES) {[ASIHTTPRequest setMaxBandwidthPerSecond:ASIWWANBandwidthThrottleAmount];} else {[ASIHTTPRequest setMaxBandwidthPerSecond:];}}18、Safari 啟動本地app在plist檔案中加入URL types 結構如,在Safari中地址欄輸入 設定的字串,比如設定的是QQ,地址欄輸入 QQ:// 就可以起點本地應用。 19、拖到視頻進度與滑動手勢衝突解決辦法#pragma mark -#pragma mark UIGestureRecognizerDelegate- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{ UIView *touchView = touch.view; if ([touchView isKindOfClass:[UISlider class]]) { return NO; } else { return YES; }}20、建立並儲存Cookie的方法 NSString *cookieString = [NSString stringWithString:[headers objectForKey:@"Cookie"]]; NSMutableDictionary *cookieProperties = [[NSMutableDictionary alloc] init]; [cookieProperties setValue:cookieString forKey:NSHTTPCookieValue]; [cookieProperties setValue:@"QQCookie" forKey:NSHTTPCookieName]; [cookieProperties setValue:@".QQ.com" forKey:NSHTTPCookieDomain]; [cookieProperties setValue:[NSDate dateWithTimeIntervalSinceNow:60*60] forKey:NSHTTPCookieExpires]; [cookieProperties setValue:@"/" forKey:NSHTTPCookiePath]; NSHTTPCookie *newcookie = [[NSHTTPCookie alloc] initWithProperties:cookieProperties]; [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:newcookie];21、popover橫豎屏位置改變解決辦法1、 delegate中 處理- (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController{ userImageShow = NO; if ([popoverController isEqual:myPopover]) { [myPopover release]; myPopover = nil; }}2、旋轉螢幕時重新彈出Popoverif (myPopover) { if ((self.interfaceOrientation == UIInterfaceOrientationLandscapeLeft) || (self.interfaceOrientation == UIInterfaceOrientationLandscapeRight)) { [myPopover presentPopoverFromRect:CGRectMake(10,180, 1, 1) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionRight animated:YES]; } else { [myPopover presentPopoverFromRect:CGRectMake(20,180, 1, 1) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionRight animated:YES]; }}22、plist各種key值含義原文:http://www.minroad.com/?p=434UIRequiresPersistentWiFi 在程式中彈出wifi選擇的key(系統設定中需要將wifi提示開啟)UIAppFonts 內嵌字型(http://www.minroad.com/?p=412 有詳細介紹)UIApplicationExitsOnSuspend 程式是否在後台運行,自己在進入背景時候exit(0)是很傻的辦法UIBackgroundModes 後台運行時的服務,具體看iOS4的後台介紹UIDeviceFamily array類型(1為iPhone和iPod touch裝置,2為iPad)UIFileSharingEnabled 開啟itunes共用document檔案夾UILaunchImageFile 相當於Default.png(更名而已)UIPrerenderedIcon icon上是否有高光UIRequiredDeviceCapabilities 裝置需要的功能(具體點擊這裡查看)UIStatusBarHidden 狀態列隱藏(和程式內的區別是在於顯示Default.png已經生效)UIStatusBarStyle 狀態列類型UIViewEdgeAntialiasing 是否開啟消除鋸齒CFBundleDisplayName app顯示名CFBundleIconFile、CFBundleIconFiles 表徵圖CFBundleName 與CFBundleDisplayName的區別在於這個是短名,16字元之內CFBundleVersion 版本CFBundleURLTypes 自訂url,用於利用url彈回程式CFBundleLocalizations 本地資源的本地化語言,用於itunes頁面左下角顯示本地話語種CFBundleDevelopmentRegion 也是本地化相關,如果使用者所在地沒有相應的語言資源,則用這個key的value來作為預設最後附上官方文檔,所有的key都有,看英文原版才是正路:)點我進入24、xcode工程內添加多個Target