我正在我的应用程序上使用AdMob interstival,并试图使实现更容易。
如果由于某些原因我不应该使用singleton来实现,请告诉我。
AdMob.h公司
#import <Foundation/Foundation.h>
#import "AdsID.h"
@import GoogleMobileAds;
@interface AdMob : NSObject<GADBannerViewDelegate,GADInterstitialDelegate>
@property(nonatomic) GADBannerView *GADRecView; @property(nonatomic, strong) GADInterstitial *interstitial;
+ (AdMob *)sharedIncetance;
-(void)loadAdMobBanner:(UIViewController*)vc view:(UIView*)view x:(int)x y:(int)y;
-(void)loadAdMobInste:(UIViewController*)vc rate:(float)rate;
@end
#import "AdMob.h"
@implementation AdMob
static AdMob *sharedData_ = nil;
+ (AdMob *)sharedInstance{
@synchronized(self){
if (!sharedData_) {
sharedData_ = [[AdMob alloc]init];
}
}
return sharedData_;
}
- (id)init {
self = [super init];
if (self) {
self.interstitial = [self createAndLoadInterstitial];
}
return self;
}
- (GADInterstitial *)createAndLoadInterstitial {
GADInterstitial *interstitial =
[[GADInterstitial alloc] initWithAdUnitID:@"XXXXXX"];
interstitial.delegate = self;
[interstitial loadRequest:[GADRequest request]];
return interstitial;
}
-(void)loadAdMobInste:(UIViewController*)vc{
if ([self.interstitial isReady]) {
[self.interstitial presentFromRootViewController:vc];
}
}
- (void)adViewDidReceiveAd:(GADBannerView *)bannerView {
NSLog(@"Banner adapter class name: %@", bannerView.adNetworkClassName);
}
@end
视图控制器
[[AdMob sharedIncetance] loadAdMobInste:[self btk_parentViewController]];
更新
我可以用Swift实现它,但错误是“其视图不在窗口层次结构中!”因为我做了以下这些,广告没有出现:
-
-
在ViewController中显示间隙
-
在SecondViewController中加载间隙
-
在SecondViewController中显示间隙(ad未显示,出现错误)
如果我使用Objective-C中的代码,则会显示ad,而不会出现错误。
AdMob公司
import Foundation
import GoogleMobileAds
class AdMob : NSObject, GADBannerViewDelegate, GADInterstitialDelegate{
static var sharedData_: AdMob? = nil
var interstitial: GADInterstitial?
class func sharedInstance() -> AdMob? {
let lockQueue = DispatchQueue(label: "self")
lockQueue.sync {
if sharedData_ == nil {
sharedData_ = AdMob()
}
}
return sharedData_
}
override init() {
super.init()
interstitial = createAndLoadInterstitial()
}
func createAndLoadInterstitial() -> GADInterstitial? {
let interstitial = GADInterstitial(adUnitID: "ca-app-pub-3940256099942544/4411468910")
interstitial.delegate = self
interstitial.load(GADRequest())
return interstitial
}
func loadInste(_ vc: UIViewController?) {
if interstitial?.isReady == true {
self.interstitial?.present(fromRootViewController: vc!)
}else{
print("Ad was not realy")
}
}
func interstitialDidDismissScreen(_ ad: GADInterstitial) {
interstitial = createAndLoadInterstitial()
}
func interstitialDidReceiveAd(_ ad: GADInterstitial) {
print("interstitialDidReceiveAd")
}
func interstitial(_ ad: GADInterstitial, didFailToReceiveAdWithError error: GADRequestError) {
print("interstitial:didFailToReceiveAdWithError: \(error.localizedDescription)")
}
func interstitialWillPresentScreen(_ ad: GADInterstitial) {
print("interstitialWillPresentScreen")
}
func interstitialWillDismissScreen(_ ad: GADInterstitial) {
print("interstitialWillDismissScreen")
}
func interstitialWillLeaveApplication(_ ad: GADInterstitial) {
print("interstitialWillLeaveApplication")
}
}
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var navigationController: UINavigationController?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
window = UIWindow(frame: UIScreen.main.bounds)
if let window = window {
let mainVC = ViewController()
navigationController = UINavigationController.init()
navigationController = UINavigationController(rootViewController: mainVC)
navigationController?.navigationBar.isHidden = true
window.rootViewController = navigationController
window.makeKeyAndVisible()
}
return true
}
}
视图控制器
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
AdMob.sharedInstance()?.loadInste(self)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let secondVC = SecondViewController()
self.navigationController?.pushViewController(secondVC, animated: true)
if AdMob.sharedInstance()?.interstitial?.isReady == true {
AdMob.sharedInstance()?.interstitial?.present(fromRootViewController: self)
} else {
print("Ad wasn't ready")
}
}
}
import GoogleMobileAds
class SecondViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.white
AdMob.sharedInstance()?.loadInste(self)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.navigationController?.popViewController(animated: true)
if AdMob.sharedInstance()?.interstitial?.isReady == true {
AdMob.sharedInstance()?.interstitial?.present(fromRootViewController: self)
} else {
print("Ad wasn't ready")
}
}
}