The iOS file system

The iOS file system
The sandbox
Subfolders of the sandbox
Writing and reading to the file system

“sandbox”: where the app keeps all of its “stuff”.

Sandbox
Bundle Container – MyApp.app
-> executable code, resources
Data Container – Documents, Library, Temp
-> user data(Documents/Inbox), non-user data(Library/Applications Support, Library/Caches)
iCloud Container – …

Library/Preferences/info.myapp.mobile.plist

*Documents: important stuff
*Caches:
*Library:

1. Find where the sandbox is.
2. Write to a file

-NSFileManager to get the path to the sandbox
-String to write or read text files
-NSData to write or read binary files

func sandboxPlayground(){
	let fm = FileManager.default
	let urls = fm.urls(for:.documentDirectory, in: .userDomainMask)
	let url = urls.last?.appendingPathComponent("file.txt")

	do {
		try "Hi There!".write(to: url!, atomically: true, encoding: String.Encoding.utf8)
	} catch { 
		print("Error while writing")
	}

	do {
		let content = try String(contentsOf: url!, encoding: String.utf8)

		if content == "Hi There!"{
			print("yay")
		} else {
			print("oops")
		} 
	} catch {
		print("Something went wrong")
	}

}

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
	
	sandboxPlayground()
	return true
})

ViewController.swift

func checkIfFirstLaunch(){
	if UserDefaults.standard.bool(forKey: "HasLaunchedBefore"){
		print("App has launched before")
	} else {
		print("This is the first launch ever!")
		UserDefaults.standard.set(true, forKey: "HasLaunchedBefore")
		UserDefaults.standard.set(0.0, forKey: "Slider Value Key")
		UserDefaults.standard.synchronize()
	}

	func application(_ application: UIApplication, willFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool{
		print("App Delegate: will finish launching")
	}

}
override func viewDidLoad(){
	super.viewDidLoad()

	do {
		audioPlayer = try AVAudioPlayer(contentsof: receivedAudio.filePathUrl as URL)
	} catch _ {
		audioPlayer = nil
	}
	audioPalyer.enableRate = true

	audioEngine = AVAudioEngine()
	do {
		audioFile = try AVAudioFile(forReading: receivedAudio.filePathUrl as URL)
	} catch _ {
		audioFile = nil
	}

	sliderView.value = UserDefaults.standard.float(forKey: SliderValueKey)

	setUserInterfaceToPlayMode(false)
}

	@IBAction func playAudio(_ sender: UIButton){
		let pitch = sliderView.value

		playAudioWithVariablePitch(pitch)
		setUserInterfaceToPlayMode(true)

		UserDefaults.standard.set(sliderView.value, forKey: SliderValueKey)
	}

Before we diveinto

default database <- system-wide feault, language defaults, app-specific defaults NSUserDefaults.standardUserDefaults() -> shared defaults object

key, value
“address” -> “P. Sherman, 42 Wallaby Way, Sydney”

NSUserDefaults.standardUserDefaults().valueForKey(key:String)

stringForKey(key:String), boolForKey(key:String), floatForKey(key:String)

NSUserDefaults.standardUserDefaults().setValue(AnyObject?, key:”keyString”)

import UIKit
import Foundation

class ViewController: UIViewController {
	@IBOutlet var mainView: UIView!
	@IBOutlet weak var midnightThemeLabel: UILabel!
	@IBOutlet weak var themeSwitch: UISwitch!
	@IBOutlet weak var titleLabel: UILabel!
	@IBOutlet weak var imageView: UIImageView!

	override func viewDidLoad(){
		super.viewDidLoad()
		if let weWantMidnight = NSUserDefaults.standardUserDefaults().valueForKey("midnightThemeOn") {
			if weWantMidnight as Bool {
				switchToMidnight()
				print("we lik midnight")
			} else {
				print("we like daylight")
			}
		} else {
		NSUserDefaults.standardUserDefaults().setValue(false, forKey:"midnightThemeOn")
		print("This is the first launch ever!")
		}
	}
}

Persistence

Persistence: Saving data to a place where it can be re-accessed and retrieved upon restart of the device or app.

Necessary for any app that wants to store data log term and keep it available to the user.

NSUser Defaults
-essentially, a persistent dictionary
-user preferences
-saves data in plist file
-data, string, number, data, array, dictionary
-keep NSUserDefaults < 1MB 1.Store thousands of notes, images, and geolocations 2. Store m4v files 3. Store TableViewCollectionView user preference

Git log

Forking a repository
Reviewing another developer’s changes
Knowing what changes to make

Forking is an action that’s done on a hosting service, like GitHub. Forking a repository creates an identical copy of the original repository and moves this copy to your account. You have total control over this forked repository. Modifying your forked repository does not alter the original repository in any way.

GoogleChrome lighthouse
https://github.com/GoogleChrome/lighthouse

[vagrant@localhost ~]$ mkdir lighthouse
[vagrant@localhost ~]$ cd lighthouse
[vagrant@localhost lighthouse]$ git clone https://github.com/GoogleChrome/lighthouse.git
Cloning into ‘lighthouse’…
remote: Counting objects: 22863, done.
remote: Compressing objects: 100% (9/9), done.
remote: Total 22863 (delta 3), reused 2 (delta 2), pack-reused 22852
Receiving objects: 100% (22863/22863), 16.75 MiB | 998.00 KiB/s, done.
Resolving deltas: 100% (16111/16111), done.
Checking connectivity… done.
[vagrant@localhost lighthouse]$ ls
lighthouse
[vagrant@localhost lighthouse]$ cd lighthouse
[vagrant@localhost lighthouse]$ ls
AUTHORS chrome-launcher lighthouse-core package.json yarn.lock
CONTRIBUTING.md docs lighthouse-extension plots
LICENSE jsconfig.json lighthouse-logger readme.md
assets lighthouse-cli lighthouse-viewer typings

git shortlog displays an alphabetical list of names and the commit messages that go along with them. If we just want to see just the number of commits that each developer has made, we can add a couple of flags: -s to show just the number of commits (rather than each commit’s message) and -n to sort them numerically (rather than alphabetically by author name).

Filter By Author
$ git log –author=Surma

$ git show 5966b66

Sending Commits

To send local commits to a remote repository you need to use the git push command. You provide the remote short name and then you supply the name of the branch that contains the commits you want to push

$ git push

$ git pull origin master

html {
  box-sizing: border-box;
  height: 100%;
}

*,
*::before,
*::after {
  box-sizing: inherit;
}

body {
  display: flex;
  margin: 0;
  height: 100%;
}

.container {
  margin: auto;
  padding: 1em;
  width: 80%;
}

.destination-container {
  display: flex;
  flex-flow: wrap;
  justify-content: center;
}

.destination {
  background: #03a9f4;
  box-shadow: 0 1px 9px 0 rgba(0, 0, 0, 0.4);
  color: white;
  margin: 0.5em;
  min-height: 200px;
  flex: 0 1 200px;
  display: flex;
  justify-content: center;
  align-items: center;
  text-align: center;
}
.destination:hover h2{
  transform: rotate(0deg);
}

h2 {
  margin: 0;
  transform: rotate(-45deg);
  transition: transform 0.5s;
  text-shadow: 0 0 5px #01579b;
}

#florida {
  background-color: #03a9f4;
}

