iPhone
如何配置iAd
by Elton on 六.29, 2010, under iPhone
1. 导入iAd.framework
2.选择要定制iAd的TabViewController.h , 添加代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | #import <UIKit/UIKit.h> #import <iAd/ADBannerView.h> @interface TabViewController : UIViewController<ADBannerViewDelegate> { ADBannerView *adView; UILabel *adStatus; } - (void)bannerViewDidLoadAd:(ADBannerView *)banner; - (BOOL)bannerViewActionShouldBegin:(ADBannerView *)banner willLeaveApplication:(BOOL)willLeave; - (void)bannerViewActionDidFinish:(ADBannerView *)banner; - (void)bannerView:(ADBannerView *) didFailToReceiveAdWithError:(NSError *)error; - (void)adAvailabilityDidChange; @property (nonatomic, retain) ADBannerView *adView; @property (nonatomic, retain) UILabel *adStatus; @end |
3.对应的TabViewController.m
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | #import "TabViewController.h" @implementation TabViewController @synthesize adStatus; @synthesize adView; // Implement viewDidLoad to do additional setup after loading the view, typically from a nib. - (void)viewDidLoad { [super viewDidLoad]; adView = [[ADBannerView alloc] initWithFrame:CGRectMake(0, 250, 320, 50)]; self.adView.delegate = self; self.adView.backgroundColor = [UIColor whiteColor]; [self.view addSubview:adView]; adStatus = [[UILabel alloc] initWithFrame:CGRectMake(10, 20, 200, 30)]; [self.view addSubview:adStatus]; NSLog(@"Trying to change the ad status"); } - (void)adAvailabilityDidChange { NSLog(@"[iAd]: Ads are available! Let's display one!"); // if([ADManager sharedAdManager].canPresentModalAd == YES) // [[ADManager sharedAdManager] presentModalAdFromViewController:self]; } - (void)cancelBannerViewAction { NSLog(@"Banner was cancelled!"); self.adStatus.text = @"[iAd]: Bannes was closed."; } - (void)bannerViewDidLoadAd:(ADBannerView *)banner { NSLog(@"[iAd]: Ad did load."); self.adStatus.text = @"[iAd]: Ad did load."; } - (BOOL)bannerViewActionShouldBegin:(ADBannerView *)banner willLeaveApplication:(BOOL)willLeave { NSLog(@"[iAd]: An action was started from the banner. Application will quit: %d", willLeave); self.adStatus.text = @"[iAd]: An action was started from the banner. Application will quit: %d", willLeave; return YES; } - (void)bannerViewActionDidFinish:(ADBannerView *)banner { NSLog(@"[iAd]: Action finished."); self.adStatus.text = @"[iAd]: Action finished."; } - (void)bannerView:(ADBannerView *) didFailToReceiveAdWithError:(NSError *)error { NSLog(@"[iAd]: Faild to load the banner: %@", error); self.adStatus.text = @"[iAd]: Faild to load the banner: %@", error; } - (void)didReceiveMemoryWarning { // Releases the view if it doesn't have a superview. [super didReceiveMemoryWarning]; // Release any cached data, images, etc that aren't in use. } - (void)viewDidUnload { // Release any retained subviews of the main view. // e.g. self.myOutlet = nil; } - (void)dealloc { [super dealloc]; } @end |
理解iPhone项目的BaseSDK和DeploymentTarget含义
by Elton on 六.17, 2010, under iPhone
iPhone OS的版本众多,很多用户由于各种各样的原因没有升级到最新版,这就给我们开发者带了麻烦。作为开发者,我们都希望软件的受众越多越好。怎么样让软件尽量适应最多的iPhone OS?这里我们就应该了解iPhone项目的Base SDK和iPhone OS Deployment Target。
Base SDK指的是当前编译用的SDK版本。iPhone OS Deployment Target指的是编译出的程序将在哪个系统版本上运行。
用更简单实用的语句描述如下:
Base SDK设置为当前xcode所支持的最高的sdk版本,比如”iphone Device 4.0″。iPhone OS Deployment Target设置为你所支持的最低的iPhone OS版本,比如”iPhone OS 3.0″。
这样设置之后,你的程序就可以运行于从iPhone OS 3.0 到 4.0的设备之上。当然,前提是,你没有用到4.0新加的API。
那么如果需要使用到新的API怎么办呢?请参考官方Sample MailComposer http://developer.apple.com/iphone/library/samplecode/MailComposer/index.html
转载自http://www.cnblogs.com/vinceoniphone/archive/2010/06/13/1757743.html
如何在iPhone图标上加提示符(badge)
by Elton on 二.21, 2010, under iPhone
如果你使用Apple iPhone自带的Mail程序,你会注意到,当有新邮件的时候,在这个程序的图标(icon)上会显示对应的新邮件的数字。 如果你的应用程序也想有类似的功能,其实很简单:
1 2 3 4 | - (void)applicationDidFinishLaunching:(UIApplication *)application { //... [application setApplicationIconBadgeNumber:123]; } |
将Array、Dictionary等集合类的序列化和反序列化
by Elton on 二.15, 2010, under iPhone
Objective-C的集合类序列化到文件中或者从文件中反序列化其实很简单,请看下面的示例代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | NSArray *array = [NSArray arrayWithObjects: @"Hefeweizen", @"IPA", @"Pilsner", @"Stout", nil]; NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys: array, @"array", @"Stout", @"dark", @"Hefeweizen", @"wheat", @"IPA", @"hoppy", nil]; // 得到documents directory的路径 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); if ([paths count] > 0) { // Array的保存路径 NSString *arrayPath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"array.out"]; // dictionary的保存路径 NSString *dictPath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"dict.out"]; // 保存array [array writeToFile:arrayPath atomically:YES]; // 保存dictionary [dictionary writeToFile:dictPath atomically:YES]; // 从文件中读取回来 NSArray *arrayFromFile = [NSArray arrayWithContentsOfFile:arrayPath]; NSDictionary *dictFromFile = [NSDictionary dictionaryWithContentsOfFile:dictPath]; for (NSString *element in arrayFromFile) NSLog(@"Beer: %@", element); for (NSString *key in dictFromFile) NSLog(@"%@ Style: %@", key, [dictionary valueForKey:key]); } |
输出如下:

创建没有按钮的UIAlertView
by Elton on 二.11, 2010, under iPhone
默认的,UIAlertView都有一个或者多个按钮,如果你想创建没有按钮的UIAlertView,可以使用以下的方法:
1 2 3 4 5 6 7 8 | UIAlertView *alert; ... alert = [[[UIAlertView alloc] initWithTitle:@"Configuring PreferencesnPlease Wait..." message:nil delegate:self cancelButtonTitle:nil otherButtonTitles: nil] autorelease]; [alert show]; |

上面的标题可能有些靠上,可以用过给标题增加回车的方式,使标题居中:
1 2 3 4 5 6 7 8 | UIAlertView *alert; ... alert = [[[UIAlertView alloc] initWithTitle:@"nnConfiguring PreferencesnPlease Wait..." message:nil delegate:self cancelButtonTitle:nil otherButtonTitles: nil] autorelease]; [alert show]; |

如果你还想给UIAlertView添加一个等待提示符,则可以这么做:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | UIAlertView *alert; ... alert = [[[UIAlertView alloc] initWithTitle:@"Configuring PreferencesnPlease Wait..." message:nil delegate:self cancelButtonTitle:nil otherButtonTitles: nil] autorelease]; [alert show]; UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge]; // Adjust the indicator so it is up a few pixels from the bottom of the alert indicator.center = CGPointMake(alert.bounds.size.width / 2, alert.bounds.size.height - 50); [indicator startAnimating]; [alert addSubview:indicator]; [indicator release]; |

解除UIAlertView的显示
因为这个AlertView没有按钮,所以就不能通过点击按钮将这个提示框去掉。可以通过程序的方式,将这个窗口关掉。
[alert dismissWithClickedButtonIndex:0 animated:YES];
Three20中给TTPhotoViewController添加播放按钮
by Elton on 二.06, 2010, under iPhone
Three20是一个很不错的iPhone类库,是facebook共享出来的框架。利用TTThumbsViewController可以很容易的制作出类似flickr的应用,实现缩略图和图片查看功能。
但是默认的TTThumbsViewController,点击一个图片的时候,下面的button只有上一个和下一个,并没有播放按钮。 之前以为Three20没有相应的播放功能。但是经过查看代码,发现在TTPhotoViewController中,其实有相关的方法,只是没有把播放按钮显示出来而已。
1 2 3 | UIBarButtonItem* playButton = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemPlay target:self action:@selector(playAction)] autorelease]; playButton.tag = 1; |
通过修改TTPhotoViewController大致在400行左右,添加playButton这个变量
1 2 | _toolbar.items = [NSArray arrayWithObjects: space, _previousButton, space, playButton, space, _nextButton, space, nil]; |
这样重新编译后,就可以看到播放按钮了。
Three20这个库其实还是很实用的,只是文档比较欠缺,没办法,只能直接看源码了。
给TableView加背景
by Elton on 二.02, 2010, under iPhone
iPhone默认的表格背景很单调,你可以通过加背景图片的方式来给你的应用增添点亮点。可以通过以下方法来给表格添加背景。
你可以在你的delegate类里面添加如下代码:
1 2 3 4 5 6 7 8 9 10 | UIView *backgroundView = [[UIView alloc] initWithFrame: window.frame]; backgroundView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"TableViewBackground.png"]]; [window addSubview:backgroundView]; [backgroundView release]; yourTableViewController = [[ATableViewController alloc] initWithStyle:UITableViewStyleGrouped]; yourTableViewController.view.backgroundColor = [UIColor clearColor]; [window addSubview:yourTableViewController.view]; [window makeKeyAndVisible]; |
ATableViewController是UITableViewController的一个子类

