Admob both test ads and real ads are not showing - swift

My app is not receiving neither of the Test ads or Real ads from Admob in both simulator and real device. But if I change the bundle id of the app, it receives both type of ads just fine. The issue is coming up after I switched the app to new AdMob account. I have updated the publisher id in Info.plist file and also the ad units that are being used to that of new account.
The error received is:
Error Domain=com.google.admob Code=1 "Request Error: No ad to show." UserInfo={NSLocalizedDescription=Request Error: No ad to show.}
I have implemented the Admob following guides at
https://developers.google.com/admob/ios/quick-start
https://developers.google.com/admob/ios/banner
var bannerView: GADBannerView!
override func viewDidLoad() {
...
bannerView = GADBannerView(adSize: kGADAdSizeBanner)
bannerView.adUnitID = "ca-app-pub-3940256099942544/2934735716"
bannerView.rootViewController = self
bannerView.delegate = self
bannerView.load(GADRequest())
addBannerViewToView(bannerView)
}
func addBannerViewToView(_ bannerView: GADBannerView) {
bannerView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(bannerView)
view.addConstraints(
[NSLayoutConstraint(item: bannerView,
attribute: .bottom,
relatedBy: .equal,
toItem: bottomLayoutGuide,
attribute: .top,
multiplier: 1,
constant: 0),
NSLayoutConstraint(item: bannerView,
attribute: .centerX,
relatedBy: .equal,
toItem: view,
attribute: .centerX,
multiplier: 1,
constant: 0)
])
}
...
func adView(_ bannerView: GADBannerView,
didFailToReceiveAdWithError error: GADRequestError) {
print(error)
}

Related

How can I improve performance in my SpriteKit game while displaying adMob banner ads?

I recently developed a game in Swift using SpriteKit. The game runs perfectly well without any ads, however, after inserting banner ads on the bottom using Google's adMob I noticed a significant drop in frame rate and performance during gameplay. Here is how I incorporated them into my game.
// in the viewDidLoad()
bannerView = GADBannerView(adSize: kGADAdSizeBanner)
bannerView.adUnitID = "ca-app-pub-2217093431614134/7329876635"
bannerView.rootViewController = self
bannerView.load(GADRequest())
bannerView.delegate = self
addBannerViewToView(bannerView)
// function to add Banner ad to view
func addBannerViewToView(_ bannerView: GADBannerView) {
bannerView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(bannerView)
view.addConstraints(
[NSLayoutConstraint(item: bannerView,
attribute: .bottom,
relatedBy: .equal,
toItem: view.safeAreaLayoutGuide,
attribute: .bottom,
multiplier: 1,
constant: 0),
NSLayoutConstraint(item: bannerView,
attribute: .centerX,
relatedBy: .equal,
toItem: view,
attribute: .centerX,
multiplier: 1,
constant: 0)
])
}
For now I disabled banner ads during the actual game. They appear again when there's no gameplay taking place. This is a temporal solution. Is there an actual solution to show banner ads during gameplay while not affecting the game's performance? Thanks in advance!

How to disable an NSTextField and make it uneditable? [isEditable property not working]

