Skip to content

Commit 4465f21

Browse files
committed
rebase: fix(iOS): Add delegate and fix some method
fix #65
1 parent 902b592 commit 4465f21

File tree

4 files changed

+62
-64
lines changed

4 files changed

+62
-64
lines changed

CapacitorCommunityAdmob.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@ Pod::Spec.new do |s|
1515
s.swift_version = '5.1'
1616
s.static_framework = true
1717
s.dependency 'Capacitor'
18-
s.dependency 'Google-Mobile-Ads-SDK', '7.69.0'
18+
s.dependency 'Google-Mobile-Ads-SDK', '~> 8.0'
1919
end

demo/angular/package-lock.json

Lines changed: 4 additions & 25 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ios/Plugin/Plugin.swift

Lines changed: 55 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ import GoogleMobileAds
1010
* here: https://capacitor.ionicframework.com/docs/plugins/ios
1111
*/
1212
@objc(AdMob)
13-
public class AdMob: CAPPlugin, GADBannerViewDelegate, GADFullScreenContentDelegate, GADRewardedAdDelegate, GADInterstitialDelegate {
13+
public class AdMob: CAPPlugin, GADBannerViewDelegate, GADFullScreenContentDelegate {
1414

1515
var bannerView: GADBannerView!
16-
var interstitial: GADInterstitial!
16+
var interstitial: GADInterstitialAd!
1717
var rewardedAd: GADRewardedAd!
1818
var testingDevices: [String] = []
1919

@@ -29,29 +29,29 @@ public class AdMob: CAPPlugin, GADBannerViewDelegate, GADFullScreenContentDelega
2929

3030
if !isTrack {
3131
GADMobileAds.sharedInstance().start(completionHandler: nil)
32-
call.success([
32+
call.resolve([
3333
"value": true
3434
])
3535
} else if #available(iOS 14, *) {
3636
#if canImport(AppTrackingTransparency)
3737
ATTrackingManager.requestTrackingAuthorization(completionHandler: { _ in
3838
// iOS >= 14
3939
GADMobileAds.sharedInstance().start(completionHandler: nil)
40-
call.success([
40+
call.resolve([
4141
"value": true
4242
])
4343

4444
})
4545
#else
4646
GADMobileAds.sharedInstance().start(completionHandler: nil)
47-
call.success([
47+
call.resolve([
4848
"value": true
4949
])
5050
#endif
5151
} else {
5252
// iOS < 14
5353
GADMobileAds.sharedInstance().start(completionHandler: nil)
54-
call.success([
54+
call.resolve([
5555
"value": true
5656
])
5757
}
@@ -108,7 +108,7 @@ public class AdMob: CAPPlugin, GADBannerViewDelegate, GADFullScreenContentDelega
108108
self.bannerView.load(self.GADRequestWithOption(call.getBool("npa") ?? false))
109109
self.bannerView.delegate = self
110110

111-
call.success([
111+
call.resolve([
112112
"value": true
113113
])
114114
}
@@ -130,7 +130,7 @@ public class AdMob: CAPPlugin, GADBannerViewDelegate, GADFullScreenContentDelega
130130
"height": 0
131131
])
132132

133-
call.success([
133+
call.resolve([
134134
"value": true
135135
])
136136
}
@@ -148,14 +148,14 @@ public class AdMob: CAPPlugin, GADBannerViewDelegate, GADFullScreenContentDelega
148148
"height": subView.frame.height
149149
])
150150

151-
call.success([
151+
call.resolve([
152152
"value": true
153153
])
154154

155155
} else {
156156
NSLog("AdMob: not find subView for resumeBanner")
157157

158-
call.success([
158+
call.resolve([
159159
"value": false
160160
])
161161
}
@@ -166,7 +166,7 @@ public class AdMob: CAPPlugin, GADBannerViewDelegate, GADFullScreenContentDelega
166166
@objc func removeBanner(_ call: CAPPluginCall) {
167167
DispatchQueue.main.async {
168168
self.removeBannerViewToView()
169-
call.success([
169+
call.resolve([
170170
"value": true
171171
])
172172
}
@@ -283,11 +283,22 @@ public class AdMob: CAPPlugin, GADBannerViewDelegate, GADFullScreenContentDelega
283283
adUnitID = testingID
284284
}
285285

286-
self.interstitial = GADInterstitial(adUnitID: adUnitID)
287286
let request = self.GADRequestWithOption(call.getBool("npa") ?? false)
288-
self.interstitial.load(request)
289-
self.interstitial.delegate = self
290-
call.resolve(["value": true])
287+
GADInterstitialAd.load(
288+
withAdUnitID: adUnitID,
289+
request: request,
290+
completionHandler: { (ad, error) in
291+
if error != nil {
292+
NSLog("Received error on loading interstatial ad");
293+
call.reject("Loading failed")
294+
return;
295+
}
296+
297+
self.interstitial = ad;
298+
self.interstitial.fullScreenContentDelegate = self;
299+
call.resolve(["value": true])
300+
}
301+
)
291302
}
292303
}
293304

@@ -307,13 +318,13 @@ public class AdMob: CAPPlugin, GADBannerViewDelegate, GADFullScreenContentDelega
307318

308319
// Intertitial Events Degigates
309320
/// Tells the delegate an ad request succeeded.
310-
public func interstitialDidReceiveAd(_ ad: GADInterstitial) {
321+
public func interstitialDidReceiveAd(_ ad: GADInterstitialAd) {
311322
NSLog("interstitialDidReceiveAd")
312323
self.notifyListeners("onInterstitialAdLoaded", data: ["value": true])
313324
}
314325

315326
/// Tells the delegate an ad request failed.
316-
public func interstitial(_ ad: GADInterstitial, didFailToReceiveAdWithError error: NSError) {
327+
public func interstitial(_ ad: GADInterstitialAd, didFailToReceiveAdWithError error: NSError) {
317328
NSLog("interstitial:didFailToReceiveAdWithError: \(error.localizedDescription)")
318329
self.notifyListeners("onInterstitialAdFailedToLoad", data: ["error": error.localizedDescription, "errorCode": error.code])
319330
}
@@ -346,32 +357,40 @@ public class AdMob: CAPPlugin, GADBannerViewDelegate, GADFullScreenContentDelega
346357
}
347358

348359
let request = self.GADRequestWithOption(call.getBool("npa") ?? false)
349-
self.rewardedAd = GADRewardedAd(adUnitID: adUnitID)
350-
self.rewardedAd?.load(request) { error in
351-
if let error = error {
352-
// Handle ad failed to load case.
353-
NSLog("Rewarded ad failed to load with error: \(error.localizedDescription)")
354-
call.reject("Loading failed")
355-
return
356-
} else {
357-
// Ad successfully loaded.
358-
NSLog("AdMob Reward: Loading Succeeded")
359-
self.notifyListeners("onRewardedVideoAdLoaded", data: ["value": true])
360-
call.resolve(["value": true])
361-
}
362-
}
360+
361+
GADRewardedAd.load(
362+
withAdUnitID: adUnitID,
363+
request: request,
364+
completionHandler: { (ad, error) in
365+
if let error = error {
366+
NSLog("Rewarded ad failed to load with error: \(error.localizedDescription)")
367+
call.reject("Loading failed")
368+
return
369+
}
370+
371+
NSLog("AdMob Reward: Loading Succeeded")
372+
373+
self.notifyListeners("onRewardedVideoAdLoaded", data: ["value": true])
374+
self.rewardedAd = ad
375+
self.rewardedAd?.fullScreenContentDelegate = self
376+
call.resolve(["value": true])
377+
}
378+
)
363379
}
364380
}
365381

366382
@objc func showRewardVideoAd(_ call: CAPPluginCall) {
367383
DispatchQueue.main.async {
368384
if let rootViewController = UIApplication.shared.keyWindow?.rootViewController {
369-
if self.rewardedAd?.isReady == true {
370-
self.rewardedAd?.present(fromRootViewController: rootViewController, delegate:self)
371-
call.resolve([ "value": true ])
372-
} else {
385+
if let ad = self.rewardedAd {
386+
ad.present(fromRootViewController: rootViewController,
387+
userDidEarnRewardHandler: {
388+
call.resolve([ "value": true ])
389+
}
390+
)
391+
} else {
373392
call.reject("Reward Video is Not Ready Yet")
374-
}
393+
}
375394
}
376395

377396
}

ios/Podfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ end
99

1010
target 'Plugin' do
1111
capacitor_pods
12-
pod 'Google-Mobile-Ads-SDK', '7.69.0'
12+
pod 'Google-Mobile-Ads-SDK', '~> 8.0'
1313
end
1414

1515
target 'PluginTests' do
1616
capacitor_pods
17-
pod 'Google-Mobile-Ads-SDK', '7.69.0'
17+
pod 'Google-Mobile-Ads-SDK', '~> 8.0'
1818
end

0 commit comments

Comments
 (0)