How to remove ADBannerView in Swift - swift

I am creating a SpriteKit game with multiple scenes. I want to remove the ad once the users starts to play the actual game, then create another ad when the user transitions to the Gameover scene. Hiding the adbannerview does not work, as it will not reappear once the user loses again.
So how do you remove an AdBannerView?

Try to use a NSNotification in the GameViewController, something like this:
func hideAdBanner(notification: NSNotification) {
println("hiding banner")
adBanner.alpha = 0
adBanner.hidden = true
}
override func viewDidLoad() {
super.viewDidLoad()
NSNotificationCenter.defaultCenter().addObserver(self, selector: "hideAdBanner:" , name: "hideAdBanner", object: nil)
}
Then call this NSNotification in your scene

Related

How to get the position of my current PDFPage?

I am using PDFKit and added a PDFView to my ViewControllers view. I want to know where the current PDFPages position in my main view is. Normally i would use the convert() function given by UIView but PDFPage is not a UIView. Any ideas ?
In my case, I put a NotificationCenter to see every time the pdfView was swiped with this
NotificationCenter.default.addObserver(self, selector: #selector(viewDidSwipe), name: Notification.Name.PDFViewPageChanged, object: nil)
Then, in the viewDidSwipe function,
#objc func viewDidSwipe(){
let currentSlideCount = pdfView.currentPage?.label
self.title = currentSlideCount!
}

How to get a current volume of iPhone and change it while playing music using swift?

In my app I need to change volume with slider, I've got current volume with
#IBOutlet weak var volumeChange: UISlider! {
didSet {
let audioSession = AVAudioSession.sharedInstance()
let volume : Float = audioSession.outputVolume
volumeChange.setValue(volume, animated: true)
}
}
How can I change it? Now I have this method
#IBAction func handleVolumeChange(_ sender: UISlider) {
player.volume = sender.value
}
This method doesn't work in this case. Is it possible to make it like for example in Music app: changing volume in app with changing volume on device?
To get the volume then you will need to use AVAudioSession.sharedInstance().outputVolume as you already have used.
In order to allow the user to control the volume you will want to have a look at MPVolumeView found in the Media Player framework. This component is able to make changes to the system volume.
It is very simple to use:
let volumeView = MPVolumeView(frame: volumeViewSize)
playerView.addSubview(volumeView)
You will want to use this class instead of using your UISlider instance. There are methods supplied that let you override the look and feel of the slider.
Additionally this class exposes a control which allows the user to choose the output route (iPhone, Airpods, Homepod, or Apple TV for example). You can choose to disable either the slider or the output route control so this gives you a fairly broad set of options with customising your user interface.
If you are working with a storyboard then you will need to add it in code. First create an empty UIView in the view controller's view on the storyboard and attach that to your view controller:
Once you've added that you will want something like the following in your view controller:
#IBOutlet var volumeSliderContainer: UIView!
private lazy var volumeView: MPVolumeView = {
let volumeView = MPVolumeView(frame: volumeSliderContainer.bounds)
volumeView.showsVolumeSlider = true
volumeView.showsRouteButton = true
volumeView.translatesAutoresizingMaskIntoConstraints = false
return volumeView
}()
override func viewDidLoad() {
super.viewDidLoad()
volumeSliderContainer.addSubview(volumeView)
NSLayoutConstraint.activate([
volumeView.widthAnchor.constraint(equalTo: volumeSliderContainer.widthAnchor),
volumeView.heightAnchor.constraint(equalTo: volumeSliderContainer.heightAnchor),
volumeView.centerXAnchor.constraint(equalTo: volumeSliderContainer.centerXAnchor),
volumeView.centerYAnchor.constraint(equalTo: volumeSliderContainer.centerYAnchor),
])
}
Now you will be able to interact with the MPVolumeView as you require.
Just watch out on the simulator, the component will not render the slider and you will only be able to see it when running your app on an actual device.
get current system volume -
var session = AVAudioSession.sharedInstance()
override func viewDidLoad() {
super.viewDidLoad()
if ((try? session.setActive(true)) != nil) {
volumeChange.setValue(session.outputVolume, animated: true)
}
}

Turn off gravity when a user sends the application to the background