I'm getting the promo code input in NSTextField and on clicking apply the promo code is applied.
At this point, my text field should be uneditable until the remove promo code is pressed. if remove promo code button is pressed the text field becomes editable. This works just like Zomato coupon codes apply and remove.
I tried isEditable = false but that doesn't work
#IBOutlet weak var promoCodeValidity: NSTextField!
#IBOutlet weak var promoCode: NSTextField!
func applyCoupon(){
let couponCode = promoCode.stringValue
if let offer = bookingView!.applyOffer(offerCode:couponCode){
promoCodeValidity.stringValue="Offer Applied "+String(offer)+"% Off"
promoCode.isEditable=false
}
else{
promoCodeValidity.stringValue="Offer Code Invalid"
}
}
why isEditable doesn't work
Try toggling the isEnabled property of the text field and then change the isEnabled property of the text field.
i.e First disable the text field, then change the editable property of the text field and then again enable the text field.
Hope this solves your problem.
I have attached a small code sample also :)
class ViewController: NSViewController {
let textField = NSTextField()
let button = NSButton()
override func viewDidLoad() {
textField.translatesAutoresizingMaskIntoConstraints=false
textField.placeholderString = "ENTER PROMO CODE"
button.translatesAutoresizingMaskIntoConstraints=false
super.viewDidLoad()
textField.isEditable = true
button.action = #selector(buttonClicked(_:))
view.addSubview(textField)
view.addSubview(button)
NSLayoutConstraint(item: textField, attribute: .centerY, relatedBy: .equal, toItem: view, attribute: .centerY, multiplier: 1.0, constant: 0).isActive = true
NSLayoutConstraint(item: textField, attribute: .centerX, relatedBy: .equal, toItem: view, attribute: .centerX, multiplier: 1.0, constant: 0).isActive = true
NSLayoutConstraint(item: textField, attribute: .width, relatedBy: .equal, toItem: nil , attribute: .notAnAttribute, multiplier: 1.0, constant: 100).isActive = true
NSLayoutConstraint(item: button, attribute: .centerY, relatedBy: .equal, toItem: view, attribute: .centerY, multiplier: 1.0, constant: 0).isActive = true
NSLayoutConstraint(item: button, attribute: .leading, relatedBy: .equal, toItem: textField, attribute: .trailing, multiplier: 1.0, constant: 20).isActive = true
NSLayoutConstraint(item: button, attribute: .width, relatedBy: .equal, toItem: nil , attribute: .notAnAttribute, multiplier: 1.0, constant: 50).isActive = true
// Do any additional setup after loading the view.
}
override var representedObject: Any? {
didSet {
// Update the view, if already loaded.
}
}
#objc func buttonClicked(_ sender: Any) {
textField.isEnabled = false
textField.isEditable = false
textField.isEnabled = true
}
}
add extention:
extension UITextField
{
open override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
if action == #selector(UIResponderStandardEditActions.paste(_:)) || action == #selector(UIResponderStandardEditActions.cut(_:)) || action == #selector(UIResponderStandardEditActions.copy(_:)) || action == #selector(UIResponderStandardEditActions.select(_:)) || action == #selector(UIResponderStandardEditActions.selectAll(_:)) {
return false
}
return false
}
}
and:
textFiled.isEditable = false

How to add bannerAds add in to pop up view swift 4

App crash when banner ads into pop up view
Terminating app due to uncaught exception 'NSGenericException',
reason: 'Unable to install constraint on view. Does the constraint
reference something from outside the subtree of the view? That's
illegal. constraint:
viewads.addConstraints(
[NSLayoutConstraint(item: bannerView,
attribute: .bottom,
relatedBy: .equal,
toItem: bottomLayoutGuide,
attribute: .top,
multiplier: 1,
constant: 0),
NSLayoutConstraint(item: bannerView,
attribute: .centerX,
relatedBy: .equal,
toItem: view,
attribute: .centerX,
multiplier: 1,
constant: 0)
]) popview.addSubview(bannerView)
banner add in to pop view
Here's how I managed to add a banner to my app:
1. Add this to your ViewDidLoad
bannerView = GADBannerView(adSize: kGADAdSizeSmartBannerPortrait)
addBannerViewToView(bannerView)
bannerView.delegate = self
bannerView.adUnitID = "ca-app-pub-3940256099942544/2934735716" //This is the test ID, always use this to test your app.
bannerView.rootViewController = self
bannerView.load(GADRequest())
2. Add these functions, you can find them on the google AdMob website, I personally suggest to add them all, before I added them my code didn't work
func addBannerViewToView(_ bannerView: GADBannerView) {
bannerView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(bannerView)
if #available(iOS 11.0, *) {
// In iOS 11, we need to constrain the view to the safe area.
positionBannerViewFullWidthAtBottomOfSafeArea(bannerView)
}
else {
// In lower iOS versions, safe area is not available so we use
// bottom layout guide and view edges.
positionBannerViewFullWidthAtBottomOfView(bannerView)
}
}
// MARK: - view positioning
#available (iOS 11, *)
func positionBannerViewFullWidthAtBottomOfSafeArea(_ bannerView: UIView) {
// Position the banner. Stick it to the bottom of the Safe Area.
// Make it constrained to the edges of the safe area.
let guide = view.safeAreaLayoutGuide
NSLayoutConstraint.activate([
guide.leftAnchor.constraint(equalTo: bannerView.leftAnchor),
guide.rightAnchor.constraint(equalTo: bannerView.rightAnchor),
guide.bottomAnchor.constraint(equalTo: bannerView.bottomAnchor)
])
}
func positionBannerViewFullWidthAtBottomOfView(_ bannerView: UIView) {
view.addConstraint(NSLayoutConstraint(item: bannerView,
attribute: .leading,
relatedBy: .equal,
toItem: view,
attribute: .leading,
multiplier: 1,
constant: 0))
view.addConstraint(NSLayoutConstraint(item: bannerView,
attribute: .trailing,
relatedBy: .equal,
toItem: view,
attribute: .trailing,
multiplier: 1,
constant: 0))
view.addConstraint(NSLayoutConstraint(item: bannerView,
attribute: .bottom,
relatedBy: .equal,
toItem: bottomLayoutGuide,
attribute: .top,
multiplier: 1,
constant: 0))
}
/// Tells the delegate an ad request loaded an ad.
func adViewDidReceiveAd(_ bannerView: GADBannerView) {
print("adViewDidReceiveAd")
addBannerViewToView(bannerView)
bannerView.alpha = 0
UIView.animate(withDuration: 1, animations: {
bannerView.alpha = 1
})
}
/// Tells the delegate an ad request failed.
func adView(_ bannerView: GADBannerView,
didFailToReceiveAdWithError error: GADRequestError) {
print("adView:didFailToReceiveAdWithError: \(error.localizedDescription)")
}
/// Tells the delegate that a full-screen view will be presented in response
/// to the user clicking on an ad.
func adViewWillPresentScreen(_ bannerView: GADBannerView) {
print("adViewWillPresentScreen")
}
/// Tells the delegate that the full-screen view will be dismissed.
func adViewWillDismissScreen(_ bannerView: GADBannerView) {
print("adViewWillDismissScreen")
}
/// Tells the delegate that the full-screen view has been dismissed.
func adViewDidDismissScreen(_ bannerView: GADBannerView) {
print("adViewDidDismissScreen")
}
/// Tells the delegate that a user click will open another app (such as
/// the App Store), backgrounding the current app.
func adViewWillLeaveApplication(_ bannerView: GADBannerView) {
print("adViewWillLeaveApplication")
}
Don't forget to set GADBannerViewDelegate in your class.

