How to display AdMob banner in AVPlayer? - ios

I'm working on an app to watch live streams. It is based on a table view and I'd like to offer it for free with ads (with the chance to buy the "Pro version" with in-app purchase). I managed to insert AdMob banners in the table view, but I'd like to insert small banners in the video streams too. I use AVPlayer to display streams and I know there's a method called contentOverlayView that allows to insert a custom view that stays visible when the video is playing, so I thought I have to use that method to display this banner, but I wasn't able to. The following code sets the player and it works perfectly, but the code I added to show the banner seems to be totally useless (i still see the video perfectly but no banner is displayed):
func allestisciPlayer(indexPath: IndexPath, destinazione: AVPlayerViewController) {
// Ricava il canale corretto per mezzo della funzione apposita
let canaleSelezionato = ritornaIlCanaleCorretto(indexPath: indexPath)
// Prende il percorso giusto per lo streaming, poi crea il player nella destinazione
let url = URL(string: canaleSelezionato.urlCanale)
if let streamingURL = url {
destinazione.player = AVPlayer(url: streamingURL)
// Il do-try-catch seguente serve per far funzionare l'audio dell'app anche se l'iPhone è in modalità silenziosa; togli il blocco per fare in modo che l'app non produca audio se l'iPhone è in modalità silenziosa
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
} catch {
print("Errore: non riesco a riprodurre l'audio se iPhone è in modalità silenziosa.")
}
let bannerView = GADBannerView()
bannerView.adUnitID = "ca-app-pub-xxxx/xxxx"
bannerView.rootViewController = self
let request = GADRequest()
request.testDevices = ["xxxx"]
bannerView.load(request)
bannerView.bounds = CGRect(x: 0, y: 0, width: 320, height: 50)
destinazione.contentOverlayView?.addSubview(bannerView)
// Infine avvia il player
destinazione.player?.play()
}
}
Where do I get wrong? I also tried to use this code in the prepareForSegue method and even by overriding the AVPlayerViewController's viewDidLoad method but still without success... anyone can help me please?
EDIT: I found out it wasn't that difficult :) I simply create a new banner (different from the existing one in the table view) and display it by creating another UIView, adding the banner as subview to this new view and finally adding the new view as subview to my AVPlayerViewController. I didn't manage to display banner using the contentOverlayView method, but I read somewhere that even if I would be able to display the ad using it, I wouldn't be able to click it cause user touches are disabled with the contentOverlayView content. Thank you anyway :)

This is not correct way to initialize the ad. You should initialize it in appdelegate, then you should be able to display the ad freely anywhere you want.

Related

Live Admob Ads have suddenly stopped displaying in my app