去掉iPhone应用图标上的弧形高光
by Elton on 一.25, 2010, under iPhone
有时候我们的应用程序不需要在图标上加上默认的高光,可以在你的应用的Info.plist中加入:
1 | UIPrerenderedIcon,让它等于YES即可 |
让你的iPhone应用的URL更加友好易记
by Elton on 一.25, 2010, under iPhone
在你的应用程序上线后,经常要做对外推广的工作,经常会把你的应用程序的url贴在各处。 默认的iTunes链接的样子如下:
1 | http://itunes.apple.com/us/app/china-charming-girl/id348850577?mt=8 |
比较长,而且对搜索引擎不是很友好。 其实iTunes的url可以有很三种方式:
公司名字
http://itunes.com/apps/ (例如, http://itunes.com/apps/prosight)
应用程序名字
http://itunes.com/apps/ (例如, http://itunes.com/apps/crazyfart)
应用程序名加公司名
http://itunes.com/apps// (例如, http://itunes.com/apps/prosight/crazyfart)
生成这个url有如下规则:
- 移除所有的空格
- 移除所有的copyright (©), trademark (™) 和registered mark (®) 符号
- 替换”&” 成 “and”
- 移除所有非法字符(见下)
- 替换所有非英语字符(ü, å, etc.) 为英文字符 (u, a, etc.)
- 保留所有其他的字符
非法字符:
1 | !¡"#$%'()*+,-./:;<=>¿?@[]^_`{|}~ |
使用Google Analytics跟踪你的手机应用
by Elton on 一.09, 2010, under iPhone
大家都知道Google Analytics可以跟踪网站的浏览情况。 其实Google Analytics提供了一个iPhone的本地类库,也可以帮助你跟踪你的手机应用,帮助你分析你的手机应用的用户使用习惯。
加入Google Analytics的方法很简单:
- 在这里下载类库。(在页面下面Supported Development Environments部分有下载链接)
- 加入libGoogleAnalytics.a静态类库和GANTracker.h头文件到你的项目中。
- 加入Google Analytics需要的其他类库(CFNetwork framework和libsqlite3.0.dylib。
- 在你的项目中加入几行初始化代码,然后添加跟踪页面或者事件的代码。见下面的示例程序。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | #import "BasicExampleAppDelegate.h" #import "GANTracker.h" // Dispatch period in seconds static const NSInteger kGANDispatchPeriodSec = 10; @implementation BasicExampleAppDelegate @synthesize window = window_; - (void)applicationDidFinishLaunching:(UIApplication *)application { // ************************************************************************** // PLEASE REPLACE WITH YOUR ACCOUNT DETAILS. // ************************************************************************** [[GANTracker sharedTracker] startTrackerWithAccountID:@"UA-0000000-1" dispatchPeriod:kGANDispatchPeriodSec delegate:nil]; NSError *error; if (![[GANTracker sharedTracker] trackEvent:@"my_category" action:@"my_action" label:@"my_label" value:-1 withError:&error]) { // Handle error here } if (![[GANTracker sharedTracker] trackPageview:@"/app_entry_point" withError:&error]) { // Handle error here } [window_ makeKeyAndVisible]; } - (void)dealloc { [[GANTracker sharedTracker] stopTracker]; [window_ release]; [super dealloc]; } @end |
Google Analytics的统计也支持Andorid平台,详情请参考这里。
当然因为这个类库目前只是0.7版本,可能还不够稳定。 类似的类库还有Pinch Media, Medialets, Mobclix,你可以进行参考。


