iAd在iPhone 2G(以及其他非iOS 4设备)上的应用程序

iAd在iPhone 2G(以及其他非iOS 4设备)上的应用程序

问题描述:

如何获得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://*.com/questions/3128457/are-apps-using-iad-compatible-with-older-ios) – 2011-03-19 22:18:06

正如丹尼尔怀特说的,首先,你必须弱连接到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

你将不得不删除你的条件编译和弱链接到iAd的库。您应该使用标准if块来检查版本。

这里是一个教程,我发现:http://www.vellios.com/2010/07/04/using-ios-4-frameworks-on-os-3/

你与ifdef在做什么,是有条件的编译。