Splash screen for watchkit in swift - swift

I would like to implement a splash screen for my watch application. After a long check, i haven come to realize there is not a way as for phone and tablet such launchscreen.xib or launch image in info.plist for watch
To implement splash screen, i made a new starting interface, which will only appear when application starts. To do that
for life cycle i have checked this blog
after that in willActivate() func i have implemented
if first {
first = false
var timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("update"), userInfo: nil, repeats: false)
}else{
first = true
self.popController()
}
and my update func
func update() {
self.pushControllerWithName("someListsController", context: nil)
}
my point is; i want this interface to show only when application starts, and when returning this interface , application to end.

Related

Swift - Calling a method in a UIViewController from a separate UIView class

Okey, I have been struggling this for a long time now.. Cant get it to work, and I know there is an easy solution that I cannot find.
I have a UIViewController displaying the main app. When it launches there is two containerViews overlaid that displays a count down for the app to start. Countdown is done, and the "overlay" viewcontrollers are removed from superview.
BUT, I need to call the function to remove VC from superview and start the app in the main UIViewController. But I am not able to trigger this function from the separate "overlay" UIView..
If I make an instance of the UIViewController to trigger the startGame() function it works, but then all the labels return nil, and the app crashes.. I believe this is because the UIViewController is triggered twice?
A lot of explaining here.. I will try to show some outtakes in code:
Main UIViewController:
class DMStartVC: UIViewController {
.....
func startGame() {
self.view.viewWithTag(100)?.removeFromSuperview()
self.view.viewWithTag(101)?.removeFromSuperview()
timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(DMStartVC.update), userInfo: nil, repeats: true)
UITimer = Timer.scheduledTimer(timeInterval: 0.5, target: self, selector: #selector(DMStartVC.UIUpdate), userInfo: nil, repeats: true)
}
....
}
The UIView handling the "overlay" container views:
class countdown: UIView {
let dmStartVC = DMStartVC()
...
//when animation is done:
dmStartVC.startGame()
}
This causes crash because all labels in the UIViewController is nil.
How can I reach that .startGame() function?
Only way I have been able to solve this is adding a timer inside the DMStartVC to trigger .startGame()... But that is not a good solution as the timer has to trigger at the exact time the countdown ends.. and it never does.

NSTimer with drawing barChart in Background

I'm using an NSTimer in an iOS App in background, which is saving some data every 30 seconds in an array. The app shows the last 10 values (values of 5 minutes) in a linechart.
My problem is to use the function of saving data into the array every 30 seconds also in background, when the app isn't on screen. I've written a lot of themes about this, but I don't understand it.
My timer is the following:
timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: (#selector(ViewController.counting)), userInfo: nil, repeats: true)
func counting() {
timerCounter += 1 //Int
if timerCounter%30==0 {
arrayOfValues.append(...) //Appending the array
reloadLineChart() // reload chart
}
}
Could anyone show me how to solve this? I know, there must be something with the background-methods in the ViewController, but don't now what to type in.
I think there must be a function, which is counting in background and a function that is reloading the chart when I'm back in the app.
I might understand that you don't want to declare the counter in app delegate, for whatever reason you might have,although I would recommend it. However you can call the functions I mentioned, from the same class in which you have defined the counter. You would need to call it like this:
override func viewDidLoad() {
super.viewDidLoad()
NSNotificationCenter.defaultCenter().addObserver(
self,
selector: "applicationWillResignActive:",
name: UIApplicationWillResignActiveNotification,
object: nil)
}
func applicationWillResignActive(notification: NSNotification) {
// stop your counter
}
Then do the same with the other function. Hope is clear.
In your app delegate you can use this method :
func applicationWillResignActive(application: UIApplication) {}
The code you add in this function will run right before your app goes in the background . Therefore you can write there the code to stop the counter.
Afterwards you need to use the following function to activate the counter again:
func applicationWillEnterForeground(application: UIApplication) {}
The code you write in this function will run when you come back to the app again. Hope it is clear enough.

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).

How do I adjust a slider to show the current brightness level on Mac OSX using Swift?

I have created a slider that allows me to control the brightness on my Mac. I'm trying to make sure the slider accurately represents the current brightness level. If I change the brightness via the keyboard, I want the slider to reflect the new level.
Right now the slider will show me the current brightness level, but only after I click the slider. Furthermore, I cannot change the brightness from the slider.
#IBAction func brightnessSlider(sender: NSSlider) {
verticalSlider.floatValue = getBrightnessLevel()
setBrightnessLevel(sender.floatValue)
}
If I remove the line trying to get the brightness, I can adjust the brightness level from the slider, but it always starts out as maxed out.
How can I have the slider be both adjustable and always reflect the brightness level?
Also, here's a link to the full source if it's helpful.
EDIT:
I'm able to have the slider display the correct brightness when I click on the menu with the following code, but it still doesn't update while it's open. Also, updating while it's closed seems like a bad idea.
NSTimer.scheduledTimerWithTimeInterval(0.2, target: self, selector: Selector("updateBrightnessSlider"), userInfo: nil, repeats: true)
Adding NSRunLoop.currentRunLoop().addTimer(timer, forMode: NSRunLoopCommonModes) seems to work. Thus, the full snippet is as follows:
let timer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: Selector("updateBrightnessSlider"), userInfo: nil, repeats: true)
NSRunLoop.currentRunLoop().addTimer(timer, forMode: NSRunLoopCommonModes)
func updateBrightnessSlider() {
verticalSlider.floatValue = getBrightnessLevel()
}

Detecting user inactivity in any VC

I need to be able to keep track of the last user activity (touch on screen). I found this question and answer here however this does not work for me in swift at all. idleTimer.release is not a thing and I just get an error. Also, I am assuming that you have to create an NSTimer since idleTimer is not a thing either unless you make it. I am also confused by the line
idleTimer = [[NSTimer scheduledTimerWithTimeInterval:maxIdleTime target:self selector:#selector(idleTimerExceeded) userInfo:nil repeats:NO] retain];
So, how can I accomplish this in swift? A bit frustrated!
Here's how I do it in my app. Create the timer to check every so often:
self.timer = NSTimer.scheduledTimerWithTimeInterval(
10, target: self, selector: "decrementScore", userInfo: nil, repeats: true)
Now we know that if decrementScore() is called, 10 seconds of idle time went by. If the user does something, we need to restart the timer:
func resetTimer() {
self.timer?.invalidate()
self.timer = NSTimer.scheduledTimerWithTimeInterval(
10, target: self, selector: "decrementScore", userInfo: nil, repeats: true)
}
For each view controller you want to track the last user activity of, create a property with either a NSDate or a NSTimeInterval.
Then, make liberal use of either NSResponder or UIResponder (you didn't specify whether or not you're doing this in iOS or MacOS) methods in your view controller subclass, which is what each application, window and view is built on top of.
UIResponder, for example, has methods for "touchesBegan:withEvent:", and you can reset your view controller's date property each time a touch happens in the view.