Swift 5 App - AdMob returns Timeout (XCode) - ios

I want to show a Reward Ad (Google AdMob) in my swift app. (XCode 11.4, Swift 5, Storyboard), but whenever I load the ad it shows the following error message in the console:
Error Domain=com.google.admob Code=5
"Request Error: The Google Ad request was unable to be fulfilled before a timeout occurred."
UserInfo={NSLocalizedDescription=Request Error: The Google Ad request was unable to be fulfilled before a timeout occurred., gad_response_info=<GADResponseInfo: 0x6000030f8090>}
I basically followed the tutorial from Google AdMob Tutorial.
I added my GADApplicationIdentifier to the info.plist
In the AppDelegate:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
GADMobileAds.sharedInstance().start(completionHandler: nil)
}
In the ViewController I followed the steps from the new RewardAd Tutorial and basically copied and pasted it, except for replacing the AdId with my own (I'm running it in a Simulator, so using own AdID's should not be a problem if I understood correctly...)
This is where it fails:
override func viewDidLoad() {
super.viewDidLoad()
rewardedAd = GADRewardedAd(adUnitID: adId)
rewardedAd?.load(GADRequest()) { error in
if let error = error {
print("Error while loading ad: \(error.description)")
// Timeout occurs here!!!
} else {
if self.rewardedAd?.isReady == true {
self.rewardedAd?.present(fromRootViewController: self, delegate:self)
}
}
}
}
Any idea why I get the timeout?
Note:
I also created a sample SwiftUI app with the same rewarded app structure, which also gets the timeout
and yes my internet connection works just fine
I tried the example AdIds from AdMob too, then I get the following error:
Error while loading ad: Error Domain=com.google.admob Code=1
"Request Error: No ad to show."
UserInfo={NSLocalizedDescription=Request Error: No ad to show., gad_response_info=<GADResponseInfo: 0x6000018f4d80>}
I used in the 'info.plist' : ca-app-pub-3940256099942544~1458002511
and for the AdID : ca-app-pub-3940256099942544/1712485313 both from the above mentioned tutorial pages from Google.

I think you should use your own appID with test Ad ID.

Related

Can't register for Sendbird push notifications in Swift