I have a simple gravity related game in SpriteKit. Before the game starts gravity is off. When the screen is touched the following code is called:
missile?.physicsBody?.affectedByGravity = true
If you take a message and come back to it I would like gravity to switch off until you touch the screen again.
I'm guessing it is something in the AppDelegate, but I am literally stuck there.
Thank you in advance for any pointers.
First add add an observer for UIApplication.willResignActiveNotification to your mainNode like this:
NotificationCenter.default.addObserver(self, selector: #selector(appMovedToBackground), name: UIApplication.willResignActiveNotification, object: nil)
now you can pause your mainNode in appMoveToBackground:
#objc func appMovedToBackground() {
mainNode.isPaused = true
}
Make sure to remove the observer in deinit:
deinit {
NotificationCenter.default.removeObserver(self)
}

How to make a button in Xcode that globally stops AVAudioPlayer?

I'm trying to make a tableviewcell stop all sounds in the app when it is clicked. I know how to stop sounds with soundEffect.stop() soundEffect.currentTime = 0, but I don't know how to apply this to a cell and this doesn't work if the StopSound button is on another viewController with a different class. How do I make a function that globally stops AVAudioPlayer and apply that to a cell?
The problem is that the player is not alone. Try this.
To stop AVAudioPlayer on another ViewController you may use NotificationCenter.
Example...
First time, add function at VC, where you need to stop AVAudioPlayer.
#objc func stopAVonThisVC() {
AVaudioPlayer.stop()
}
Second, add NotificationCenter observer at this VC, and after just post on tap button.
// At VC, where you need to stop AVAudioPlayer.
NotificationCenter.default.addObserver(self, selector:
#selector(stopAVonThisVC), name: NSNotification.Name("Stop at VC1"), object: nil)
// At VC2
NotificationCenter.default.post(name: NSNotification.Name("Stop at VC1"), object: nil)
Sorry for poor english.
Create a BaseViewController for your project and subclass all your view controllers from there,
Define the AVAudioPlayer in BaseViewController and invoke soundEffect.stop() soundEffect.currentTime = 0 from any of your subclass, it should resolve the issue.
Example:
class BaseViewController: UIViewController {
var audioPayer: AVAudioPlayer!
}
class ViewControllerA: BaseViewController {
//start or stop audio player
}
class ViewControllerB: BaseViewController {
//start or stop audio player
}

How do I pause and play background video when navigating from foreground to background?

I have a background video playing when I start my app. I want the video to pause and resume where it left off if I happen to hit the home button. Here is my code:
class ViewController: UIViewController
{
let moviePlayerController = AVPlayerViewController()
var aPlayer = AVPlayer()
func playBackgroundMovie()
{
if let url = NSBundle.mainBundle().URLForResource("IMG_0489", withExtension: "m4v")
{
aPlayer = AVPlayer(URL: url)
}
moviePlayerController.player = aPlayer
moviePlayerController.view.frame = view.frame
moviePlayerController.view.sizeToFit()
moviePlayerController.videoGravity = AVLayerVideoGravityResizeAspect
moviePlayerController.showsPlaybackControls = false
aPlayer.play()
view.insertSubview(moviePlayerController.view, atIndex: 0)
}
func didPlayToEndTime()
{
aPlayer.seekToTime(CMTimeMakeWithSeconds(0, 1))
aPlayer.play()
}
override func viewDidLoad()
{
// Do any additional setup after loading the view, typically from a nib.
super.viewDidLoad()
playBackgroundMovie()
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ViewController.didPlayToEndTime), name: AVPlayerItemDidPlayToEndTimeNotification, object: nil)
}
Do I have to perform some actions in the app delegate? I looked at previous videos, but I think some of them are using outdated versions of swift. Or are just a little too confusing for me.
You should be able to register for a backgrounding/foregrounding notification via NSNotificationCenter to play or pause your aPlayer like so:
let notificationCenter = NSNotificationCenter.defaultCenter()
notificationCenter.addObserver(self, selector: #selector(pauseVideoForBackgrounding), name: UIApplicationDidEnterBackgroundNotification, object: nil)
There are several UIApplication notification names available including UIApplicationWillEnterForegroundNotification. You can leverage as many notifications as necessary to let your video player class know what's happening with the app state.
If you're supporting iOS 8 or lower, make sure to also remove your player class as an observer from any NSNotifications you've added to your view controller.
Yes, all of the application specific actions happen within your appdelegate protocol. Look to put your pause code in your appdelegate's applicationDidEnterBackground function, and your resume code in applicationWillEnterForeground.
I would check out Apple's docs on the AppDelegate protocol, particularly these two functions, as there's a little more to take into consideration in regards to the amount of time you're given to finish up tasks before your app becomes inactive (though if you're just pausing and resuming an AVPlayer you should be fine).