在iPhone程序中集成iAd廣告
在iPhone程序中集成iAd廣告 是本文要介紹的內(nèi)容,iAd的推出無(wú)疑給Iphone/IPad的應(yīng)用程序開(kāi)發(fā)者打開(kāi)了另一條創(chuàng)收之門,前不久,美國(guó)的一位開(kāi)發(fā)者Json Ting開(kāi)發(fā)的將相機(jī)閃光燈轉(zhuǎn)為手電筒的應(yīng)用,集成iAd后在***天就給他帶來(lái)了1400$的廣告收入。我將在這篇文章中講講如何把iAd集成到你的應(yīng)用程序中。另外也會(huì)提到集成中可能遇到的一些問(wèn)題:
如何支持橫屏跟豎屏。
如何保持與os 3.0的后向兼容。
與UITableViewController的集成。
1、將Base SDK設(shè)為4.0, 將Deployment target設(shè)為3.0. 如圖所示:
2、鏈接iAd Framework.
右擊Frameworks, 選擇"Add\Existing Frameworks", 添加"iAd.framework". 但是在沒(méi)有iAd.framework的機(jī)器上,比如3.x版本的,這樣會(huì)Crash.所以要把這個(gè)鏈接變?yōu)閣eak link. 在"targets"中右擊你的工程,然后"Get Info", 在Linking\Other Linker Flags里添加"-weak_framework iAd". 這樣就能夠保證程序的后向兼容性。
3、在XIB中加入你的UI
可以考慮把其它功能性的UI加在一個(gè)父親UIView,后面把iAd跟這個(gè)父親UIView作為同一級(jí),這樣iAd顯示時(shí)不會(huì)影響原有UI。
4、與UIViewController的集成
(1)獲取iAd Banner大小的幫助函數(shù)(見(jiàn)示例).
(2)創(chuàng)建iAd Banner的函數(shù).
- - (void)createAdBannerView
- {
- Class classAdBannerView = NSClassFromString(@"ADBannerView");
- if (classAdBannerView != nil)
- { ...
- }
- }
這個(gè)地方使用NSClassFromString 能夠保證代碼的后向兼容性,在os 3.x的系統(tǒng)上這個(gè)函數(shù)不會(huì)成功,iAd不會(huì)顯示,但是程序仍然能夠運(yùn)行.
(4) 調(diào)整功能性UI及iAd Banner的位置的函數(shù)。(見(jiàn)代碼示例 fixupAdView)
(5)在合適時(shí)機(jī)創(chuàng)建和調(diào)整iAd Banner位置。
- - (void)viewDidLoad
- {
- [self createAdBannerView];
- }
- - (void) viewWillAppear:(BOOL)animated
- {
- [self refresh];
- [self fixupAdView:[UIDevice currentDevice].orientation];
- }
- - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
- duration:(NSTimeInterval)duration
- {
- [self fixupAdView:toInterfaceOrientation];
- }
- 4.5 實(shí)現(xiàn)ADBannerViewDelegate
- - (void)bannerViewDidLoadAd:(ADBannerView *)banner
- {
- if (!_adBannerViewIsVisible)
- {
- _adBannerViewIsVisible = YES;
- [self fixupAdView:[UIDevice currentDevice].orientation];
- }
- }
- - (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
- {
- if (_adBannerViewIsVisible)
- {
- _adBannerViewIsVisible = NO;
- [self fixupAdView:[UIDevice currentDevice].orientation];
- }
- }
5、 與UITableViewController的集成(更新中)
小結(jié):在IPhone程序中集成iAd廣告 的內(nèi)容介紹完了,希望本文對(duì)你有所幫助。
本文來(lái)自:http://www.cnblogs.com/MobileDevelop/archive/2010/07/17/1779133.html