Programmatically created NSCollectionView `makeItem` returns nil even if registered

I am developing a macOS app targeting macOS 10.10 SDK and using Xcode 9.3 (Swift 4). I am not using xibs, but creating all views programmatically.
I want to create a NSCollectionView. I register a subclass of NSCollectionViewItem and then register that class to the NSCollectionView with a call to collectionView.register(:,forItemWithIdentifier). Later, in the data source, I call collectionView.makeItem(withIdentifier:,for:).
However the makeItem method always returns nil. What am I doing wrong?
I found a similar question but the solution is to call register, which I already do.
For reference, here's the minimum working to reproduce my the issue: when I put a breakpoint after the call to makeItem, I can see that the returned value is always nil.
import Cocoa
#NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
#IBOutlet weak var window: NSWindow!
func applicationDidFinishLaunching(_ aNotification: Notification) {
window.contentViewController = TestViewController()
}
func applicationWillTerminate(_ aNotification: Notification) {
// Insert code here to tear down your application
}
}
let cellIdentifier = NSUserInterfaceItemIdentifier(rawValue: "testIdentifier")
class TestViewController: NSViewController, NSCollectionViewDataSource {
override func loadView() {
self.view = NSView()
}
override func viewDidLoad() {
let scroll = NSScrollView()
self.view.addSubview(scroll)
scroll.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint(item: scroll, attribute: .bottom, relatedBy: .equal, toItem: self.view, attribute: .bottom, multiplier: 1, constant: 0).isActive = true
NSLayoutConstraint(item: scroll, attribute: .top, relatedBy: .equal, toItem: self.view, attribute: .top, multiplier: 1, constant: 0).isActive = true
NSLayoutConstraint(item: scroll, attribute: .left, relatedBy: .equal, toItem: self.view, attribute: .left, multiplier: 1, constant: 0).isActive = true
NSLayoutConstraint(item: scroll, attribute: .right, relatedBy: .equal, toItem: self.view, attribute: .right, multiplier: 1, constant: 0).isActive = true
NSLayoutConstraint(item: scroll, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 500).isActive = true
NSLayoutConstraint(item: scroll, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 500).isActive = true
let collection = NSCollectionView()
scroll.documentView = collection
collection.register(TestViewItem.self, forItemWithIdentifier: cellIdentifier)
collection.dataSource = self
collection.collectionViewLayout = NSCollectionViewFlowLayout()
collection.reloadData()
}
func collectionView(_ collectionView: NSCollectionView, numberOfItemsInSection section: Int) -> Int {
return 10
}
func collectionView(_ collectionView: NSCollectionView, itemForRepresentedObjectAt indexPath: IndexPath) -> NSCollectionViewItem {
let item = collectionView.makeItem(withIdentifier: cellIdentifier, for: indexPath)
return item
}
}
class TestViewItem: NSCollectionViewItem {
override func loadView() {
self.view = NSView()
}
}
I think this is a bug. Workaround: set collectionViewLayout before registering the class.
Apparently the registered class is not stored if collectionViewLayout is not set. collectionViewLayout can be set to a new layout after registering the class, the registered class isn't removed.