I have an iOS app in which I am using Sendbird successfully. Nevertheless, the functionality of receiving notifications when the app is running in background does not work. In fact, when I try to register the user for these notifications in the AppSelegate.swift file, I get the error 800101 ERR_CONNECTION_REQUIRED, what suggests I am not connecting correctly to the Sendbird server. However, I have checked that the connection is established without giving errors as soon as the app launches and before trying to register for those notifications.
This is the code for establishing the connection:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
SBDMain.initWithApplicationId(Constants.SENDBIRD_APP_ID, useCaching: false) {
} completionHandler: { error in
print(error)
}
...
}
This is my code for trying to register:
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
Messaging.messaging().apnsToken = deviceToken
SBDMain.registerDevicePushToken(deviceToken, unique: true, completionHandler: { (status, error) in
if error == nil {
if status == SBDPushTokenRegistrationStatus.pending {
return
}
else {
print("Successfully registered")
}
}
else {
print(error)
}
})
}
As I said, I have made sure the sendbird application is initialized before the registration, but for some reason, when trying to register for the push notifications I get the "connection required" error. The rest of the Sendbird functionalities work fine, so I guess the connection is well established. The notifications work fine on Android using the same data.
Disclaimer: I am a Sendbird employee.
Hi Pepito,
I'm unsure specifically what version you're on however it looks like you may not be calling the actual connect method prior to trying to register the token.
There is a two step process when creating a connection. The first is initializing the SDK with your Application ID. The second is actually calling the connect method with the userId and accessToken(If you're using them).
// Initialize a SBDMain instance to use APIs in the client app.
SBDMain.initWithApplicationId(APP_ID, useCaching: false) {
} completionHandler: { error in
}
// The USER_ID below should be unique to your Sendbird application.
SBDMain.connect(withUserId: USER_ID, completionHandler: { (user, error) in
guard error == nil else {
// Handle error.
}
// The user is connected to Sendbird server.
...
})
If you're still having trouble after this, I suggest you reach out on our community forums so we can acquire more information from you. https://community.sendbird.com

Swift - trouble integrating Google AdMob with my application

I'm attempting to implement ads for my iOS application using Google's AdMob SDK.
I've been following the official [docs](https://developers.google.com/admob/ios/quick-start), but I'm having trouble getting that to work.
What I've done so far:
Created an AdMob account and registered my app.
Updated my info.plist so it matches the format shown in the tutorial.Needless to say, I replaced the tutorial's app ID with the ID I got from my Google AdMob account.
Building my application as-is with the info.plist changes (without any imports or additional code) already introduced some weird logs:
2020-11-16 03:14:08.242623+0200 [5520:1418287] - <Google>[I-ACS025031] AdMob App ID changed. Original, new: (nil), ca-app-pub-2838133095156647~5679250242
2020-11-16 03:14:08.249700+0200 [5520:1418287] [NetworkInfo] Could not successfully update network info for descriptor <CTServiceDescriptor 0x281b3cec0, domain=1, instance=2> during initialization.
2020-11-16 03:14:08.250665+0200 [5520:1418287] [NetworkInfo] Signal strength query returned error: Error Domain=NSPOSIXErrorDomain Code=13 "Permission denied", descriptor: <CTServiceDescriptor 0x281b3f1a0, domain=1, instance=1>
2020-11-16 03:14:08.251144+0200 [5520:1418287] [NetworkInfo] Signal strength query returned error: Error Domain=NSPOSIXErrorDomain Code=13 "Permission denied", descriptor: <CTServiceDescriptor 0x281b3cec0, domain=1, instance=2>
2020-11-16 03:14:08.258569+0200 [5520:1418287] - <Google>[I-ACS023007] Analytics v.70100000 started
2020-11-16 03:14:08.259511+0200 [5520:1418287] - <Google>[I-ACS023008] To enable debug logging set the following application argument: -APMAnalyticsDebugEnabled (see https://help.apple.com/xcode/mac/8.0/#/dev3ec8a1cb4)
2020-11-16 03:14:08.285264+0200 [5520:1418283] [MC] System group container for systemgroup.com.apple.configurationprofiles path is /private/var/containers/Shared/SystemGroup/systemgroup.com.apple.configurationprofiles
2020-11-16 03:14:08.286833+0200 [5520:1418283] [MC] Reading from public effective user settings.
2020-11-16 03:14:08.303409+0200 [5520:1418283] - <Google>[I-ACS800023] No pending snapshot to activate. SDK name: app_measurement
2020-11-16 03:14:08.325373+0200 [5520:1418286] - <Google>[I-ACS023012] Analytics collection enabled
Then, I downloaded Google-Mobile-Ads-SDK using cocoapods & attempted to initialize the mobile ads SDK with the following code:
AppDelegate.swift:
// other imports
import GoogleMobileAds
#main
class AppDelegate: UIResponder, UIApplicationDelegate {
var window : UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions: [UIApplication.LaunchOptionsKey : Any]?) -> Bool {
GADMobileAds.sharedInstance().start(completionHandler: nil)
if #available(iOS 13, *) {
return true
}
else {
self.window = UIWindow()
let vc = ViewController()
self.window!.rootViewController = vc
self.window!.makeKeyAndVisible()
}
return true
}
...
This introduces a new error: Main Thread Checker: UI API called on a background thread (in addition to the weird loggings from #3 that remain).
What is my problem? Any help would be appreciated!
I ended up solving my issue by using an older version of the SDK, version 7.43.0 worked for me.
In order to solve the UI API problem, I also had to use DispatchQueue.main.async:
ViewController.swift
override func viewDidLoad() {
DispatchQueue.main.async {
self.interstitial = self.createAndLoadInterstitial()
}
...
I wrote the createAndLoadInterstitial on a separate file:
AdMob.swift
import GoogleMobileAds
extension ViewController: GADInterstitialDelegate {
func createAndLoadInterstitial() -> GADInterstitial {
self.interstitial = GADInterstitial(adUnitID: "ca-app-pub-3940256099942544/4411468910") // Google's test interstitial adUnitID
interstitial.delegate = self
interstitial.load(GADRequest())
return interstitial
}
func interstitialDidDismissScreen(_ ad: GADInterstitial) {
// on interstitial dismiss - load another one
self.interstitial = createAndLoadInterstitial()
}
}
Hopefully future Googlers find this helpful :)

Unable to get Authcode after google SignIn in iOS (swift)

Problem:
In iOS app I am using google signIn to facilitate the process of getting the authCode, which later is sent into the server and it is exchanged for a refresh_token in google calendar API. The bad news is that user.serverAuthCode is always nil.
What happens now:
I have followed the google's documentation for implementing it. At this moment when I click the google sign-in button I go to a page where I fill in my credentials and I consent to all the scopes. Then it redirects me back to iOS app but I don't get the authCode.
Code:
class AppDelegate: UIResponder, UIApplicationDelegate, GIDSignInDelegate{
func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) {
if (error == nil) {
print("Authcode is : \(user.serverAuthCode)") // This is nil :(
}
}
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
GIDSignIn.sharedInstance().clientID = "MY_CLIENT_ID"
GIDSignIn.sharedInstance().delegate = self
GIDSignIn.sharedInstance().scopes.append("https://www.googleapis.com/auth/calendar")
GIDSignIn.sharedInstance().scopes.append("https://www.googleapis.com/auth/calendar.events")
return true
}
}
I've also added URL type in target info as described in google's documentation
Lastly I have a viewController which uses GIDSignInButton to start the log-in process.
Is there anything I am missing? I took a look at this post but I could not solve the problem. Thank you for your time!
After some time spending on searching the solution was found :)
Problem fixed after adding:
GIDSignIn.sharedInstance()?.serverClientID = "SERVER_CLIENT"
If you don't know how to get these: SERVER_CLIENT vs CLIENT_ID