In June, Admob adverts worked perfectly and AdMob sent a letter with a verification PIN for me to verify my identity & payment details.
Around the beginning of July, nearly all live ads stopped displaying in my app. I am still making the same number of requests, but impressions are so low I have dropped to £0.00/£0.01 a day. All test ads work correctly.
This issue began around the time I renamed my app (only on the app store display), however; all links to my app in my AdMob account are correct so the name change appears to have made no difference on their front-end UI.
When I debug my app, I get a list of warnings in the output section:
[I-ACS025031] AdMob App ID changed. Original, new: (nil), AppId
My 'GADApplicationIdentifier' value in my info.plist is the same as the 'new' app id.
[I-ACS013003] User property name must start with a letter: _ap
I am not setting any user properties, no idea what this means.
What have I tried?
Setting up new ad units.
Reverting back to an older version of the app.
Contacted AdMob 'support' via a form. They told me my ad serving is being limited. They did not say for how long and it has been around 2/3 weeks (by 'limited', I don't think they meant completely stopped).
Checked for policy violations in my account; nothing is there.
Code I use to display ads:
I have created an 'AdMobDisplayer' class that allows me to set up and display ads; this is called by each view controller. For example, my banner ads code:
View Controller:
let adMobDisplayer = AdMobDisplayer()
#IBOutlet weak var bannerView: GADBannerView!
override func viewDidLoad() {
super.viewDidLoad()
self.bannerView = self.adMobDisplayer.setupAdBannerView(self.bannerView, viewController: self, adUnitId: Constants.timerTabBannerAdId)
self.adMobDisplayer.displayBannerAd(self.bannerView)
}
AdMobDisplayer:
func setupAdBannerView(_ bannerView: GADBannerView, viewController: UIViewController, adUnitId: String, bannerViewDelgate: GADBannerViewDelegate? = nil) -> GADBannerView {
if(checkIfAdsAreDisabled()) {
return bannerView
}
/// Creates a new GADBannerView to be displayed in a view controller
bannerView.adUnitID = adUnitId
/// bannerView.adUnitID = Constants.testBannerAdId
bannerView.rootViewController = viewController
if let delegate = bannerViewDelgate {
bannerView.delegate = delegate
}
return bannerView
}
func displayBannerAd(_ bannerView: GADBannerView) {
if(checkIfAdsAreDisabled()) {
return
}
///Creates a request and loads an advert from AdMob
let request = GADRequest()
request.testDevices = [ "My Device Id" ]
bannerView.load(request)
}
This should display a banner ad in the view. It worked when I first added adverts in, it works for test adverts, but intermittently/rarely for live adverts now.
Find the full application on my GitHub: https://github.com/AlexMarchant98/KeGal-Trainer
Thanks in advance for any help!
I recently worked on GADBannerView in my last app and had almost similar issue, in your case you may need to generate Admob ad id, from their website.
So I fixed this ages ago, but, my fix was to add the required 'NSAppTransportSecurity' keys into my info.plist.
https://developers.google.com/admob/ios/app-transport-security

using swift can we airplay photos on Apple TV programmatically?

I want to build a simple app in iOS using swift which shows the photos of the album in my app and on button click it should airplay on my Apple TV.
I read so many documents and articles but not sure if we can do it for photos. Few have said for audio/video it is possible but not sure about photos.
At least any links or sample code would be helpful.
Thanks
A user should connect the to Apple TV at Control Center via AirPlay:
Then you can use Apple TV as second screen of the app.
Check if the app have external display:
if UIScreen.screens.count > 1 {
// External display is connected ...
}
Create new UIScreen for external window:
if let externalScreen = UIScreen.screens.last {
externalWindow = UIWindow()
externalWindow.screen = externalScreen
configureExternalWindow(externalWindow)
externalWindow.isHidden = false
}
And use it as you want to display photo albums:
// In our Collection View selection callback
if inSingleDisplayMode {
photoViewController.photo = photo
navigationController?.pushViewController(photoViewController, animated: true)
} else {
showOnExternalDisplay(photo)
}
Additional information at WWDC sessions:
Adding Delight to your iOS App
AirPlay and External Displays in iOS apps

How to rename Done button in AVPlayer?

I'm developing an app in Swift 3.0 that gives users the chance to stream TV channels online. It is restricted to Italy at the time, so I'm trying to translate every button in the app in Italian... but I'm having problems for the Done button (and the "Live streaming" field) at the top of the AVPlayer screen that appears whenever the user chooses a channel to stream. I'm sure there's a way to edit them, because when I open a stream in Safari, the Done button is correctly displayed as "Fine" and "Live streaming" becomes "Trasmissione in diretta"... I'd like to see them this way too but I have no idea of the way to reach that goal.
I haven't made any specific class for the player, just created a segue from the storyboard to the AVPlayerController, and I manage streams entirely from the table view that contains the channels; here's my significant code:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "guardaCanale" {
let destinazione = segue.destination as! AVPlayerViewController
if let cellaCanaleSelezionata = sender as? CanaleTableViewCell {
let indexPath = tableView.indexPath(for: cellaCanaleSelezionata)!
let canaleSelezionato = ritornaIlCanaleCorretto(indexPath: indexPath)
let url = URL(string: canaleSelezionato.urlCanale)
if let streamingURL = url {
destinazione.player = AVPlayer(url: streamingURL)
// Il do-try-catch seguente serve per far funzionare l'audio dell'app anche se l'iPhone è in modalità silenziosa; togli il blocco per fare in modo che l'app non produca audio se l'iPhone è in modalità silenziosa
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
} catch {
print("Errore: non riesco a riprodurre l'audio se iPhone è in modalità silenziosa.")
}
destinazione.player?.play()
}
}
}
}
Anyone can help me please? I don't want to modify the Done button behavior, just change its visible name onscreen... hope someone could help me! Thank you so much! :)
It sounds like you haven't properly setup your app for Italian localization.
Translation for Apple provided UI elements is based on the language of the device and whether or not your app supports that language. If you change the devices language to Italian, this is what it should show automatically.
I'd suggest reading Apple's Localization Documentation to make sure you are properly setting up you app so the system knows it's localized for Italian.

