Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Building a Modern iOS App

Building a Modern iOS App

It's easy to rely on tools and techniques you've always used, but if you do that you're missing out on all the great new features Apple has introduced in newer iOS versions. In this session you'll learn how to take advantage of features introduced in iOS 9, 10, and 11 to make your apps look and work better with little effort.

Paul Hudson

April 17, 2018
Tweet

More Decks by Paul Hudson

Other Decks in Programming

Transcript

  1. ▸ @twostraws on Twitter ▸ twostraws on GitHub ▸ twostraws

    on Reddit ▸ twostraws on StackOverflow ▸ [email protected] GET IN TOUCH!
  2. Conditional conformances Synthesized Equatable Key decoding strategy Recursive constraints canImport(),

    targetEnvironment() Derived enum collections #warning and #error @dynamicMemberLookup
  3. ▸ Writing modern Swift ▸ Modern layout techniques ▸ Using

    new APIs ▸ As much code as possible! THE PLAN
  4. 4.1 Key encoding and decoding strategies let encoder = JSONEncoder()

    encoder.keyEncodingStrategy = .convertToSnakeCase let encoded = try encoder.encode(someObject) myPropertyName > my_property_name
  5. 4.1 Conditional conformances extension Array: Buying where Element: Buying {

    func buy() {
 for item in self {
 item.buy()
 }
 } }
  6. 4.2 Derived collections of enum cases enum Pasta: CaseIterable {

    case cannelloni, fusilli, linguine, tagliatelle } for shape in Pasta.allCases {
 print("I like eating \(shape).")
 }
  7. let old1 = String(data: data, encoding: .utf8) let new1 =

    String(decoding: data, as: UTF8.self) let old2 = str.data(using: .utf8) let new2 = Data(str.utf8) let old3 = characters[3..<characters.count] let new3 = characters[3...] scores["Sophie", default: 0] += 1
  8. AUTO LAYOUT It lets you describe the relationship between your

    objects! It automagically adapts so that text in other languages fits great! It even has its own neat visual format language! H:|[w00t]
  9. The next time you had a tricky layout problem… “I

    know… I’ll use Auto Layout!”
  10. UIStackView Horizontal or vertical Can be nested Adjustable distribution Inter-item

    spacing Can be animated Non-scrolling LinearLayout Available iOS 9 Android 1
  11. let stackView = UIStackView() stackView.axis = .vertical addSubview(stackView) for _

    in 0 ..< 10 { let vw = UIButton(type: .system) vw.setTitle("Button", forState: .normal) stackView.addArrangedSubview(vw) }
  12. override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection? ) { let sc =

    traitCollection.horizontalSizeClass if sc == .compact { stackView.axis = .vertical } else { stackView.axis = .horizontal } }
  13. ▸ Sharing content ▸ Web views ▸ Wide color ▸

    Rich notifications ▸ 3D Touch ▸ Spotlight USING NEW APIs
  14. import Social @IBAction func shareTapped() { let vc = SLComposeViewController(

    forServiceType: SLServiceTypeTwitter ) vc?.setInitialText("Hello, social!") present(vc, animated: true) }
  15. @IBAction func shareTapped() { let activityVC = UIActivityViewController( activityItems: ["Hello,

    social!"], applicationActivities: nil ) present(activityVC, animated: true) }
  16. let r = CGRect(x: 0, y: 0, width: 256, height:

    256) UIGraphicsBeginImageContextWithOptions( r.size, false, 0 ) let ctx = UIGraphicsGetCurrentContext() UIColor.blue.set() ctx?.fillEllipse(in: r) let img = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext()
  17. let r = CGRect(x: 0, y: 0, width: 256, height:

    256) let renderer = UIGraphicsImageRenderer(size: r.size) let img = renderer.image { ctx in UIColor.blue.set() ctx.cgContext.fillEllipse(in: r) }
  18. let icon = UIApplicationShortcutIcon(type: .compose) let item = UIApplicationShortcutItem( type:

    "com.angry.rantwriter.compose", localizedTitle: "Write Rant”, localizedSubtitle: "Get angry right now", icon: icon, userInfo: nil ) UIApplication.shared.shortcutItems = [item]
  19. let item = launchOptions?[.shortcutItem] if let shortcut = item as?

    UIApplicationShortcutItem { if shortcut.type == “com.you.yourItemID" { startComposing() } }
  20. func detailViewController(for index: Int) -> DetailViewController { guard let vc

    = storyboard?.instantiateViewController( withIdentifier: "Detail" ) as? DetailViewController else { fatalError("Couldn't load view controller") } vc.selectedImage = pictures[index] return vc }
  21. func previewingContext( _ previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint ) ->

    UIViewController? { if let i = tableView.indexPathForRow(at: location) { previewingContext.sourceRect = 
 tableView.rectForRow(at: i) return detailViewController(for: i.row) } return nil }
  22. func previewingContext( _ previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint ) ->

    UIViewController? { if let i = tableView.indexPathForRow(at: location) { previewingContext.sourceRect = 
 tableView.rectForRow(at: i) return detailViewController(for: i.row) } return nil }
  23. func previewingContext( _ previewingContext: UIViewControllerPreviewing, commit viewControllerToCommit: UIViewController ) {

    navigationController?.pushViewController(
 viewControllerToCommit, animated: true ) }
  24. let note = UILocalNotification() note.soundName = UILocalNotificationDefaultSoundName note.alertTitle = "Hey,

    listen!" note.fireDate = Date().addingTimeInterval(3600) UIApplication.shared.scheduleLocalNotification(note)
  25. let content = UNMutableNotificationContent() content.title = "Hey, listen” content.sound =

    UNNotificationSound.default() let trigger = UNTimeIntervalNotificationTrigger( timeInterval: 3600, repeats: false )
  26. let index = CSSearchableIndex.default() index.indexSearchableItems([item]) { error in if let

    error = error { print("Error: \(error.localizedDescription)") } else { print("Successfully indexed!") } }
  27. ▸ Sharing content ▸ Web views ▸ Wide color ▸

    Rich notifications ▸ 3D Touch ▸ Spotlight USING NEW APIs
  28. ▸ Writing modern Swift ▸ Modern layout techniques ▸ Using

    new APIs ▸ As much code as possible! THE PLAN
  29. ▸ Swift keeps marching on. ▸ “Convert to Current Swift

    Syntax” isn’t enough. ▸ Improvements mean you can write less code – fewer bugs! ▸ Some changes are subtle. MODERN SWIFT
  30. ▸ The safe area is a requirement. ▸ Auto Layout

    is good; stack views are better. ▸ Use PDFs for artwork. ▸ Split asset catalogs up to help keep them organized. MODERN LAYOUT
  31. ▸ Your users spent $1000 on their phone – use

    it fully! ▸ It’s obvious when apps miss 3D Touch and Wide Color. ▸ Integrate into the system: Spotlight, notifications. USING NEW APIs