#paris {
  background-color: #d32f2f;
}

$ git fetch origin master
$ git merge origin/master
$ git pull
to retrieve and automatically merge updates

Hosting on GitHub

Now the problem with GitHub is that the name is so similar to Git that people sometimes conflate Git and GitHub and think they’re the same thing when they’re actually quite different.

-Git is a version control tool
-GitHub is a service to host Git projects

https://git-scm.com/book/en/v2/Git-Basics-Working-with-Remotes#_showing_your_remotes

git commit

[vagrant@localhost my-travel-plans]$ git add README.md
[vagrant@localhost my-travel-plans]$ git commit -m “first commit”
[master (root-commit) e226c9a] first commit
1 file changed, 2 insertions(+)
create mode 100644 README.md
[vagrant@localhost my-travel-plans]$ git status
On branch master
Untracked files:
(use “git add …” to include in what will be committed)

app.css
index.html

nothing added to commit but untracked files present (use “git add” to track)
[vagrant@localhost my-travel-plans]$ git commit -am “second commit”
On branch master
Untracked files:
app.css
index.html

nothing added to commit but untracked files present
[vagrant@localhost my-travel-plans]$ git add app.css
[vagrant@localhost my-travel-plans]$ git add index.html
[vagrant@localhost my-travel-plans]$ git commit -m “second commit”
[master f5e2913] second commit
2 files changed, 77 insertions(+)
create mode 100644 app.css
create mode 100644 index.html
[vagrant@localhost my-travel-plans]$ git status
On branch master
nothing to commit, working directory clean