SpriteKit (Swift) orientation change

I am making/made a game in landscape orientation and have a Facebook and a twitter button in the main menu to either open the app or Safari.
The problem I have is that the app or safari opens up in portrait mode and when I switch back to my game it briefly shows everything in portrait before changing back to landscape. It also shows you a multitasking preview in portrait.
It is not a big deal but obviously for that brief 1-2 seconds the game is in portrait mode everything is squashed and it looks sort of ugly and unprofessional. This only happens when you use those 2 social media buttons, when you just switch to a portrait app yourself via multitasking the game will stay in landscape upon return.
I have being trying to figure it out but I cannot find anything, especially for swift. Any help would be much appreciated. Thanks
PS. This is the code I used for the social media parts, I think its pretty straight forward and boiler plate.
//MARK: - Load Social Media
func loadFacebook() {
var appURL = NSURL(string: "APP ID HERE")
var webURL = NSURL(string: "WEB ID HERE")
if(UIApplication.sharedApplication().canOpenURL(appURL!)) {
// App
UIApplication.sharedApplication().openURL(appURL!)
} else {
// Safari
UIApplication.sharedApplication().openURL(webURL!)
}
}
func loadTwitter() {
var appURL = NSURL(string: "APP ID HERE")
var webURL = NSURL(string: "WEB ID HERE")
if(UIApplication.sharedApplication().canOpenURL(appURL!)) {
// App
UIApplication.sharedApplication().openURL(appURL!)
} else {
// Safari
UIApplication.sharedApplication().openURL(webURL!)
}
}
see here for answer as this is still an issue where there is not a lot of information about.
Swift sprite kit landscape only forced into portrait

ios iAd loading before displaying

I'm working on a iOS game in swift.
I'm trying to add an interstitial ad.
What I would like to do is when the game is over, displaying an Ad.
After closing it, the user can launch another game.
When the game is over I call this method:
func showFullScreenAd() {
if requestingAd == false {
interstitial = ADInterstitialAd()
interstitial!.delegate = self
requestingAd = true
}
}
func interstitialAdDidLoad(interstitialAd: ADInterstitialAd!) {
if interstitial?.loaded==true{
....}
}
The ad will be displayed when interstitialAdDidLoad is called.
So, between showFullScreenAd() and interstitialAdDidLoad some times we may have several seconds (the time for the ad to load) so the user can click on 'new game' and starts the game.
So, the game will begin and then the ad will be displayed (I'm handling the pause mode also) but it's weird to start the game and to have the ad showing up.
I'm thinking about the following case:
doing
interstitial = ADInterstitialAd()
when loading the game
and when the game is over just show the ad (if available), and doing it again after that.
What do you think about this ?
Thanks.
C.C.
You have to add the ad onto the view as a subview right? So load it and add it to the view when you are ready to. I'm assuming that you're doing it before interstitialAdDidLoad at the moment?

Resources