Swift 2 Programmatically Constraints, i can't see the web view

i have a webkit and under the webkit there is another view... so i have three view = view, secondView(the one contain the WKWebView) and bannerView(with ADS). I added WKWebView to the secondView programmatically but bannerView and their constraints are set in storyboard. Now i need to set the constraints for WKWebView, i saw hundreds of tutorial, run without crash and problems but i can't see the WKWebView! how can i fix that ?
Code:
class ViewController: UIViewController {
#IBOutlet var bannerView: GADBannerView!
#IBOutlet var secondView: UIView!
var webView = WKWebView()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
print("Google Mobile Ads SDK version: " + GADRequest.sdkVersion())
//bannerView.adUnitID = ""
//bannerView.rootViewController = self
//var request = GADRequest()
//request.testDevices = ["kGADSimulatorID"]
//bannerView.loadRequest(request)
let preferences = WKPreferences()
preferences.javaScriptEnabled = true
let configuration = WKWebViewConfiguration()
configuration.preferences = preferences
webView.frame = CGRectMake(0, 0, 600, 543)
webView.customUserAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4) AppleWebKit/600.7.12 (KHTML, like Gecko) Version/8.0.7 Safari/600.7.12"
let url = NSURL(string:"https://apple.com/")
let req = NSURLRequest(URL:url!)
self.webView.loadRequest(req)
secondView.addSubview(webView)
secondView.bringSubviewToFront(webView)
webView.translatesAutoresizingMaskIntoConstraints = false
let xConstraint = NSLayoutConstraint(item: webView, attribute: .CenterX, relatedBy: .Equal, toItem: secondView, attribute: .CenterX, multiplier: 1, constant: 0)
let yConstraint = NSLayoutConstraint(item: webView, attribute: .CenterY, relatedBy: .Equal, toItem: secondView, attribute: .CenterY, multiplier: 1, constant: 0)
let bottomConstraint = NSLayoutConstraint(item: webView, attribute: .Bottom, relatedBy: .Equal, toItem: secondView, attribute: .Bottom, multiplier: 1, constant: 0)
secondView.addConstraints([xConstraint, yConstraint, bottomConstraint])
/*
let topConstraint = NSLayoutConstraint(item: webView, attribute: NSLayoutAttribute.TopMargin, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.BottomMargin, multiplier: 1, constant: 0)
let leadingConstraint = NSLayoutConstraint(item: webView, attribute: NSLayoutAttribute.Leading , relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: -20)
let trailingConstraint = NSLayoutConstraint(item: webView, attribute: NSLayoutAttribute.Leading , relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: -20)
let bottomConstraints = NSLayoutConstraint(item: webView, attribute: NSLayoutAttribute.Bottom , relatedBy: NSLayoutRelation.Equal, toItem: self.bannerView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0)
*/
/* var LeftwebViewConstraints = NSLayoutConstraint(item: webView, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Leading, multiplier: 1.0, constant: -20)
var RightwebViewConstraints = NSLayoutConstraint(item: webView, attribute: NSLayoutAttribute.TrailingMargin, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.TrailingMargin, multiplier: 1.0, constant: -20)
var BottomwebViewConstraints = NSLayoutConstraint(item: webView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: bannerView, attribute: NSLayoutAttribute.BottomMargin, multiplier: 1.0, constant: 0)
// view.addConstraints([LeftwebViewConstraints, RightwebViewConstraints, BottomwebViewConstraints])
NSLayoutConstraint.activateConstraints([LeftwebViewConstraints, RightwebViewConstraints, BottomwebViewConstraints])
*/
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Yes, it would appear that you are missing some constraints.
I might suggest adding all of the necessary constraints using VFL, which may be more intuitive:
secondView.addSubview(webView)
webView.translatesAutoresizingMaskIntoConstraints = false
let views = ["webView" : webView]
secondView.addSubview(webView)
secondView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[webView]|", options: [], metrics: nil, views: views))
secondView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[webView]|", options: [], metrics: nil, views: views))
This obviously assumes that the constraints for secondView were defined correctly in the first place. Check its frame before you start losing any sleep over the frame of the webView.
But note, I don't manually set the frame of the webView, but rather I simply defined both horizontal and vertical constraints.