Dismissing SKScene Completely - swift

Okay so I have an app that starts off with a menu view controller that prompts user to press one of 4ish buttons which then loads a viewcontroller which then presents a scene and the user plays the game based on which button was pressed.
I am then having the user being redirected to another viewcontroller which presents another another scene, once a condition is met (they lose the game). Only problem is, the 2nd viewcontroller(and i'm assuming it's scene) is still running. I know this because I have a print statement inside of it's override function update method to see if it's still there.
In addition, I have audio playing in my old gamesene and it's still currently playing. I wouldn't EXACTLY mind that since later on i'm going to just end up passing audio data (mute all) between all 3 viewcontrollers and their presented scenes.
Only problem with what is going on right now is that when I run the app since the old viewiewcontroller an it's scene seem to still be running underneath, it keeps calling the transition which causes a weird look where when the condition meets, the transition loops endlessly to the new viewcontroller then back to the beginning of the transition then to the new viewcontroller again.
I've tried this piece of code:
let theVC = self.viewController?.storyboard?.instantiateViewController(withIdentifier: "TrumpVC") as! TrumpViewController
self.viewController?.navigationController?.pushViewController(theVC, animated: true)
self.viewController?.dismiss(animated: true, completion: {});
But it doesn't seem to help at all :( Essentially I navigate to a new viewcontroller and dismiss the current one (this is all in my scene)
Thanks

Solution:
let theVC = self.viewController?.storyboard?.instantiateViewController(withIdentifier: "TrumpVC") as! TrumpViewController
self.viewController?.navigationController?.pushViewController(theVC, animated: true)
self.viewController?.removeFromParentViewController()
self.viewController?.dismiss(animated: true, completion: nil)
self.view?.presentScene(nil)

Related

AppleTv - CustomOverlayViewController force popup at timecode

I'm using the CustomOverlayViewController on an AVPlayerViewController to display movies related to what the user is currently watching. (https://developer.apple.com/documentation/avkit/avplayerviewcontroller/3229856-customoverlayviewcontroller)
I want to bring that up into focus automatically when the credits start playing. I have a timecode in my metadata and can detect when that occurs. But, I can't find a way to force the view to popup without the user swiping to it on the remote. Is there a way to force this view into focus without user input?
P.S. It is fine if it can be accessed at other times manually by the user as well. I just need to be able to also autofocus it at a certain point.
I ran into the same issue and instead of using the customOverlayViewController property of AVPlayerViewController, I just call
vcToPresent = ViewController()
vcToPresent.modalPresentationStyle = .overCurrentContext
self.avPlayerVC?.present(vcToPresent, animated: true, completion: nil)
to present the desired VC on screen at the time you want it to display.

Dismiss completly a view controller in middle

I've been looking for days. Here's the problem:
I have a Home view controller. When it receives a notification, it presents a NotiViewController, called noti1, by
`home.present(noti1, animated: true, completion: nil)`.
After 15sec, another notification comes, I get the top view controller topVC, present another NotiViewController, called noti2, by
`topVC.present(noti2, animated: true, completion: nil)`.
Each notiVC has an timer which waits for 30sec to dismiss itself. Now I have Home -> noti1 -> noti2.
After 15sec, noti1 runs out of time and has to be dismissed. How can I dismiss it without interupting noti2 which is being presenting?
I tried
`orderVC.beginAppearanceTransition(false, animated: false)
orderVC.willMove(toParent: nil)
orderVC.view.removeFromSuperview()
orderVC.removeFromParent()
orderVC.endAppearanceTransition()`
These code does remove the view from screen, but it leaves a UITransitionView behind, which blocks user actions.
This image is took after noti2 is removed from superview etc, so there are 2 UITransitionViews is presenting, blocking user actions.
Any idea? Thanks a lot.
Found a way. I'm not sure if I'm correct or wrong, but here is my thought: View controller must be dismissed the way it's presented before. For example if you use present(viewController, animated, completion) to present, you should use dismiss(animated, completion) to dismiss. The problem, in my case, is if you dismiss a view controller, all child view controllers will be gone too.
The way I resolved my issue is using another way to "show" the target view controller:
parentVC.view.addSubview(childVC.view)
parentVC.addChild(childVC)
childVC.didMove(toParent: parentVC)
After adding some view controllers, I got a tree parentVC -> child1 -> child2 -> child3... If I want to dismiss child2, the code in my question worked:
child2.willMove(toParent: nil)
child2.view.removeFromSuperview()
child2.removeFromParent()
There is no UITransitionView blocking user actions, but it got no animation. For my this is good enough.

AVPlayer Audio Lock Screen updates stop working, only after another AVPlayer instance plays Video content

