I am currently trying to create a mobile application with rewarded ads and I struggle with the code on XCode.
For now I have already integrated interstitial ads and I would like to adapt the code for rewarded ads.
Here is my current working code for interstitial ads adapted from something found on Github:
UIView *gView;
UIViewController *gViewColtroller;
@interface adInterstitial : NSObject
/// The interstitial ad.
@property(nonatomic, strong) GADInterstitial *interstitial;
@property (strong, nonatomic) GADRequest *request;
@end
@implementation adInterstitial
-(id)init {
[self createAndLoadInterstitial];
NSLog(@"adInterstitial init");
return self;
}
- (void)createAndLoadInterstitial {
self.interstitial = [[GADInterstitial alloc] initWithAdUnitID:@"ca-app-pub-3940256099942544/4411468910"]; // test id
UIWindow *window = [UIApplication sharedApplication].keyWindow;
UIViewController *rootViewController = window.rootViewController;
self.interstitial.delegate = self;
GADRequest *request = [GADRequest request];
request.testDevices = @[ kGADSimulatorID, @"2077ef9a63d2b398840261c8221a0c9a" ];
[self.interstitial loadRequest:request];
NSLog(@"createAndLoadInterstitial");
}
- (void)interstitialWillDismissScreen:(GADInterstitial *)ad {
// Method for reloading the object so that you can show ads again
NSLog(@"interstitialWillDismissScreen");
[self createAndLoadInterstitial];
}
- (void)InterstitialView { // show interstitial ADS
if (self.interstitial.isReady) {
NSLog(@"Show interstitial ADS!");
UIWindow *window = [UIApplication sharedApplication].keyWindow;
UIViewController *rootViewController = window.rootViewController;
[self.interstitial presentFromRootViewController:rootViewController];
} else {
NSLog(@"interstitial Ad wasn't ready");
}
}
@end
And here is my attempt with rewarded ads:
@interface adRewarded : NSObject
/// The interstitial ad.
@property(nonatomic, strong) GADRewardedAd *rewarded;
@property (strong, nonatomic) GADRequest *request;
@end
@implementation adRewarded
-(id)init {
[self createAndLoadRewarded];
NSLog(@"adRewarded init");
return self;
}
- (void)createAndLoadRewarded {
self.rewarded = [[GADRewardedAd alloc] initWithAdUnitID:@"ca-app-pub-3940256099942544/4411468910"]; // test id
UIWindow *window = [UIApplication sharedApplication].keyWindow;
UIViewController *rootViewController = window.rootViewController;
self.rewarded.delegate = self;
GADRequest *request = [GADRequest request];
request.testDevices = @[ kGADSimulatorID, @"2077ef9a63d2b398840261c8221a0c9a" ];
[self.rewarded loadRequest:request];
NSLog(@"createAndLoadRewarded");
}
- (void)interstitialWillDismissScreen:(GADRewardedAd *)ad {
// Method for reloading the object so that you can show ads again
NSLog(@"rewardedWillDismissScreen");
[self createAndLoadRewarded];
}
- (void)RewardedView { // show interstitial ADS
if (self.rewarded.isReady) {
NSLog(@"Show rewarded ADS!");
UIWindow *window = [UIApplication sharedApplication].keyWindow;
UIViewController *rootViewController = window.rootViewController;
[self.rewarded presentFromRootViewController:rootViewController];
} else {
NSLog(@"rewarded Ad wasn't ready");
}
}
@end
And I obtain an error on the line : self.rewarded.delegate = self;
The error is : Property 'delegate' not found on object of type 'GADRewardedAd'
Just for info to start the ad in Python I am using the following code:
from pyobjus import autoclass
current_ad = autoclass("adInterstitial").alloc().init()
current_ad.show()
I also tried to use the Google documentation https://developers.google.com/admob/ios/rewarded#objective-c to create a new version but I struggle with the object named ViewController.
Either I get this error if I just copy paste the example:
Cannot find interface declaration for ViewController; did you mean UIViewController
or I get this error if I replace with UIViewController:
Cannot synthesize weak property in file using manual reference counting
Would you please know how to solve it ?
Thank you in advance!