2011-03-19 51 views
0

如何獲得iAds能夠在iPhone 2G上運行的應用程序。我知道這個設備不支持i​​Ads,因此我希望代碼不被執行。iAd在iPhone 2G(以及其他非iOS 4設備)上的應用程序

這是我的一些代碼。 .h文件:

#import <UIKit/UIKit.h> 
#ifdef __IPHONE_4_0 
#import <iAd/iAd.h> 
#endif 

@interface ViewController : UIViewController 
#ifdef __IPHONE_4_0 
<ADBannerViewDelegate> 
#endif 
{ 
#ifdef __IPHONE_4_0 
    ADBannerView *adView; 
#endif 
... 

在.m文件我都iAd的周圍來編碼,像這樣:

#ifdef __IPHONE_4_0  //iAd Code 
    adView = [[ADBannerView alloc] initWithFrame:CGRectZero]; 
    adView.frame = CGRectOffset(adView.frame, 0, 500); 
    adView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierPortrait; 
    [self.view addSubview:adView]; 
    adView.delegate = self; 
    self.bannerIsVisible = NO; 
#endif 

但是我仍然得到一個錯誤。該應用程序總是崩潰,只會顯示啓動圖像。我得到一個彈出窗口,說:「啓動可執行程序的錯誤'應用程序'啓動遠程程序時出錯:無法獲取進程411的任務。」這裏是控制檯:

GNU gdb 6.3.50-20050815 (Apple version gdb-1518) (Thu Jan 27 08:40:30 UTC 2011) 
Copyright 2004 Free Software Foundation, Inc. 
GDB is free software, covered by the GNU General Public License, and you are 
welcome to change it and/or distribute copies of it under certain conditions. 
Type "show copying" to see the conditions. 
There is absolutely no warranty for GDB. Type "show warranty" for details. 
This GDB was configured as "--host=x86_64-apple-darwin --target=arm-apple-darwin".tty /dev/ttys000 
warning: Unable to read symbols from "iAd" (not yet mapped into memory). 
target remote-mobile /tmp/.XcodeGDBRemote-6877-41 
Switching to remote-macosx protocol 
mem 0x1000 0x3fffffff cache 
mem 0x40000000 0xffffffff none 
mem 0x00000000 0x0fff none 
+0

可能的[使用iAd的應用程序與舊iOS兼容]的副本(http://stackoverflow.com/questions/3128457/are-apps-using-iad-compatible-with-older-ios) – 2011-03-19 22:18:06

回答

1

正如丹尼爾懷特說的,首先,你必須弱連接到iAd庫(這樣,如果操作系統沒有它,庫不會在運行時鏈接到你的應用程序)。爲此,請在Xcode中的目標中展開您的應用程序名稱下的樹,然後單擊Link Binary With Libraries項目。然後,您應該能夠找到iAd.framework並將其角色從「必需」更改爲「弱」。

然後,在您的代碼中,您需要執行運行時(而不是編譯時,正如您現在所做的那樣)檢查iAd框架。例如,

// Add in iAd banner, if we are running on 4.0+ 
Class adBannerClass = NSClassFromString(@"ADBannerView"); 
if (adBannerClass != nil) 
{ 
     adView = .... 

這樣相同的代碼將執行在iOS 4.0+設備和iOS 3.2及更早版本的設備就好了。

+0

是否需要圍繞.h文件中的任何代碼? (導入行,'','ADBannerView * adView;')或.m文件中的其他內容? ('bannerViewDidLoadAd'方法,失敗的方法和'adView.delegate = nil;''[adView release];'dealloc方法中的代碼)謝謝 – michaellindahl 2011-03-21 02:24:19

+0

@michaellindahl:你需要圍繞任何引用'ADBannerView'類或它的一個實例,在你的.m(以及任何iAD相關的常量等)中。但是,如果在.h文件中聲明'adView'只是一個'UIView'(而不是'ADBannerView'),則可以避免必須在dealloc中包含代碼(但仍然可以將IBOutlet在IB中)。由於''實際上只是一個編譯時的事情,所以你根本不用擔心。 – 2011-03-21 04:36:41

相關問題