当前位置: 代码迷 >> Iphone >> iphone app开发心得(一)
  详细解决方案

iphone app开发心得(一)

热度:98   发布时间:2016-04-25 06:22:18.0
iphone app开发经验(一)
1.在viewDidLoad时对属性的附值要注意在属性前加self,比如[self xxx]=jjj;

2.从文件读取文本 
 NSString *htmlstring=[[NSString alloc] initWithContentsOfFile:filePath  encoding:NSUTF8StringEncoding error:nil];   // encoding:NSUTF8StringEncoding error:nil 这一段一定要加,不然中文字会乱码


3.//枚举判断后缀名是否存在
     NSString *filename;    while (filename = [direnum nextObject]) {        if([[filename pathExtension] hasSuffix:@"jpg"]){            [files addObject:filename];        }    }


4.Debug不一定能马上知道错误在哪,可以先看warnning的提示,大部分问题出在那里。

5.————————修改程序名字———————————–
这个功能太常用了:程序员经常以一个名字开始编写一个应用;但在发布之前,常常需要为应用更名.或者想从一个现成程序开始,修改成一个新的应用,而不需要copy\paste.但苹果公司没有提供一个简单的实现方法.本文讲述的方法可以让你随心所欲修改应用输出的名称.
1.复制现有文件夹,将拷贝更名为你所想要的名字.
2.打开新文件夹,将.xcodeproj更名
3.右键点击.xcodeproj并选择Show package contents,弹出一个对话框包含了几个文件.(事实上.xcodeproj是这几个文件打成的包)
4.用textEdit打开 project.pbxproj文件,将所有旧名字替换成为新的.
5.删除build文件夹
6.用XCode载入该工程,点击 Build/Clean all targets
然后重新编译工程 — 应当能看到输出的应用有了新名字吧!有的文章说需要修改.pch文件名,但那样更复杂了,所以这里不做介绍.


6.如果你不希望应用运行时 iPhone 进入锁屏待机状态,加入下面这行代码即可
 [[UIApplication sharedApplication] setIdleTimerDisabled:YES];


7.Cocoa程序支持多国语言环境

8.调用Safari
  NSURL *url = [[NSURL alloc] initWithString: @"http://www.oreilly.com/" ];  [ [ UIApplication sharedApplication ] openURL: url ]; 


9.挂起与复原
  - (void)applicationWillResignActive:(UIApplication *) application {      NSLog(@"About to be suspended");       /* Code to prepare for suspend */  } - (void)applicationDidBecomeActive:(UIApplication *) application {      NSLog(@"Became active");       /* 为恢复做准备的代码 */  } 


10.发起电话呼叫
  NSURL *url = [ [ NSURL alloc ]      initWithString: @"tel:212-555-1234" ];  [ [ UIApplication sharedApplication ] openURL: url ]; 


11.隐藏toolbar,  点击屏幕又出现回来
   [UIView beginAnimations:@"test" context:NULL];   [UIView setAnimationDuration:0.4];// 这里放代码,减速左右,做到淡入淡出   [UIView commitAnimations];


12.删除subviews
 // With some valid UIView *view:for(UIView *subview in [view subviews]) {    [subview removeFromSuperview];}


13.增加进度条或等待风火轮
 - (void) createProgressionAlertWithMessage:(NSString *)message withActivity:(BOOL)activity{UIAlertView *progressAlert = [[UIAlertView alloc] initWithTitle: message    message: @"Please wait..."  delegate: self  cancelButtonTitle: nil  otherButtonTitles: nil];// Create the progress bar and add it to the alertif (activity) {UIActivityIndicatorView *activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];activityView.frame = CGRectMake(139.0f-18.0f, 80.0f, 37.0f, 37.0f);[progressAlert addSubview:activityView];[activityView startAnimating];} else {UIProgressView *progressView = [[UIProgressView alloc] initWithFrame:CGRectMake(30.0f, 80.0f, 225.0f, 90.0f)];[progressAlert addSubview:progressView];[progressView setProgressViewStyle: UIProgressViewStyleBar];}[progressAlert show];[progressAlert release];}


14.iPhone的特殊URL
在iPhone中,可以直接用UIApp打开URL地址。如下所示:
 [ UIApp openURL: [ NSURL URLWithString:@"http://www.apple.com" ] ];

或者:
 [ UIApp openURL: [ NSURL URLWithString:@"mailto:apple@mac.com?Subject=hello" ] ]; 


与此同时,iPhone还包含一些其他除了http://或者mailto:之外的URL:

sms:// 可以调用短信程序

tel:// 可以拨打电话

itms:// 可以打开MobileStore.app

audio-player-event:// 可以打开iPod

audio-player-event://?uicmd=show-purchased-playlist 可以打开iPod播放列表

video-player-event:// 可以打开iPod中的视频

15.让iAd 在取不到广告时隐藏广告条的方法
相信很多苹果开发者希望在自己的应用里加入 iAd 广告以提高收入,但是 iAd 广告显示目前仅限美国,所以取不到广告时,要让广告条隐藏,可以使用以下代码
 - (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error{if (self.bannerIsVisible)  {      [UIView beginAnimations:@"animateAdBannerOff" context:NULL];// assumes the banner view is at the top of the screen.      banner.frame = CGRectOffset(banner.frame, 0, -50);      [UIView commitAnimations];      self.bannerIsVisible = NO;  }}

如果 iAd 接收到广告,当然要恢复显示广告条了,代码

 - (void)bannerViewDidLoadAd:(ADBannerView *)banner



16.iOS 应用里发 Twitter 的代码
- (void) postToTwitter{  // Since this will be launched in a separate thread, we need  // an autorelease pool  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];   NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:    [NSURL URLWithString:@"http://TWITTER_ACCOUNT:PASSWORD@twitter.com/statuses/update.xml"]     cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30.0];   // The text to post  NSString *msg = @"testing";   // Set the HTTP request method  [request setHTTPMethod:@"POST"];   [request setHTTPBody:[[NSString stringWithFormat:@"status=%@", msg]     dataUsingEncoding:NSASCIIStringEncoding]];   NSURLResponse *response;  NSError *error;   if ([NSURLConnection sendSynchronousRequest:request       returningResponse:&response error:&error] != nil)    NSLog(@"Posted to Twitter successfully.");  else     NSLog(@"Error posting to Twitter.");    // Release pool  [pool release]; }  [NSThread detachNewThreadSelector:@selector(postToTwitter)   toTarget:self withObject:nil];
  相关解决方案