0

我已经对我的问题进行了一些研究,但没有找到解决方案。我的项目编译成功并启动,但模拟器只显示黑色。在我将谷歌插页式广告添加到我的应用程序之前,我的项目运行良好。以下是我实现这一点的代码:

Appdelegate.h

@interface AppDelegate : UIResponder <UIApplicationDelegate,GADInterstitialDelegate> {

    UIWindow *window_;
    MainViewController *mainController;
    GADInterstitial *splashInterstitial_;
}

@property (nonatomic, retain) UIWindow *window;
@property(nonatomic, retain) IBOutlet MainViewController *mainController;
@property(nonatomic, readonly) NSString *interstitialAdUnitID;

- (GADRequest *)createRequest;

Appdelegate.m

#define INTERSTITIAL_AD_UNIT_ID @""

@synthesize mainController = mainViewController_;
@synthesize window = window_;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

[self.window setRootViewController:mainViewController_];
    [self.window makeKeyAndVisible];

    splashInterstitial_ = [[GADInterstitial alloc] init];

    splashInterstitial_.adUnitID = self.interstitialAdUnitID;
    splashInterstitial_.delegate = self;

    [splashInterstitial_ loadAndDisplayRequest:[self createRequest]
                                   usingWindow:self.window
                                  initialImage:[UIImage imageNamed:@"InitialImage"]];
}

- (void)dealloc {
    splashInterstitial_.delegate = nil;
}

- (NSString *)interstitialAdUnitID {
    return INTERSTITIAL_AD_UNIT_ID;
}


#pragma mark GADRequest generation


- (GADRequest *)createRequest {
    GADRequest *request = [GADRequest request];


    request.testDevices =
    [NSArray arrayWithObjects:

     nil];
    return request;

}

主视图控制器.h

@interface MainViewController : UIViewController <GADInterstitialDelegate> {
    UIButton *interstitialButton_;
    GADInterstitial *interstitial_;
}

@property (nonatomic, retain) GADInterstitial *interstitial;
@property (nonatomic, retain) IBOutlet UIButton *interstitialButton;

- (IBAction)showInterstitial:(id)sender;

主视图控制器.m

@synthesize interstitialButton = interstitialButton_;
@synthesize interstitial = interstitial_;


- (void)interstitial:(GADInterstitial *)interstitial
didFailToReceiveAdWithError:(GADRequestError *)error {
    // Alert the error.
    UIAlertView *alert = [[UIAlertView alloc]
                           initWithTitle:@"GADRequestError"
                           message:[error localizedDescription]
                           delegate:nil cancelButtonTitle:@"Drat"
                           otherButtonTitles:nil] ;
    [alert show];

    interstitialButton_.enabled = YES;
}

- (void)interstitialDidReceiveAd:(GADInterstitial *)interstitial {
    [interstitial presentFromRootViewController:self];
    interstitialButton_.enabled = YES;
}

- (IBAction)showInterstitial:(id)sender {

    self.interstitial = [[GADInterstitial alloc] init] ;
    self.interstitial.delegate = self;


    AppDelegate *appDelegate =
    (AppDelegate *)
    [UIApplication sharedApplication].delegate;
    self.interstitial.adUnitID = appDelegate.interstitialAdUnitID;

    [self.interstitial loadRequest: [appDelegate createRequest]];
    interstitialButton_.enabled = NO;
}
4

0 回答 0