Summary:
If you only use AVPlayer to play streaming audio only in your application, then Lock Screen Updates will work just fine (many streams - one after another). However, if you use AVPlayer to play both Streaming Audio followed by a Local Video file (even while using separate instances of AVPlayer and on different view controllers), then when you try to play Streaming Audio again - nothing will show up on the Lock Screen any more. Not related to cleanup - because playing only multiple audio files in a row works fine and shows up on the Lock Screen every time, but as soon as you play 1 video file (Or even initialize it to any AVPlayer without calling .play() on it), audio Lock Screen updates permanently stop working, and you need to restart the app for it to work again. It sounds like an AVFoundation bug to me...
Detailed Breakdown:
I have an appliction with View Controllers A and B. View controller A is the presenter of view controller B (pushes it using a navigation controller). Both A and B have a seperate AVPlayer inside them (A's plays audio. B's plays video. Audio is streamed. Video is local file in docs directory).
VC A configures its AVPlayer to update the Lock Screen with the Artwork Image, progress status, forward 15s, back 15s buttons. Everything works great if only view controller A's player is used to play audio - no matter how many times I go back and forward to it (I.E. VC B's player is never used to play()). (The whole controller gets deinit correctly) The Lock Screen always works, and always updates all required playback information.
VC A (AVPlayer1) --> PUSH --> VC B (AVPlayer2)
This is the code to register for Lock Screen updates (including button and progress callbacks):
func setupNowPlaying() {
var nowPlayingInfo = [String : Any]()
nowPlayingInfo[MPMediaItemPropertyTitle] = title
nowPlayingInfo[MPMediaItemPropertyArtist] = artist
let artwork = MPMediaItemArtwork.init(boundsSize: self.artImage.size, requestHandler: { (size) -> UIImage in
return self.artImage
})
nowPlayingInfo[MPMediaItemPropertyArtwork] = artwork
guard let player = self.player else { return }
guard let currentItem = self.player?.currentItem else { return }
nowPlayingInfo[MPMediaItemPropertyPlaybackDuration] = currentItem.asset.duration.seconds
nowPlayingInfo[MPNowPlayingInfoPropertyPlaybackRate] = player.rate
nowPlayingInfo[MPNowPlayingInfoPropertyElapsedPlaybackTime] = currentItem.currentTime().seconds
let rcc = MPRemoteCommandCenter.shared()
let skipBackwardCommand = rcc.skipBackwardCommand
skipBackwardCommand.isEnabled = true
skipBackwardCommand.addTarget(handler: skipBackward)
skipBackwardCommand.preferredIntervals = [15]
let skipForwardCommand = rcc.skipForwardCommand
skipForwardCommand.isEnabled = true
skipForwardCommand.addTarget(handler: skipForward)
skipForwardCommand.preferredIntervals = [15]
UIApplication.shared.beginReceivingRemoteControlEvents()
MPNowPlayingInfoCenter.default().nowPlayingInfo = nowPlayingInfo
}
Additionally, the following is hooked up in AppDelegate (applicationWillResignActive), to enable background playback (standard stuff):
do {
try AVAudioSession.sharedInstance().setCategory(.playback, mode: .default, options: [.mixWithOthers, .allowAirPlay, .allowBluetooth, .allowBluetoothA2DP ])
print("Playback OK")
try AVAudioSession.sharedInstance().setActive(true)
print("Session is Active")
} catch {
print(error)
}
The problem of Not showing anything on the lock screen starts to occur when VC B gets presented and plays video using its own separate AVPlayer. VC A's AVPlayer is first paused. If I call .play() on VC B's player then when I come back to VC A (by clicking Back < in the nav bar), the progress updates to the Lock Screen will never show up again (the whole playback UI doesn't show up), however the background playback still works.
So as soon as I use another AVPlayer on another View Controller, my original player will never be able to post updates to the Lock Screen UI, ever again, until the application is terminated and re-launched.
I tried to do full cleanup and re-init on the player in VC A.
I tried to just not call .play() on VC B. (Everything works fine with VC A in that case).
VC B's player is not registered for Lock Screen updates, because it just plays video in the app for preview purposes.
Something under-the-hood with Apple's AVPlayer breaks VC A's AVPlayer's Lock Screen updates capability, as soon as another instance of AVPlayer is introduced and used to play in another View Controller.
I know the issue is related to the second AVPlayer on the separate controller, because just not calling .play() on VC B's player, fixes the screen lock update disappearance issue on VC A's player.
How can I work around this? I don't want to remove VC A's player from superview, and pass it to VC B, and reconfigure it, then reconfigure it back again in VC A if the user comes back. (very laborious and error prone). It also currently bricks Lock Screen updates permanently, after the first time the user goes through VC B's playback (which is part of my happy path)
Thanks in advance,
Alex
I was able to avoid this problem by removing dependence on AVPlayerViewController in VC-B. Now I use a third party component called: "Player" (Cocoapods: "Player"), which uses an AVPlayerLayer, but not AVPlayerViewController. https://cocoapods.org/pods/Player
Unfortunately I lost my playback UI controls (progress bar, play button, full screen button - which AVPlayerViewController was giving me for free). So I will have to build some custom playback controls.
So it seems that AVPlayerViewController was causing the problem with the lock screen and breaking it. After switching to using "Player", which still uses AVPlayer, but not AVPlayerViewController, the problem went away, and I now always see my playback controls on the Lock Screen.

Segue following a change in criteria from a local notification in Swift

I have created a local notification in Swift which gives the option to end a current game without having to go back in to the app. That's all fine and works as it should. The issue I'm having is that if the user does this, I don't want them to go back to the Game view controller if that happens to be the last view that was open when the app entered the background. I would like them to go back to the app's Home view controller instead.
I expected to be able to add a perform segue to my Game view controller in the following way, should the criteria match. I tried adding it to viewDidAppear(), but it didn't work:
override func viewDidAppear(animated: Bool) {
if isThereACurrentGame() == false {
performSegueWithIdentifier("unwindToHomeScreen", sender: self)
}
}
Is this something to do with viewDidAppear() not being called when the app comes back to the foreground? If so, what might an alternative be?
P.S. My isThereACurrentGame() function works as it should, as does the performSegueWithIdentifier elsewhere in the view controller, so these aren't the cause of the problem.

After FB logout programmatic transitions dont work (Swift)

having logged out with FB (keep in mind I havent yet updated to SDK 4.0 so that may be part of the issue), i try to switch views to another viewcontroller programmatically, but it doesnt work. Segues set up using the storyboard work fine, but this code no longer has any effect -
let logPage = self.storyboard?.instantiateViewControllerWithIdentifier("TheStart") as! InitialLoginViewController
self.navigationController?.pushViewController(logPage, animated: false)
Why would this be?