creator已经可以跑Xcode 的方法,广告也试跑成功
不过,就是卡在static method这里
想问问大家是怎么设置这static 方法来触发广告的
我把static method写在AppDelegate, 然后把广告设置在viewcontroller跑
初学者,请求大神帮忙
creator已经可以跑Xcode 的方法,广告也试跑成功
不过,就是卡在static method这里
想问问大家是怎么设置这static 方法来触发广告的
我把static method写在AppDelegate, 然后把广告设置在viewcontroller跑
初学者,请求大神帮忙
你好,我已经成功从creator执行方法到xcode了
我觉得我现在最大的问题是:
(void)loadRewardedAd {
GADRequest *request = [GADRequest request];
[GADRewardedAd
loadWithAdUnitID:@“ca-app-pub-3940256099942544/1712485313”
request:request
completionHandler:^(GADRewardedAd *ad, NSError *error) {
if (error) {
NSLog(@“Rewarded ad failed to load with error: %@”, [error localizedDescription]);
return;
}
self.rewardedAd = ad;
NSLog(@“Rewarded ad loaded.”);
//self.rewardedAd.fullScreenContentDelegate = self;
}];
}
(void)show {
if (self.rewardedAd) {
[self.rewardedAd presentFromRootViewController:self
userDidEarnRewardHandler:^{
GADAdReward *reward =
self.rewardedAd.adReward;
// TODO: Reward the user!
}];
} else {
NSLog(@“Ad wasn’t ready”);
}
在loadRewardedAd 的方法里,我已经分配self.rewardedAd, 但是在show的方法里面, self.rewarded永远的(null)
去年写的,希望能帮得上忙
#pragma mark - RewardAd
(void)loadRewardedAd {
GADRequest *request = [GADRequest request];
[GADRewardedAd
loadWithAdUnitID:kAdUnitIDReward
request:request
completionHandler:^(GADRewardedAd *ad, NSError *error) {
if (error) {
NSLog(@“Rewarded ad failed to load with error: %@”, [error localizedDescription]);
return;
}
self.rewardedAd = ad;
NSLog(@“Rewarded ad loaded.”);
self.rewardedAd.fullScreenContentDelegate = self;
}];
}
(void)showRewardAd;
{
if (self.rewardedAd) {
UIViewController *topRootViewController = [[UIApplication sharedApplication] keyWindow].rootViewController;
//设置当前的conroller为置顶controller
while (topRootViewController.presentedViewController)
{
topRootViewController = topRootViewController.presentedViewController;
}
[self.rewardedAd presentFromRootViewController:topRootViewController userDidEarnRewardHandler:^{
// TODO: Reward the user!
[self rewardCallBack:true];
NSLog(@"获得奖励");
}];
} else {
NSLog(@“Ad wasn’t ready”);
}
}
请问你怎么去执行showRewardAd
我是这样的
+(void)showAds:(NSString *) args2 args1:(NSString *)content{
ViewController *vc = [[ViewController alloc] init];
[vc loadRewardedAd];
}
可是我的self.rewardedAd 总是给我(null)
看你代码没问题,确认下账户是否正常(试试banner或者插屏广告能不能拉)或者试着用测试广告位拉取一下呢,断点看一下走没走到加载成功的回调,或者失败有没有抛出失败原因。我这边调用是用c++封装的瀑布流,ts调c++调oc,不方便贴了,加载和展示是这样没问题,现在还在苹果商店正常使用
有什么办法可以保留到self.rewardedAd的value吗?我从loadRewardedAd拿得到value,可是去到showRewardAd 我的self.rewardedAd就变成(null)了,感觉是我的代码出现了错误
这个类做成单例试试呢,我是存在了self里,如果load没问题查查代码吧,类有没有重复初始化之类的
以上两个方法loadRewardedAd, showRewardAd是写在ViewController
然后在AppDelegate.h 是这样的
#pragma once
#import “platform/ios/AppDelegateBridge.h”
#import <UIKit/UIKit.h>
@class ViewController;
@interface AppDelegate : NSObject {
}
+( void )showAds:(NSString *)args2 args1:(NSString *)content; //cocos按键传过来的方法
@property ( nonatomic , readonly ) ViewController *viewController;
@property ( nonatomic , readonly ) AppDelegateBridge *appDelegateBridge;
@end
我的AppDelegate.m
#import “AppDelegate.h”
#import “ViewController.h”
#import “View.h”
#include “platform/ios/IOSPlatform.h”
#import “platform/ios/AppDelegateBridge.h”
#import “service/SDKWrapper.h”
#import <GoogleMobileAds/GoogleMobileAds.h>
@implementation AppDelegate
@synthesize window;
@synthesize appDelegateBridge;
#pragma mark -
#pragma mark Application lifecycle
(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[[SDKWrapper shared] application:application didFinishLaunchingWithOptions:launchOptions];
appDelegateBridge = [[AppDelegateBridge alloc] init];
// Add the view controller’s view to the window and display.
CGRect bounds = [[UIScreen mainScreen] bounds];
self.window = [[UIWindow alloc] initWithFrame:bounds];
// Should create view controller first, cc::Application will use it.
_viewController = [[ViewController alloc] init];
_viewController.view = [[View alloc] initWithFrame:bounds];
_viewController.view.contentScaleFactor = UIScreen.mainScreen.scale;
_viewController.view.multipleTouchEnabled = true;
[self.window setRootViewController:_viewController];
[self.window makeKeyAndVisible];
[appDelegateBridge application:application didFinishLaunchingWithOptions:launchOptions];
[GADMobileAds.sharedInstance startWithCompletionHandler:nil];
GADMobileAds.sharedInstance.requestConfiguration.testDeviceIdentifiers = @[ GADSimulatorID ];
ViewController *viewController = (ViewController *)self.window.rootViewController;
[viewController loadRewardedAd];
return YES;
}
+(void)showAds:(NSString *) args2 args1:(NSString *)content{
ViewController *vc = [[ViewController alloc] init];
[vc show];
//NSLog(@"@ @ %@", content,args2,[vc rewardedAd]);
}
@end
AppDelegate.m 主要看 showAds这个方法是连接ViewController的方法的
求帮忙,我已经绝望了
新起个类吧,别在代理类写看起来load和show的对象不一致导致的,这是我这个单例类的源码了
//
// AdmobHelper.hpp
//
// Created by Versperchen on 2021/3/26.
//
#import <Foundation/Foundation.h>
#import <GoogleMobileAds/GoogleMobileAds.h>
@interface AdmobHelper : UIViewController
typedef void(^RewardStatue)(int RewardStatue);
@property (nonatomic,copy)RewardStatue RewardStatue;
///Inter
(void)loadInterAd;
(void)showInterAd;
///Reward
(void)loadRewardedAd;
(void)showRewardAd;
///Banner
(void)addBanner:(UIView *)parentView size:(GADAdSize)adSize controller:(UIViewController *)controller;
(void)hideBanner;
(void)showBanner;
-(void)rewardCallBack:(BOOL) statue;
-(void)stopAnimation;
-(void)startAnimation;
@end
//
// AdmobHelper.cpp
//
//
// Created by Versperchen on 2021/3/26.
//
#import “AdmobHelper.h”
static AdmobHelper *helper = nil;
//typedef void(^RewardStatue)(int loginStatue);
@interface AdmobHelper ()
@property(nonatomic,strong) GADBannerView *mBannerView;
@property(nonatomic,strong) GADInterstitialAd *interstitial;
@property(nonatomic, strong) GADRewardedAd *rewardedAd;
@end
@implementation AdmobHelper
(AdmobHelper *)instance
{
if(helper == nil) {
helper = [[AdmobHelper alloc] init];
}
return helper;
}
(void)viewDidLoad{
[super viewDidLoad];
NSLog(@“load”);
}
(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
}
#pragma mark - bannerAd
(void)addBanner:(UIView *)parentView size:(GADAdSize)adSize controller:(UIViewController *)controller;
{
GADBannerView *view = [[GADBannerView alloc] initWithAdSize:adSize];
// Set the position of the view to bottom
CGFloat y = parentView.frame.size.height - view.frame.size.height;
CGRect newFrame = view.frame;
newFrame.origin.y = y;
newFrame.origin.x = parentView.frame.size.width/2-view.frame.size.width/2;
view.frame = newFrame;
[parentView addSubview:view];
// Set the reference
_mBannerView = view;
// Replace this ad unit ID with your own ad unit ID.
view.adUnitID = kAdUnitIDBanner;
view.rootViewController = controller;
GADRequest *request = [GADRequest request];
GADMobileAds.sharedInstance.requestConfiguration.testDeviceIdentifiers = @[ @“e6e85bc01252631fb6f724efe42189a2” ];
[view loadRequest:request];
[self hideBanner];
}
(void)hideBanner
{
[_mBannerView setHidden:YES];
}
(void)showBanner
{
[_mBannerView setHidden:NO];
}
(void)to{
AdmobHelper* ad=[[AdmobHelper alloc]init];
[self presentViewController:ad animated:YES completion:nil];
}
#pragma mark - InterAd
/// Tells the delegate that the ad failed to present full screen content.
/// Tells the delegate that the ad presented full screen content.
/// Tells the delegate that the ad dismissed full screen content.
}
#pragma mark - RewardAd
(void)loadRewardedAd {
GADRequest *request = [GADRequest request];
[GADRewardedAd
loadWithAdUnitID:kAdUnitIDReward
request:request
completionHandler:^(GADRewardedAd *ad, NSError *error) {
if (error) {
NSLog(@“Rewarded ad failed to load with error: %@”, [error localizedDescription]);
return;
}
self.rewardedAd = ad;
NSLog(@“Rewarded ad loaded.”);
self.rewardedAd.fullScreenContentDelegate = self;
}];
}
(void)showRewardAd;
{
if (self.rewardedAd) {
UIViewController *topRootViewController = [[UIApplication sharedApplication] keyWindow].rootViewController;
//设置当前的conroller为置顶controller
while (topRootViewController.presentedViewController)
{
topRootViewController = topRootViewController.presentedViewController;
}
[self.rewardedAd presentFromRootViewController:topRootViewController userDidEarnRewardHandler:^{
// TODO: Reward the user!
[self rewardCallBack:true];
NSLog(@"获得奖励");
}];
} else {
NSLog(@“Ad wasn’t ready”);
}
}
extern void rewardCallBack(bool status);
extern void stopAnimation();
extern void startAnimation();
-(void)rewardCallBack:(BOOL) statue;
{
rewardCallBack(statue);
}
-(void)stopAnimation;
{
stopAnimation();
}
-(void)startAnimation;
{
startAnimation();
}
@end
谢谢,让我看看一下
谢谢你
我已经把广告做好了,其实就是用Shared的方法跑另一个controller的方法,这样self.rewarded就可以拿到value
请问你使用的是否是cocos 3.x?我用cocos 2.x接入admob没有问题。但3.x里 viewDidLoad方法不会回调。你是在哪个时机去加载广告view的呢?