How to fix `The user canceled the sign-in flow.` with Sign In with Google on iOS?

I am integrating Sign in with Google with iOS Firebase SDK. When a user taps on the GIDSignIn button, the app shows an alert showing "MyAPP" want to use Google.com for Sign in, but the alert dismisses quickly. The error I get in the didSignInFor user: GIDGoogleUser method as follows:
2019-02-21 16:54:13.104279+0530 MyApp[18743:185089] [Warning] Attempting to load the view of a view controller while it is deallocating is not allowed and may result in undefined behavior (<SFAuthenticationViewController: 0x7fa327945800>)
[DEBUG] Google sign-in error: Error Domain=com.google.GIDSignIn Code=-5 "The user canceled the sign-in flow." UserInfo={NSLocalizedDescription=The user canceled the sign-in flow.}
I have followed the Authenticate Using Google Sign-In on iOS documentation.
I also tried the answer at Google Sign-In crashes on iOS 9 attempting to call canOpenURL, but it is not working.
The AppDelegate implements GIDSignInUIDelegate as well.
If I change the button from GIDSignInButton to UIButton, it works, but the style is lost.
I also faced the issue before. The GIDSignInButton object don't need addTarget in code or linking #IBAction. The GIDSignInButton class already handle that action for us.
If you are trying to to run GIDSignIn.sharedInstance()?.signIn() in #IBAction. You can run GIDSignIn.sharedInstance()?.presentingViewController = self just before signIn instead of insideviewDidLoad().
#IBAction func googleLogin(_ sender: UIButton) {
GIDSignIn.sharedInstance()?.presentingViewController = self
GIDSignIn.sharedInstance()?.signIn()
}
In Appdelegate try using the below code
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey: Any] = [:]) -> Bool {
let result = GIDSignIn.sharedInstance().handle(url,
sourceApplication: options[UIApplication.OpenURLOptionsKey.sourceApplication] as? String,
annotation: options[UIApplication.OpenURLOptionsKey.annotation])
return result
}

Swift & Admob : No ad to show

I'm trying to add AdMob to my app.
My app is build with :
UITabBarController with 1. UIViewController; 2. UITableViewController; 3. UIViewController; 4. UIViewController.
I'm actually tring to add a Banner Ad from Admob in the 3. UIViewController.
I used that code:
#IBOutlet weak var amobBan: GADBannerView!
[•••]
amobBan.delegate = self
amobBan.adUnitID = "ca-app-pub-xxxxxxx" // I used mine in other apps : it works.
amobBan.rootViewController = self // NB: I also tried with tabBarController istead "self"
amobBan.loadRequest(GADRequest())
And it not works : ad isn't displayed.
So I used that function:
func adView(bannerView: GADBannerView!,
didFailToReceiveAdWithError error: GADRequestError!) {
print("adView:didFailToReceiveAdWithError: \(error.localizedDescription)")
}
And I got this output:
adView:didFailToReceiveAdWithError: Request Error: No ad to show.
I really think it's due to amobBan.rootViewController = self since Ad is showed when I set the 3. UIViewController as the Initial ViewController into the Storyboard.
I need answers in Swift, please.
Add your test devices - real devices and the simulator. You may find your test device string in Xcode output pane.
let request: GADRequest = GADRequest()
request.testDevices = [ "661359a1bfeb6e588caf9c8133904d10", "96a6d0a80ffaa7094ad91b6648b6b50f", kGADSimulatorID ]
bannerView.load(request)
Late answer but this may helps to other.
If you are facing issue like "No ad to show" in cases if you are using google banner ads(admob) then that issue solved by making view width 320 fixed.I solved my issue by making ad width 320 and height 50.
Hope this will helpful for others.
Use a test ad unit id, or add a test device
Google's documentation is pretty straight forward so maybe you should take a look into it.
https://developers.google.com/admob/ios/banner
At first glance I have a question:
Are you configuring AdMob in your AppDelegate didFinishLaunching?
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Initialize the Google Mobile Ads SDK.
// Sample AdMob app ID: ca-app-pub-3940256099942544~1458002511
GADMobileAds.configure(withApplicationID: "YOUR_ADMOB_APP_ID")
return true
}
Some tips, like Payal Umraliya said, size is a pretty big deal in admob, so make sure adview has the same size of the ad you are requesting.

Resources