Khoa Pham
Khoa Pham

Ohayo

Swift Discovery

Discover all the tech

Featured

My year in review 2020

Issue #715

I remember this time last year in December 2019, I spent almost every single bit of my free time on Puma because I want a Swift friendly version of fastlane that suits my need and leverages Swift 5 features.

Here’s my review of my …

How to use Firebase AutoML Vision Edge to classify images

Issue #490

Create project on Firebase and choose Vision Edge

Vision Edge is part of MLKit, but for custom images training

https://console.firebase.google.com/u/0/project/avengers-ad2ce/ml/

firebase vision edge firebase dataset

Model Avengers_dataset_2019114133437 is training and may …

How to use Google AutoML to classify images

Issue #489

Create bucket on Google Cloud Storage

https://console.cloud.google.com/storage

cloud storage

Create dataset by uploading images to Google AutoML Vision

https://console.cloud.google.com/vision

automl dataset automl images

How to get Binding via dollar prefix in SwiftUI

Issue #488

The dollar is not a prefix, it seems to be a generated code for property wrapper, and each kind of property wrapper can determine which value it return via this dollar sign

State and ObservedObject are popular property wrappers in SwiftUI …

How to modify state from state in SwiftUI

Issue #487

In case we have to modify state when another state is known, we can encapsulate all those states in ObservableObject and use onReceive to check the state we want to act on.

See code Avengers

If we were to modify state from within body …

How to show loading indicator in SwiftUI

Issue #486

import SwiftUI

struct ActivityIndicator: UIViewRepresentable {
    @Binding var isAnimating: Bool
    let style: UIActivityIndicatorView.Style

    func makeUIView(context: UIViewRepresentableContext<ActivityIndicator>) -> …

How to show image picker in SwiftUI

Issue #485

The easiest way to show image picker in iOS is to use UIImagePickerController, and we can bridge that to SwiftUI via UIViewControllerRepresentable

First attempt, use Environment

We conform to UIViewControllerRepresentable and make a …

How to add monkey test to iOS apps

Issue #484

Use SwiftMonkey which adds random UITests gestures

Add to UITests target

target 'MyAppUITests' do
  pod 'R.swift', '~> 5.0'
  pod 'SwiftMonkey', '~> 2.1.0'
end

Troubleshooting

Failed to …

How to use array of strings in ForEach in SwiftUI

Issue #483

Every item in list must be uniquely identifiable

List {
    ForEach(books, id: \.bookId) { book in
        NavigationLink(destination:
            BookView(book: book)
                .navigationBarTitle(book.name)
        ) { …

How to make multiline Text in SwiftUI in watchOS

Issue #482

lineLimit does not seem to work, use fixedSize instead

Fixes this view at its ideal size.

A view that fixes this view at its ideal size in the dimensions given in fixedDimensions.

extension Text {
    func styleText() -> some View { …

How to show documentations for GitHub projects

Issue #481

  • Use jazzy to generate documentation which the generated artifacts in docs folder
  • GitHub has a nifty feature to use docs as GitHub pages
Screenshot 2019-10-31 at 10 49 35

How to use CommonCrypto in iOS

Issue #480

Use modulemap

modulemap approach

I use modulemap in my wrapper around CommonCrypto https://github.com/onmyway133/arcane, https://github.com/onmyway133/Reindeer

For those getting header not found, please take a look …

How to make ISO 8601 date in Swift

Issue #479

From ISO8601 spec, the problems are the representation and time zone

ISO 8601 = year-month-day time timezone
For date and time, there are basic (YYYYMMDD, hhmmss, ...) and extended format (YYYY-MM-DD, hh:mm:ss, ...)
Time zone can be Zulu, …

How to configure test target in Xcode

Issue #478

This applies to

  • Main targets
    • App
    • Framework
  • Test targets
    • Unit tests
    • UI tests

Examples

Dependencies used

Examples

  • Cocoapods
  • Carthage

Notes

  • Make sure test target can link to all the …

How to check platform versions in Swift

Issue #477

Mark APIs availability

@available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
public extension View {

}

Check platform

#if canImport(UIKit)
import UIKit
#elseif canImport(OSX)
import AppKit
#endif

In watchOS app, it still can import …

How to use react-native link and CocoaPods

Issue #476

React Native comes with a default React library, and most CocoaPods libraries for React Native has React as a dependency pod, which causes the conflict

How to flick using UIKit Dynamic in iOS

Issue #475

For a snack bar or image viewing, it’s handy to be able to just flick or toss to dismiss

We can use UIKit Dynamic, which was introduced in iOS 7, to make this happen.

Use UIPanGestureRecognizer to drag view around, UISnapBehavior to …

How to use Swift package manager in watchOS

Issue #474

SPM

Go to Project -> Swift Packages, add package. For example https://github.com/onmyway133/EasyStash

Select your WatchKit Extension target, under Frameworks, Libraries and Embedded Content add the library

CocoaPods

If we use CocoaPods, …

How to use external display in iOS

Issue #473

Before iOS 13

Use UIScreen.didConnectNotification

NotificationCenter.default.addObserver(forName: UIScreen.didConnectNotification,
                         object: nil, queue: nil) { (notification) in
        // Get the new screen …

How to show error message like Snack Bar in iOS

Issue #472

Build error view

Use convenient code from Omnia

To make view height dynamic, pin UILabel to edges and center

import UIKit

final class ErrorMessageView: UIView {
    let box: UIView = {
        let view = UIView()
        view. …

How to hide tab bar when push in iOS

Issue #471

let navigationController = UINavigationController(rootViewController: viewControllerA)
navigationController.pushViewController(viewControllerB, animated: true)

In view controller B, need to set hidesBottomBarWhenPushed in init

final class …

Khoa Pham

Hello, I’m Khoa

I’m a thinker and storyteller with a passion for exploring the intersection of creativity and technology

🧑‍💻 I love crafting high quality and useful apps
🔥 I love open source. My GitHub open source has 2.3k followers with packages that are integrated by 45k+ apps and over 3.4m+ downloads on CocoaPods.
✍️ I write here on my blog and on Medium, which has over 2.7k+ followers with tons of articles and 90k+ monthly views.
🖥 Follow me for sharings about Swift, SwiftUI, iOS and macOS development.
Hei