What we will do:
- implement messages about shot coordinates, results etc. as well as use user choice from settings how hit information should be displayed - hit or miss only or full;
- implement autosave and continue last game;
- add a launch screen (splash screen) - static screen displayed during application start. From May 2020 this I a required component if you want to submit your app to AppStore;
- add localization - show how to add Polish translation for English strings used so far.
Details for each step:
- Download source code for this chapter (download source code).
- About 1
This steps needs some changes inEngineGameBattleship
. Some of them are inBoard.swift
file
12private func afterHitAction(row: Int, col: Int) -> (result: EngineGameBattleship.ShotResult, ship: Ship)?func shot(row: Int, col: Int) -> (result: EngineGameBattleship.ShotResult, ship: Ship)?
andEngineGameBattleship.swift
1func shot(who: Who, row: Int, col: Int) -> (result: EngineGameBattleship.ShotResult, ship: Ship?)
but check carefully what has beed changed. Of course some changes are also inGameBattleshipIOS
part.When implemented, results should be as in this film (download video).
- About 2
Add an outlet for "Continue last game" button (download video).We use, as before for saving information about ship sets, game definitions (both in previous tutorial), and settings (just before) a
Codable
protocol but this time it needs some customization as we want to save data being ofenum
typy which does not conformCodable
protocol. So me must take some actions to fix this.Please read about this here:
- NSCoding Tutorial for iOS: How to Permanently Save App Data
- Encoding and Decoding in Swift
- Codable enums in Swift
Changes are mede (mostly) in:
Ship
(start analyzing from this as the most embedded and the easiest)Ships
- only minor changes and conforms automaticaly when Ship conformsBoards
- copy approach from Ship
Then we need to save also information about whose turn was as last. Because it is a smal amount of data, we will use
UserDefaults
. For explanation , please refer to:- How to save user settings using UserDefaults
- UserDefaults iOS Tutorial
- Short summary in: How can I use UserDefaults in Swift?
- UserDefaults in Swift 5.1
More complete tutorial about saving data in games
Data storing options summary
Nice introduction to CoreData (you may think of this like an internal, built-in database)
You may also find useful this:
If you are interested in secure save data you may read about some concepts here (an outdated text but main concepts remains)
I made some decision where, in my opinion, is the best place to put the call to game state saving code: look into
SceneDelegate.swift
and1234func sceneWillResignActive(_ scene: UIScene) {// Called when the scene will move from an active state to an inactive state.// This may occur due to temporary interruptions (ex. an incoming phone call).}We will not use
1234func sceneDidBecomeActive(_ scene: UIScene) {// Called when the scene has moved from an inactive state to an active state.// Use this method to restart any tasks that were paused (or not yet started) when the scene was inactive.}It would be good if you will read some material related to iOS lifecycle and make your own choice:
When all done in this step, results should be as in this film (download video)
- About 3
Watch video and repeat in your project all the steps (download video).You may also want to read these materials
- About 4
Watch video and repeat in your project all the steps (download video).