Khoa Pham

Khoa Pham

from Oslo 935 posts
I’m open source contributor, writer, speaker and product maker.

How to observe optional ObservableObject in SwiftUI

Issue #988

When working with Core Data, there are times we have optional NSManagedObject to pass around. These objects conform to ObservableObject, and in SwiftUI we can’t @ObservedObject on optional ObservableObject

One way we can workaround …

How to clear background for TextField inside list in macOS

Issue #986

When using TextField in SwiftUI List on Mac, it has unwanted background color when focused. We can turn it off using introspection or a custom TextField wrapper

TextField("Search", text: $searchText)
    .introspect(.textField, on: …

How to use GitHub Copilot for Xcode

Issue #985

During GitHub Universe 2024, GitHub announced that GitHub Copilot code completion in Xcode is available in public preview. The project is open source at CopilotForXcode

image

GitHub Copilot has been available as VS Code extension for a while, …

How to conditionally render widgets in iOS

Issue #984

The WidgetBundle lets us expose multiple widgets from a single widget extension

It uses WidgetBundleBuilder to constructs a widget bundle’s body.

In iOS 18, if we include ControlWidget then we need to check iOSApplicationExtension iOS 18. …

How to open app with Control Widget on iOS 18

Issue #983

In iOS 18, we can make Control Widget in Widget extension

import WidgetKit
import SwiftUI

@available(iOS 18.0, *)
struct BookControlWidget: ControlWidget {
    var body: some ControlWidgetConfiguration {
        StaticControlConfiguration …

How to use NSFetchedResultsController memory wise in Core Data

Issue #982

If you’re using NSFetchedResultsController in Core Data, it might take up a lot of memory, especially when working with large datasets. To keep your app running smoothly, it’s important to manage memory efficiently

Use Fetch Limits and …

How to use NSDragOperation

Issue #981

NSDragOperation represent which operations the dragging source can perform on dragging items.

There are several types of drag operations, and each one has a different purpose and visual cue.

Copy Operation .copy

  • What It Does: The item …

How to make NSCollectionView with diffable data source and SwiftUI

Issue #980

NSCollectionView, available since macOS 10.5+, is a good choice to present a list of content. Let’s make a SwiftUI wrapper for NSCollectionView with diffable data source and compositional layout

Use NSViewControllerRepresentable …

How to use React Query useQuery with debounce

Issue #979

When dealing with user input, such as in an autocomplete component, it’s common to implement debouncing to reduce the number of API calls and improve the user experience.

React Query’s useQuery hook makes it easy to manage the …

How to handle tap gesture in SwiftUI Charts

Issue #978

From iOS 17, SwiftUI Charts has chartGesture, together with SpatialTapGesture we can check tap location and convert that to Charts value

Chart {}
    .chartGesture { chart in
        SpatialTapGesture()
            .onEnded { value in …

How to sign in with Apple and Firebase and Supabase

Issue #977

Show ASAuthorizationController with a simple nonce. Once we have the idToken, create an OAuthProvider.appleCredential and pass to Firebase Auth

final class AppleLoginService: NSObject {
    static let shared = AppleLoginService() …

How to serve a local development environment over https using pnpm and webpack

Issue #976

When developing locally, especially when interacting with third-party services that have CORS restrictions, serving your development environment over a custom domain with HTTPS can be crucial. Let’s walk through the steps to achieve this …

How to use React Custom Hooks as the View Model pattern

Issue #975

When building a React application, separating the logic and state management from the UI can make your code easier to manage, test, and reuse. This is where the view model pattern comes in handy. By using a custom hook as a view model, you …

How to use memory in lowdb

Issue #974

In lowdb 7, we can use MemorySync https://github.com/typicode/lowdb/blob/main/src/examples/in-memory.ts

import { LowSync, MemorySync, SyncAdapter } from '../index.js'
import { JSONFileSync } from '../node.js'

declare …

How to detect Barcode and QR code

Issue #973

Before iOS 11, we used to use CIDetector and CIDetectorTypeQRCode to detect QR code

CIDetector

An image processor that identifies notable features, such as faces and barcodes, in a still image or video.

CIDetectorTypeQRCode

A detector …

How to make swifty UserDefaults

Issue #972

We want to have a swifty UserDefaults API that works with subscript and in a type safe manner.

extension Defaults.Keys {
    static let string = Defaults.Key("string", default: "0")
}

XCTAssertEqual(defaults[.string], …

How to use act vs waitFor using React Testing Library

Issue #971

When testing React components, dealing with tasks that happen at different times is super important to make sure your tests give reliable results. React Testing Library gives you two important tools for dealing with these situations: act …

How to use OSLog and OSLogStore in Swift

Issue #970

We can use Logger to log and OSLogStore to retrieve logs

import OSLog
import ReuseAcross

final class LogService {
    static let shared = LogService()
    
    let logger = Logger(
        subsystem: "com.example.myapp", …

How to include custom error payload in hapi Boom

Issue #969

Hapi.js, commonly referred to as Hapi, is an open-source, back-end web application framework for building and deploying web applications and APIs in Node.js

In Hapi.js, you can use the Boom module to create and return error responses in a …

How to read image paste from clipboard in React

Issue #968

Are you looking to grab images directly from your clipboard with a button click on your web page? The Async Clipboard API makes this quite easy and efficient. Let’s break it down into simpler steps:

Requesting Permission

The first …

How to create user gitignore

Issue #967

Git is a helpful tool for managing code and projects, but sometimes you want to ignore certain files or folders only on your computer without affecting everyone else. That’s where the .user_gitignore file comes in. It allows you to …

How to use useCallback in React

Issue #966

The use of useCallback and useMemo in React hooks is an adaptation to address certain limitations inherent in the functional programming style adopted by React. In JavaScript, every entity, whether it’s a function, variable, or any …

How to extend class in Javascript

Issue #965

In JavaScript, classes are a template for creating objects. They encapsulate data with code to work on that data. ES6 introduced a class syntax to the JavaScript language to create classes in a way that’s similar to other …

How to use export all and export default in Javascript

Issue #964

In JavaScript, particularly in modules used in frameworks like React, export statements are used to expose code—such as functions, classes, or constants—from one module so that they can be imported and reused in other modules.

export *

The …

How to escape characters in json and regex with Swift string

Issue #963

In the world of Swift programming, we often come across situations where we need to work with string literals that contain special characters. These characters can include new lines, tabs, backslashes, and quotes — all of which need to be …

How to use nextjs Image

Issue #962

Fill parent div

A boolean that causes the image to fill the parent element, which is useful when the width and height are unknown.

The parent element must assign position: “relative”, position: “fixed”, or position: …

How to check NSTextField is first responder

Issue #961

NSTextField uses NSFieldEditor under the hood, you can check currentEditor if it is the firstResponder

extension NSTextField {
    var isFirstResponder: Bool {
        currentEditor() == window?.firstResponder
    }
}

Building an iOS camera calculator with Core ML’s Vision and Tesseract OCR

Issue #960

Also written on Fritz


Math might be scary, but it’s an essential part of everyday life. Wouldn’t it be cool if we could build an app, point our phone’s camera at an expression, and let the app compute the result? Whenever I’ve needed to …

How to decode dynamic JSON key with JSONDecoder

Issue #959

Decoding JSON in Swift is most of the time very straightforward with help of Codable protocol and JSONDecoder.

Sometimes the json contains dynamic key, like

{
    "shipmunk": {
        "name": "Shipmunk", …

How to bundle js for use in JavaScriptCore in Swift

Issue #958

We can use any bundler, like Parcel, Webpack or Vite. Here I use Webpack 5

Install Webpack and Babel

npm install @babel/polyfill webpack webpack-cli --save-dev

@babel/polyfill is a package provided by Babel, a popular JavaScript compiler. …

How to handle log in JSContext with JavascriptCore

Issue #957

Define console object and set log function to point to our Swift function

import JavaScriptCore

extension JSContext {
    func injectConsoleLog() {
        
        evaluateScript(
        """
            var console = {}; …

How to make attributed TextView for macOS and iOS with SwiftUI

Issue #956

macOS

import Foundation
import SwiftUI
import AppKit

struct AttributedTextView: NSViewRepresentable {
    @Binding var attributedText: NSAttributedString
    var isEditable: Bool = true
    
    final class Coordinator: NSObject { …

Apple Developer Learning resources

Issue #955

Besides WWDC videos & documentation, Apple also has interactive tutorials and books. Below are some of my favorites learning resources

Tutorials

How to show anchor bottom view in SwiftUI

Issue #954

From iOS 15, there’s a handy safeAreaInset that allows us to place additional content extending the safe area.

Shows the specified content beside the modified view.

safeAreaInset allows us to customize which edge and alignment we …

How to debounce text input in React

Issue #953

Use debounce from lodash and useCallback to memoize debounce function

import React, { useCallback } from "react"
import debounce from "lodash/debounce"

const SearchField = (props: Props) => {
    const callback = …

How to remove duplicates in Javascript array while keeping latest occurrence?

Issue #952

Use ES6 Map

The Map object holds key-value pairs and remembers the original insertion order of the keys. Any value (both objects and primitive values) may be used as either a key or a value.

type Deal = {
    name: string,
    link: …

How to drag multiple files in SwiftUI on Mac

Issue #951

Create a custom NSView that handles mouseDragged to beginDraggingSession

struct DraggingSourceViewWrapper: NSViewRepresentable {
    let fileUrls: [URL]
    let onEnd: () -> Void
    
    func makeNSView(context: Context) -> …

How to dynamically build tailwind class names

Issue #950

Inspired by shadcn

Combine

  • tailwind-merge: Utility function to efficiently merge Tailwind CSS classes in JS without style conflicts.
  • clsx: constructing className strings conditionally.
import { clsx, type ClassValue } from "clsx" …

How to store Codable in AppStorage

Issue #949

AppStorage and SceneStorage accepts RawRepresentable where value is Int or String.

Creates a property that can read and write to a string user default, transforming that to RawRepresentable data type.

init(wrappedValue:_:store:)

init( …

How to update widget for iOS 17

Issue #948

iOS 17 has a new Stand by mode so SwiftUI introduces containerBackground for the system to decide when to draw background. It also automatically applies margin to widget so we may need to disable that

To update existing widgets, we can …

How to get Supabase user token from server API

Issue #947

Sometimes Row Level Security is not enough and we want to do all logic server side, then we need a way for the server to get hold onto current user token.

Send JWT token in Authorization header

From client, we can get session from supabase …

How to mirror auth.users on Supabase

Issue #946

For security purposes, the auth schema is not exposed on the auto-generated API. We can make a profiles table in public namespace and mirror data from auth.users when user signs up.

I need id, username and raw_user_metadata so I will …

How to force localized language with SwiftGen

Issue #945

The default SwiftGen generate generated strings L10n file like this

extension L10n {
  private static func tr(_ table: String, _ key: String, _ args: CVarArg..., fallback value: String) -> String {
    let format = BundleToken.bundle. …

How to make tag flow layout using Layout protocol in SwiftUI

Issue #944

SwiftUI in iOS 16 supports Layout protocol to arrange subviews

We need to implement 2 methods

How to use hover annotation in Swift Charts

Issue #943

In this tutorial, we’ll learn how to use Swift Charts to visualize ranking data.

Screenshot 2023-08-18 at 11 48 28

We use default AxisMarks and AxisMarks to let Swift Charts interpolate x and y grid lines. For y axis, I want to have finer grain control over the …

How to use Supabase auth with React Context

Issue #942

Expose supabase with createClient

useSupabase.ts

import { createClient } from '@supabase/supabase-js'

const supabaseUrl = process.env.SUPABASE_URL
const supabaseAnonKey = process.env.SUPABASE_ANON_KEY

export const supabase = …

How to create React app with Parcel

Issue #941

In this tutorial we will be creating a React app with Parcel 2 with Typescript and Tailwind

Install the following dependencies. Parcel supports TypeScript out of the box without any additional configuration.

npm install --save-dev parcel …

How to make Chrome extension with Nextjs 13

Issue #940

We can develop Nextjs 13 apps and export it to a Chrome extension.

Start by init the project

npx create-next-app@latest

Here is the project structure with app router and not using src directory. I put an extension to the root of the …

How to scale image fill without affect layout in SwiftUI

Issue #939

Instead of letting the Image decide the size, we can put it as background or overlay. I use clipped and contentShape to avoid the fill image obscuring touch event

Color.clear
    .frame(height: 100)
    .overlay {
        AsyncImage(url: …

How to move Core Data database to AppGroup folder

Issue #938

To let app and extension to talk to the same database, we need to use AppGroup. Here is how to use replacePersistentStore

Replaces one persistent store with another

actor DatabaseMigrator {
    @AppStorage( …

How to read write files to iCloud Drive

Issue #937

First, you need to enable iCloud Documents capability. Go to target settings -> Signing & Capabilities -> iCloud ` Screenshot 2023-07-28 at 16 09 14

Then inside your Info.plist, add this with your iCloud identifier and app name …

How to make reusable Button component in React

Issue #936

From https://github.com/antonioerdeljac/next13-spotify

import { forwardRef } from "react";
import { twMerge } from "tailwind-merge";

export interface ButtonProps
  extends React.ButtonHTMLAttributes<HTMLButtonElement …

How to use keychain in Swift

Issue #934

There are a few keychain wrappers around but for simple needs, you can write it yourself

Here is a basic implementation. I use actor to go with async/await, and a struct KeychainError to contain status code in case we want to deal with …

How to handle route case sensitivity in Nextjs

Issue #933

By default, Nextjs route is case sensitive, so localhost:3000/About and localhost:3000/about are different routes.

To make uppercase routes become lowercase routes, we can add a middleware.tsx file to the src so it is same level as pages …

How to make share and action extension in iOS

Issue #932

Add Share extension and Action extension respectively in Xcode. We can use the same code to both extension

Screenshot 2023-07-12 at 21 11 41

SwiftUI

I usually make a ShareView in SwiftUI with ShareViewModel to control the logic

struct ShareView: View {
    @ …

AppStore screenshots size checklist

Issue #931

AppStore screenshots

Screenshot specifications

iPhone 6.7"

Portrait 1290 x 2796

iPhone 6.5"

Portrait 1242 x 2688

In-App Purchase screenshots

In-app purchase information

iOS 640 x 920 tvO 1920 x1080 pixels macOS 1280 x 800 pixels

How to use AppIntents in iOS 16

Issue #930

AppIntents

Declare AppShortcutsProvider, note that appShortcuts uses @AppShortcutsBuilder syntax

import AppIntents

struct OurShortcutsProvider: AppShortcutsProvider {
    static var shortcutTileColor: ShortcutTileColor = .lightBlue …

How to press and hold button in SwiftUI

Issue #929

We can use ButtonStyleConfiguration to detect isPressed state

struct RecordButton: View {
    var body: some View {
        Button {
        
        } label: {
            Image(systemSymbol: .micFill)
        }
        .buttonStyle( …

How to copy text to the clipboard in Swift

Issue #928

For iOS, use string

Setting this property replaces all current items in the pasteboard with the new item. If the first item has no value of the indicated type, nil is returned.

let pasteboard = UIPasteboard.general
pasteboard.string = …

How to add launch screen in SwiftUI project

Issue #927

For a plain SwiftUI project, Xcode will generate a Info.plist file once you start to edit the Info tab in target settings.

There is a Launch screen (UILaunchScreen) key by default. It has a UILaunchScreen which we don’t need to so we …

How to create UserDefaults property wrapper in Swift

Issue #926

Like AppStorage, we can make a custom UserDefaults property wrapper that conforms to DynamicProperty

@propertyWrapper
public struct UserDefault<Value>: DynamicProperty {
    @State private var value: Value

    let key: String …

How to encrypt using CryptoKit in Swift

Issue #925

Use AES.GCM method with 128 bits key

import CryptoKit

public extension Optional {
    func tryUnwrap() throws -> Wrapped {
        if let value = self {
            return value
        } else {
            throw NSError(domain: …

How to use NavigationSplitView and NavigationStack in SwiftUI

Issue #924

Note

  • Navigation state needs to be in the container of NavigationSplitView for changes to propagate
  • Need to use WindowGroup for navigation bar to work

NavigationSplitView

the navigation split view coordinates with the List in its first …

How to style NavigationLink in macOS

Issue #923

NavigationLink on Mac applies the default button style. We can style it using ButtonStyle, here to use plain style we can just

NavigationLink(value: DetailRoute.books) {
    BooksView()
}
.buttonStyle(.plain)

SwiftUI EnvironmentValues

Issue #922

EnvironmentValues Views in SwiftUI can react to configuration information that they read from the environment using an Environment property wrapper

Updated for iOS 17

SwiftUI Environment values

How to make TextField Stepper in SwiftUI

Issue #921

Use HStack with TextField and a little extension

extension Binding where Value == Int {
    var toString: Binding<String> {
        Binding<String>(
            get: {
                "\(wrappedValue)"
            }, …

How to clear TextEditor background in SwiftUI

Issue #920

For iOS 16 and macOS 13.0

TextEditor(text: $text)
    .scrollContentBackground(.hidden)

For below, use [SwiftUI-Introspect](https://github.com/siteline/SwiftUI-Introspect)

TextEditor(text: $text)
    .instrospectTextView {
        $0. …

Learning Metal for SwiftUI

Issue #919

WWDC23 introduces lots of new additions to SwiftUI, notably Metal shader support with these modifiers

  • colorEffect: Returns a new view that applies shader to self as a filter effect on the color of each pixel.
  • layerEffect: Returns a new …

WWDC23 SwiftUI Q&A

Issue #918

Interesting SwiftUI Q&A during WWDC23

Observable vs ObservableObject

Q: With the new SwiftUI @Observable macro, are there any cases where ObservableObject would still be a better alternative?

A: Use ObservableObject when you need to …

WWDC21 SwiftUI Q&A

Issue #917

Interesting SwiftUI Q&A during WWDC21

@StateObject or ObservedObject?

Q: Let’s say I have a purely SwiftUI flow. I have a ListView with a @StateObject var listFetcher, that makes requests for a list of items. Tapping on an item in the …

What's new in SwiftUI iOS 17 at WWDC23

Issue #916

WWDC23 brings new additions to SwiftUI

Screenshot 2023-06-07 at 22 02 36

Scrolling

The scroll transition modifier is very similar to the visual effect modifier Curt used earlier for the welcome screen. It lets you apply effects to items in your scroll view.

I’m …

How to render markdown view with React

Issue #914

Use react-markdown to parse markdown content, react-syntax-highlighter to highlight code, and rehype-raw to parse raw html

import ReactMarkdown from "react-markdown"
import { Prism as SyntaxHighlighter } from …

How to create Quick look thumbnail for files

Issue #913

Use QuickLookThumbnailing framework

import AppKit
import QuickLookThumbnailing

actor QuicklookService {
    static let shared = QuicklookService()
    
    private let generator = QLThumbnailGenerator.shared
    
    func image( …

How to deal with actor reentrancy in Swift

Issue #912

Perform check before and after suspension point

actor Worker {
    var isDoing = false
    var toBeDone = Set<String>()
    
    func work(string: String) async {
        if isDoing {
            toBeDone.insert(string) …

How to run parallel Task with Swift concurrency

Issue #911

Make an parallelTask function that wraps TaskGroup

public func parallelTask(@ParallelTaskBuilder builder: () -> [ParallelTaskBuilder.Work]) async {
    await withTaskGroup(of: Void.self) { group in
        for work in builder() { …

How to use Range and NSRange in Swift

Issue #910

Use one-sided range operator

let string = "Hello world"
string[string.startIndex...] // Hello world
string[..<string.endIndex] // Hello world

Substring

let string = "Hello world"
let range = string.startIndex ..< …

How to handle status bar with custom overlay UIWindow

Issue #908

When we add another UIWindow, then its rootViewController will decide the style of the status bar, not the rootViewController of the keyWindow anymore

childForStatusBarStyle

The usual way to fix this is to defer the decision to the correct …

How to use actor in Swift concurrency

Issue #905

Protect mutable state with Swift actors

Actor reentrancy

abc

Imagine we have two different concurrent tasks trying to fetch the same image at the same time. The first sees that there is no cache entry, proceeds to start downloading the …

How Task use thread in Swift concurrency

Issue #904

Consider this code where we have an ObservableObject with fetch1 and async fetch2, and a fetch inside ContentView

Here the observation in Xcode 14

  • ViewModel.fetch1: run on main thread
  • ViewModel.fetch2: run on cooperative thread pool …

How to show animated gif NSImage on Mac

Issue #903

let image = NSImage(contentsOf: url)
let imageView = NSImageView(image: image)
image.animates = true

How to handle shortcut intents in iOS

Issue #902

iOS 13 Intents Extension & Intents UI Extension

Siri can predict shortcuts to actions that a user may want to perform using your app, and suggest those shortcuts to the user in places such as …

How to use Universal Links in iOS

Issue #901

Apple app site association

https://com.example/.well-known/apple-app-site-association

Supporting Associated Domains New format from iOS 13 Can also remove components section if we match all URLs

{
  "applinks": { …

How to find previous frontmost application in macOS

Issue #900

Listen to didActivateApplicationNotification and check that it is not our app

NSWorkspace.shared.notificationCenter
    .publisher(for: NSWorkspace.didActivateApplicationNotification)
    .sink(receiveValue: { [weak self] note in …

How to show view below title bar for macOS in SwiftUi

Issue #899

Use NSTitlebarAccessoryViewController

var titleBarAccessoryVC: NSTitlebarAccessoryViewController {
    let vc = NSTitlebarAccessoryViewController()
    let view = HStack {
        Spacer()
        Button {
            
        } label: { …

How to drag using DragGesture in SwiftUI

Issue #898

Change element position using either offset or position, and use DragGesture

Use GestureState to store the updating startDragLocation to keep the start location when drag begins, so we can add translation

struct MoveModifier: ViewModifier …

How to handle slow large dataset Picker when dragging in SwiftUI

Issue #897

During continuous events like dragging or TextField typing, and we have a Picker onscreen with large data set, it will slow down main thread.

One option is to explicitly conform that view to Equatable

struct FontPicker: View, Equatable { …

How to pass FocusState binding in SwiftUI

Issue #896

Use underscore _focus we get access to underlying FocusState object, but underscore _ is private to a View hence can’t be used in extension

If we want to pass FocusState to another View or in extension, we can pass its Binding

enum …

How to move reversed List in SwiftUI

Issue #895

Apply .move on reversed array

List(selection: $viewModel.selectedBook) {
    ForEach(viewModel.books.reversed()) { book in
        BookCell(book: book)
    }
    .onMove { source, dest in
        var reversed = Array(viewModel.books. …

How to set popoverPresentationController sourceView in SwiftUI

Issue #894

Use a UIView as source view and set it in background

class ViewModel {
    lazy var sourceView = UIView()
}

struct SourceView: UIViewRepresentable {
    let viewModel: ViewModel

    func makeUIView(context: Context) -> UIView { …

My favorite WWDC videos

Issue #891

Below are my favorite WWDC videos. My focus is to use SwiftUI to make useful apps with great UX. I don’t pay much attention to new frameworks as they come and go, but the underlying reasons and design principles are worth …

WWDC swiftui-lounge

Issue #890

In WWDC21, WWDC22 Apple provide a Slack channel https://wwdc22.slack.com/ for people to interact with Apple engineers in digital lounges. Here I note down some interesting Q&As

WWDC22

What’s the difference between a custom ViewModifier …

WWDC22 SwiftUI Q&A

Issue #890

Interesting SwiftUI Q&A during WWDC22

What’s the difference between a custom ViewModifier vs View extension

Q: What’s the difference between a custom ViewModifier (without DynamicProperty) that uses some built-in modifiers in …

What's new in SwiftUI iOS 16 at WWDC22

Issue #889

asda

What’s new in SwiftUI

New EnvironmentValues

abc

TextField inside Alert

abc

List uses UICollectionView

See gist https://gist.github.com/onmyway133/fc08111964984ef544a176a6e9806c18

abc

ButtonStyle composition

Screenshot 2022-06-09 at 10 25 30
Section("Hashtags") { …

How to use any vs some in Swift

Issue #888

Embrace Swift generics

This generic pattern is really common, so there’s a simpler way to express it. Instead of writing a type parameter explicitly, we can express this abstract type in terms of the protocol conformance by writing …

How to convert NSImage to PNG Data

Issue #885

Create NSBitmapImageRep with preferred size and draw NSImage onto that. Need to construct NSBitmapImageRep specifically instead of using convenient initializers like NSBitmapImageRep(data:), NSBitmapImageRep(cgImage:) to avoid device …

How to get notification userInfo at launch

Issue #884

When user taps on push notification, depending on app state

SceneDelegate

Checking UIScene.ConnectionOptions.notificationResponse?.notification.request.content.userInfo in scene(_:willConnectTo:options:)

  • app terminated: sometimes nil
  • app …

How to use popover in SwiftUI

Issue #882

In SwiftUI, .popover shows as popover on Mac and iPad, but as .sheet on iPhone (compact size class)

We can use minWidth, minHeight to specify sizes on Mac and iPad. On iPhone, we can check and wrap it inside NavigationView. Setting …

How to select in List in SwiftUI

Issue #881

Specify optional value for List(selection:).

This keeps selection on macOS, but not on iPad. On iPad each row in the List needs to be NavigationLink, no need for .tag. The selection is not updated, need to manually update with onTapGesture …

How to use native SSL Pinning

Issue #880

From iOS 14, we can do Identity Pinning: How to configure server certificates for your app right from Info.plist

<key>NSAppTransportSecurity</key>
    <dict>
        <key>NSPinnedDomains</key> …

How to use NSPersistentCloudKitContainer

Issue #879

Setting Up Core Data with CloudKit

  • Enable iCloud
  • Enable CloudKit and Push Notifications
  • Enable Remote Notifications in the Background

Creating a Core Data Model for CloudKit

Initialize Your CloudKit Schema During Development

let …

How to allow multiple selection in List in SwiftUI

Issue #878

Note that

  • Explicit id is needed, although Book already conforms to Identifiable
  • selection needs a default value
class BookViewModel: ObservableObject {
    @Published var books: [Book] = []
    @Published var selectedBooks: Set<Book …

How to use ViewBuilder in SwiftUI

Issue #877

Over the course of making several SwiftUI apps, I’ve discovered quite a few hidden magic of SwiftUI that are quite fun.

Here are 6 interesting SwiftUI features in View Builder many don’t know are even possible 🤯

View protocol …

How to debounce TextField search in SwiftUI

Issue #876

Make a dedicate DebounceObject to debounce (or throttle). Then we can even observe with onChange on the debouncedText or just use it directly to sort

import Combine

public final class DebounceObject: ObservableObject {
    @Published var …

How to create document based macOS app

Issue #875

Read newDocument

This method calls openUntitledDocumentAndDisplay(_:).

Read openUntitledDocumentAndDisplay

The default implementation of this method calls defaultType to determine the type of new document to create, calls …

How to add dot indicator to tab bar item in iOS

Issue #874

From iOS 13, use UITabBarAppearance and UITabBarItemAppearance

let appearance = UITabBarAppearance()

let itemAppearance = UITabBarItemAppearance(style: .stacked)
itemAppearance.normal.badgeBackgroundColor = .clear
itemAppearance.normal. …

How to use Multipeer Connectivity

Issue #873

Use assistant

let assistant = MCAdvertiserAssistant(serviceType: "my-service, discoveryInfo: nil, session: mcSession)
assistant.start()

let browser = MCBrowserViewController(serviceType: "my-service", session: mcSession) …

How to generate Polygon wallet account in Swift

Issue #871

Use libraries

import web3
import web3keystore
import KeychainAccess

private final class KeyStorage: EthereumKeyStorageProtocol {
    enum Key: String { …

How to make simple Plist builder with resultBuilder in Swift

Issue #869

We can use PropertyListEncoder from Swift 4

let encoder = PropertyListEncoder()
encoder.outputFormat = .xml
let data = try encoder.encode(model)

Or we can manually do with resultBuilder style

Declare @resultBuilder for PlistBuilder that …

How to generate JWT token for App Store Connect API in Swift

Issue #868

Use JWTKit and code from AppStoreConnect library

import JWTKit

public struct Credential {
    let issuerId: String
    let privateKeyId: String
    let privateKey: String

    public init(
        issuerId: String,
        privateKeyId: …

How to send SPL token in Swift

Issue #867

Using https://github.com/ajamaica/Solana.Swift and the sendSPLToken method. Note that the from is the token account address and amount can have decimals

solana.action.sendSPLTokens(
    mintAddress: MINT_ADDRESS,
    from: …

How to calculate Solana transaction fee

Issue #866

A simple way is to use a fixed fee of 0.000005

For example from https://solscan.io/tx/5DkApvwTYuMqCiA94MhUVKJoLn8MGma9gAWXhreRJKqAs395P5CqEK3R84m3MWjcTKMem53XcLwYErGkaJAbQC2h?cluster=testnet

And call some exchange API, like Coingecko …

How to parse large JSON Dictionary in Swift

Issue #865

We can define some typealias and build extensions on JSONDictionary to easily extract values

typealias JSONDictionary = [String: Any]
typealias JSONArray = [JSONDictionary]

extension JSONDictionary {
    func dict(_ key: String) -> …

How to make simple async URLSession in Swift

Issue #864

Since async URLSession.shared.data is available in iOS 15+, we can build a custom one with withCheckedThrowingContinuation

import UIKit

enum HTTPMethod: String {
    case get = "GET"
    case post = "POST"
}

extension …

How to check SPL token balance on Solana

Issue #863

We will check USDC token balance on Solana testnet.

Firstly, we will use https://usdcfaucet.com/ to airdrop some USDC tokens into our wallet. Secondly, we check USDC token mint address on testnet cluster using Solana Explorer …

How to use subscript in Swift

Issue #861

Make it easy to access common cases, for example UserDefaults

extension UserDefaults {
    enum Key: String {
        case hasBackup
    }

    subscript(key: Key) -> Bool {
        get {
            bool(forKey: key.rawValue)
        } …

How to encode JSON dictionary into JSONEncoder

Issue #860

JSONEncoder deals with type-safe, so we need to declare an enum JSONValue for all possible types. We also need a custom initializer to init JSONValue from a JSON Dictionary

import Foundation

enum JSONValue {
    case string(String) …

How to parse Apple Pay PKPayment in Swift

Issue #859

To parse PKPayment and used with Wyre CreateAppleOrder API, we can declare some Encodable structs

import PassKit
import Foundation

struct PaymentObject: Encodable {
    var billingContact: Contact?
    var shippingContact: Contact? …

How to pop multiple level with NavigationView and NavigationLink in SwiftUI

Issue #858

Use isActive and isDetailLink(false)

Use Introspect

.introspectNavigationController { nav in
    self.nav = nav
}

Read more

How to generate Solana wallet acount in Swift

Issue #857

Use Solana.swift and Mnemonic seed phrase. For production, change endpoint to mainnet

import UIKit
import Solana
import KeychainAccess

enum SolanaError: Swift.Error {
    case accountFailed
    case unauthorized
}

final class …

How to use Apple Pay in iOS

Issue #856

Use PKPaymentRequest and PKPaymentAuthorizationViewController

@MainActor
final class WalletViewModel: NSObject, ObservableObject {
    var canMakePayments: Bool {
        PKPaymentAuthorizationViewController.canMakePayments()
    } …

How to show QR code in SwiftUI

Issue #855

Use CoreImage to generate QR image

import SwiftUI
import CoreImage.CIFilterBuiltins

struct QRView: View {
    let qrCode: String
    @State private var image: UIImage?

    var body: some View {
        ZStack {
            if let image = …

How to not encode with Enum key in Swift

Issue #854

If you use enum case as key in Dictionary, JSONEncoder will encode it as Array. For example

enum Vehicle: String, Codable {
    case car
    case truck
}
struct Container: Codable {
    var map: [Vehicle: String]
}
struct Container2: …

How to disable with ButtonStyle in SwiftUI

Issue #853

With ButtonStyle, the disabled modifier does not seem to work, we need to use allowsHitTesting.

import SwiftUI

struct ActionButtonStyle: ButtonStyle {
    func makeBody(configuration: Configuration) -> some View {
        HStack { …

How to query document id in array in Firestore

Issue #852

Supposed we have Book object

struct Book: Identifiable, Codable, Hashable {
    @DocumentID var id: String?
}

We should use FieldPath instead of id for query

let booksRef: CollectionReference = ...
let ids: [String] = ...

booksRef
    . …

How to provide default Codable in Swift

Issue #851

Use DefaultValue to provide defaultValue in our property wrapper DefaultCodable

public protocol DefaultValue {
    associatedtype Value: Codable
    static var defaultValue: Value { get }
}

public enum DefaultBy {
    public enum True: …

How to use dynamic shape in SwiftUI

Issue #850

Erase with AnyShape

struct AnyShape: Shape {
    init<S: Shape>(_ wrapped: S) {
        innerPath = { rect in
            let path = wrapped.path(in: rect)
            return path
        }
    }

    func path(in rect: CGRect) -> …

How to use Picker with optional selection in SwiftUI

Issue #849

We need to explicitly specify optional in tag

extension AVCaptureDevice: Identifiable {
    public var id: String { uniqueID }
}

@State var device: AVCaptureDevice?

Picker("Camera", selection: $device) {
    ForEach(manager. …

How to deinit NSWindow

Issue #848

Hold a weak reference to NSWindow, and let system window server manages its lifetime

weak var window = NSWindow()
window.isReleasedWhenClosed = true

How to scale system font size to support Dynamic Type

Issue #847

We should use Dynamic Font Type as much as possible, as per Typography guide and https://www.iosfontsizes.com/

But in case we have to use a specific font, we can scale it with UIFontMetrics

import SwiftUI
import UIKit

extension Font { …

How to round specific corner in SwiftUI

Issue #846

We use UIBezierPath with addArc to draw specific corner with different rounding values.

import SwiftUI

extension View {
    func clipCorners(
        topLeft: CGFloat = 0,
        bottomLeft: CGFloat = 0,
        topRight: CGFloat = 0, …

How to show suffix text in TextField in SwiftUI

Issue #845

Suppose we have struct Payment as the state, we declare custom Binding<String> that derives from our state.

struct Payment {
    var amount: Int = 0
}

To show our suffix view, we use .fixedSize(horizontal: true, vertical: false) to …

How to show currency symbol in TextField in SwiftUI

Issue #844

Use custom Binding and validate the text. In case of zero, return empty string to let TextField display placeholder

var textField: some View {
    let text = Binding<String>(
        get: {
            state.amount > 0 ? "$\( …

How to make multiline message text view in SwiftUI

Issue #843

This can be used in message bar input or some popover form. We use sizeThatFits to calculate the height so that it grow under certain height limit

import SwiftUI
import UIKit

struct MessageTextField: View {
    let placeholder: String …

How to read safe area insets in SwiftUI

Issue #842

Declare EnvironmentKey and read safeAreaInsets from key window in connectedScenes

struct SafeAreaInsetsKey: EnvironmentKey {
    static var defaultValue: EdgeInsets {
        UIApplication.shared.keyWindow?.safeAreaInsets.swiftUIInsets ?? …

How to make tab strip with enum cases in SwiftUI

Issue #841

Declare generic on RawRepresentable

import SwiftUI

struct TabStrip<T: RawRepresentable & Hashable>: View where T.RawValue == String {
    let values: [T]
    @Binding var selected: T

    var body: some View {
        ScrollView …

How to replace multiple regex matches in Swift

Issue #840

Used to replace credit card regex 30[0-5]#-######-###L in EasyFake

We can use ? to have non-greedy behavior, or I here use square bracket to fine-tune the expression \{[\d*,]*\d*\} Also, I need to reversed the matches because each …

How to move files with Swift script

Issue #839

Use locales data from faker.js to https://github.com/onmyway133/EasyFake, renaming files since files, regardless of sub directories in Xcode, must have different name.

We use enumerator API on FileManager to traverse all files in all …

How to map Binding with optional value in SwiftUI

Issue #838

We can write our custom Binding

import SwiftUI

extension Binding where Value == Date? {
    func flatten(defaultValue: Date) -> Binding<Date> {
        Binding<Date>(
            get: { wrappedValue ?? defaultValue }, …

How to get view height in SwiftUI

Issue #837

I usually use GeometryReader in background to get size of view, and encapsulate it inside a ViewModifier

struct GetHeightModifier: ViewModifier {
    @Binding var height: CGFloat

    func body(content: Content) -> some View { …

How to prevent wheel Picker conflict with DragGesture in SwiftUI

Issue #836

If we have a Picker inside a View that has DragGesture, chances are when we scroll the wheel, the DragGesture is detected too

To prevent this, we can attach a dummy DragGesture to our Picker

Picker("", selection: $number) { …

How to make equal width buttons in SwiftUI

Issue #835

I usually define ButtonStyle to encapsulate common styles related to buttons. Here we specify .frame(maxWidth: .infinity) to let this button take the whole width as possible

struct MyActionButtonStyle: ButtonStyle {
    func makeBody( …

How to make bottom sheet in SwiftUI

Issue #834

In iOS 15, we can use UISheetPresentationController to show bottom sheet like native Maps app. But before that there’s no such built in bottom sheet in UIKit or SwiftUI.

We can start defining API for it. There are 3 ways to show …

How to show abbreviated ago Date in Swift

Issue #833

Use RelativeDateTimeFormatter. This assumes US locale is used

extension Date {
    private static let relativeFormatter: RelativeDateTimeFormatter = {
        let formatter = RelativeDateTimeFormatter()
        formatter.calendar = …

Icon won't take on final exe

Issue #832

How to bind to nested ObservableObject in SwiftUI

Issue #831

We can normally listen to sub ObservableObject by either listen to its objectWillChange or its Publisher

class SubModel: ObservableObject {
    @Published var count = 0
}

class AppModel: ObservableObject {
    @Published var submodel: …

How to structure navigation in SwiftUI apps

Issue #830

Since iOS 16, it is possible to define programmatic routes with `NavigationStack

I usually start by defining enum Route for all possible routes in the app. Note if your app is complex, you can define multiple Route type, each for different …

How to track contentSize and scroll offset for ScrollView in SwiftUI

Issue #829

Use PreferenceKey with a custom coordinateSpace to make our own TrackableScrollView. Note that the offset here is in reversed to contentOffset in normal UIScrollView

import SwiftUI

struct TrackableScrollView<Content: View>: View { …

How to focus TextField in SwiftUI

Issue #828

From iOS 15, FocusState

Use focused(_:) for single TextField, and focused(_:equals:) for multiple TextField

struct FormView: View {

    @FocusState private var isFocused: Bool
    @State private var name = ""

    var body: some …

How to use debounce in Combine in Swift

Issue #827

I’m trying a simple Future and sink. As long as I have debounce, only receiveCompletion gets called, but not receiveValue

private var bag = Set<AnyCancellable>()

let future = Future<Int, Never> { promise in
    promise(. …

How to use Button inside NavigationLink in SwiftUI

Issue #826

Use isActive binding

@State private var goesToDetail: Bool = false

NavigationLink(
    destination: DetailView(viewModel: viewModel),
    isActive: $goesToDetail) {
    Button(action: { goesToDetail = true }) {
        Text("Next" …

How to structure user state for App in SwiftUI

Issue #825

For many apps that require user authentication, a common practice is to define a shared UserManager with an optional User. This is convenient but it requires us to constantly unwrap and check that user

class UserManager {
    static let …

How to do NavigationLink programatically in SwiftUI

Issue #824

Use a custom NavigationLink with EmptyView as the background, this failable initializer accepts Binding of optional value. This works well as the destination are made lazily.

extension NavigationLink where Label == EmptyView {
    init …

How to make custom navigation bar in SwiftUI

Issue #823

Make atomic components and compose them. Firstly with NavigationBar that has custom leading and trailing content, there we can customize padding.

import SwiftUI

struct NavigationBar<Leading: View, Trailing: View>: View {
    @ …

How to convert NSEvent locationInWindow to window coordinate

Issue #822

Get active screen with mouse

func activeScreen() -> NSScreen? {
    let mouseLocation = NSEvent.mouseLocation
    let screens = NSScreen.screens
    let screenWithMouse = (screens.first { NSMouseInRect(mouseLocation, $0.frame, false) }) …

How to sync width for child views in SwiftUI

Issue #821

Suppose we have a 16:9 preview Image and we want the title Text to be the same width

First we need PreferenceKey to store and sync size

struct SizePreferenceKey: PreferenceKey {
    typealias Value = CGSize
    static var defaultValue: …

How to highlight link in Text in SwiftUI

Issue #820

Use NSDataDetector to detect links, and NSMutableAttributedString to mark link range. Then we enumerateAttributes and build our Text

Screenshot 2021-07-06 at 07 23 56
func attribute(string: String) -> Text {
    guard let detector = try? NSDataDetector(types: …

How to conform UIImage to Codable

Issue #819

Either use EasyStash or make a simple CodableImage

struct CodableImage: Codable {
    let image: UIImage?

    init(image: UIImage) {
        self.image = image
    }

    enum CodingKeys: CodingKey {
        case data
        case scale …

How to show custom context menu in SwiftUI

Issue #818

There’s a lot to do to imitate iOS ContextMenu look and feel

  • vibrancy blur background
  • haptics feedback
  • targeted view position
  • interaction dismiss gesture

For now here’s a rough implementation of a custom context menu where we …

How to declare View with ViewBuilder in SwiftUI

Issue #817

Thanks to resultBuilder and container type in Swift, the following are possible in SwiftUI

Local variable

struct ChartView {
    var body: some View {
        computation
    }

    @ViewBuilder
    var computation: some View {
        let …

How to make custom Menu in SwiftUI

Issue #816

SwiftUI supports Menu but the menu items only appear after the menu button is touched. We can initiate the look and feel of Menu with a custom implementation

Screenshot 2021-07-01 at 21 11 02
import SwiftUI

struct CustomMenu<Content: View>: View {
    @ViewBuilder …

How ObservableObject work in SwiftUI

Issue #815

When ever a property marked with @Published change, ObservableObject will emit objectWillChange.send hence telling the View that observes it to reinvalidate.

In WWDC 2021 session Discover concurrency in SwiftUI they mention how …

How to cancel vertical scrolling on paging TabView in SwiftUI

Issue #814

From iOS 14, TabView has the PageTabViewStyle that turns TabView into the equivalent UIPageViewController.

We can of course implement our own Pager but the simple DragGesture does not bring the true experience of a paging UIScrollView or …

How to make simple swipe vertically to dismiss in SwiftUI

Issue #813

Use simultaneousGesture to not collide with potential horizontal scrolling in carousel view, and check that we’ more accidentally swipe horizontally.

import SwiftUI

struct SwipeToDismissModifier: ViewModifier {
    var onDismiss: () …

How to make carousel pager view in SwiftUI

Issue #812

Use GeometryReader to set width and a DragGesture on LazyVStack

CarouselView(
    pageCount: images.count,
    currentIndex: $selectedImageIndex
) {
    ForEach(0 ..< images) { image in
        // AsyncImage
    }
}
struct CarouselView …

How to use SwiftFormat

Issue #811

Don’t add SwiftFormat as SPM package, instead use command line directly

if which swiftformat >/dev/null; then
  swiftformat .
else
  echo "warning: SwiftFormaat not installed, download from …

How to update Firestore value with KeyPath in Swift

Issue #810

struct User {
    var createdAt: Date
    var name: Sttring
    var locked: Bool
}
extension KeyPath where Root == User {
    var keyPathString: String {
        switch self {
        case \User.createdAt: return "createdAt" …

How to show context menu with custom preview in SwiftUI

Issue #809

Add a hidden overlay UIContextMenuInteraction. Provide preview in previewProvider and actions in actionProvider. Use @ViewBuilder to make declaring preview easy.

extension View {
    func contextMenuWithPreview<Content: View>( …

How to login with Apple in SwiftUI

Issue #808

Make SignInWithAppleButton

Wrap ASAuthorizationAppleIDButton inside UIViewRepresentable

import SwiftUI
import UIKit
import AuthenticationServices

struct SignInWithAppleButton: View {
    @Environment(\.colorScheme)
    private var …

How to use SwiftLint in SPM project

Issue #807

Don’t add SwiftLint via SPM, but just add a Run Script Build phrase

if which swiftlint >/dev/null; then
  swiftlint
else
  echo "warning: SwiftLint not installed, download from https://github.com/realm/SwiftLint"
fi …

How to show modal window in AppKit

Issue #806

Use runModal

https://developer.apple.com/documentation/appkit/nsapplication/1428590-runmodalsession

Blocks main queue

A loop that uses this method is similar in some ways to a modal event loop run with runModal(for:), except with this …

How to perform action during long press in SwiftUI

Issue #805

Use LongPressGesture to detect when long-press gesture has been recognized, and chain with DragGesture to check when pressing still occurs

Image(systemName: SFSymbol.deleteLeft.rawValue)
    .imageScale(.medium)
    .onTapGesture { …

How to fallback img in React

Issue #804

<img 
    src={user.avatarUrl} 
    onError={(e) => {
        e.target.onerror = null; 
        e.target.src = "/images/default.png"
    }}
/>

Extract to React Component

interface Props {
    src: string
    className: …

How to use videojs in React

Issue #803

import React from 'react';
import videojs from 'video.js'
import 'video.js/dist/video-js.css';

export default class VideoPlayer extends React.Component {
    createPlayer() {
        // instantiate Video.js …

How to use FontAwesome 5 in Nextjs

Issue #802

npm i --save @fortawesome/fontawesome-svg-core \
             @fortawesome/free-solid-svg-icons \
             @fortawesome/free-brands-svg-icons \
             @fortawesome/react-fontawesome
import { faApple, faGithub, faTwitter } from …

How to use Bulma in Nextjs

Issue #801

Read

npm install bulma sass

In styles/index.scss

@import '~bulma/bulma';

In _app.tsx

import '../styles/index.scss'

How to make your apps stand out on the AppStore

Issue #799

Besides coming up with a good idea and building the app, there are many other things you can do to boost your apps’ visibility

Twitter thread https://twitter.com/onmyway133/status/1387851727714635776 Thread reader …

How to use SwiftGen and LocalizedStringKey in SwiftUI

Issue #798

swiftgen.yml

strings:
  inputs: PastePal/Resources/Localizable/en.lproj
  outputs:
    - templatePath: swiftgen-swiftui-template.stencil
      output: PastePal/Resources/Strings.swift

Template from …

How to use ForEach with indices in SwiftUI

Issue #796

One seemingly obvious way to use ForEach on array with indices is using enumerated

struct Book: Hashable, Identifiable {
    let id: UUID
    let name: String
}

struct BooksView: View {
    let books: [Book]

    var body: some View { …

How to resize NSImage with padding

Issue #795

extension NSImage {
    func resize(width: CGFloat, height: CGFloat, padding: CGFloat) -> NSImage {
        let img = NSImage(size: CGSize(width: width, height: height))

        img.lockFocus()
        let ctx = NSGraphicsContext. …

How to convert from paid to freemium in SwiftUI with RevenueCat

Issue #794

I’ve been distributing my apps PastePal and Push Hero as a paid upfront apps on Appstore. I’ve quickly come to realize the importance of trials since many have requested a tryout before purchasing.

Manual sending out trials …

How to repeat array of object in Swift

Issue #793

To create array containing number of repeated value in Swift, we can use Array.init(repeating:count:)

let fiveZs = Array(repeating: "Z", count: 5)
print(fiveZs)
// Prints "["Z", "Z", "Z", "Z", …

How to use dynamic color in iOS

Issue #792

iOS 13 introduced Dark Mode with User Interface Style that makes it easy to support dark and light theme in our apps. Before we dive in, here are some official resources

How to use View protocol in SwiftUI

Issue #791

SwiftUI has View protocol which represents part of your app’s user interface and provides modifiers that you use to configure views.

You create custom views by declaring types that conform to the View protocol. Implement the required …

How too save image to Photo library in iOS

Issue #790

Use UIImageWriteToSavedPhotosAlbum

Adds the specified image to the user’s Camera Roll album.

Let’s make an NSObject delegate class so we can perform target action to notify about completion

import UIKit

struct ImageService { …

How to manage WindowGroup in SwiftUI for macOS

Issue #789

Using WindowGroup

New in SwiftUI 2.0 for iOS 14 and macOS 11.0 is WindwGroup which is used in App protocol. WindowGroup is ideal for document based applications where you can open multiple windows for different content or files. For …

How to show img tag from GitHub markdown in Hugo

Issue #788

I just convert my blog https://onmyway133.com/ from Hexo.js back to Hugo again. Hugo now uses goldmark as the default markdown processor instead of blackfriday

All works well, except that I use GitHub markdown to write articles, which use …

How to name Boolean property in Swift

Issue #787

In Swift, property, especially for Boolean flagging, uses the regular verb form for the third person. There are few exceptions, such as enable

NSManagedObjectContext.automaticallyMergesChangesFromParent
UIView.clipsToBounds
UIView. …

How to use with block configure in Swift

Issue #786

Sometimes we need to update some properties between objects, for example

book.name = updatedBook.name
book.page = updatedBook.page
book.publishedAt = updatedBook.publishedAt

Repeating the caller book is tedious and error-prone. In Kotlin, …

How to use Core Data

Issue #785

Core Data

Calling mergeChanges on a managed object context will automatically refresh any managed objects that have changed. This ensures that your context always contains all the latest …

How to force resolve Swift Package in Xcode

Issue #784

Every time I switch git branches, SPM packages seem to be invalidated and Xcode does not fetch again, no matter how many times I reopen. Running xcodebuild -resolvePackageDependencies does fetch but Xcode does not recognize the resolved …

How to listen to remote changes in CloudKit CoreData

Issue #783

Remove chane notification

Read Consuming Relevant Store Changes

If the import happens through a batch operation, the save to the store doesn’t generate an NSManagedObjectContextDidSave notification, and the view misses these relevant …

How to listen to Published outside of SwiftUI view

Issue #782

Use $ to access Publisher

final class Store: ObservableObject {
    @Published var showsSideWindow: Bool = false
}
var anyCancellables = Set<AnyCancellable>()

store.$showsSideWindow
    .removeDuplicates()
    .throttle(for: 0.2, …

How to filter non numeric digit from String in Swift

Issue #781

This sounds like an easy task, but a quick search on Stackoverflow results in this with highest votes https://stackoverflow.com/questions/29971505/filter-non-digits-from-string

CharacterSet.decimalDigits contains more than just digits

This …

How to build container view in SwiftUI

Issue #780

To make a container view that accepts child content, we use ViewBuilder

struct ContainerView<Content: View>: View {
    let content: Content

    init(@ViewBuilder content: () -> Content) {
        self.content = content()
    } …

How to tune performance with ButtonBehavior in SwiftUI

Issue #779

With Xcode 12.4, macOS 11.0 app. Every time we switch the system dark and light mode, the CPU goes up to 100%. Instruments show that there’s an increasing number of ButtonBehavior

Screenshot 2021-02-24 at 10 12 05

Suspect State in a row in LazyVStack

Every cell has …

How to use GroupBox in SwiftUI

Issue #778

For now using GroupBox has these issues in macOS

  • Prevents dragging scroll indicator to scroll
  • Switch from light to dark mode may cause 100% CPU usage

How to suppress selector warning in Swift

Issue #777

Sometimes we need to use dynamic selector and that triggers warning in Swift

Selector("updateWithCount:") // Use '#selector' instead of explicitly constructing a 'Selector'

In ObjC we can use clang macro to …

How to make simple search bar in SwiftUI

Issue #776

We need to use a custom Binding to trigger onChange as onEditingChanged is only called when the user selects the textField, and onCommit is only called when return or done button on keyboard is tapped.

import UIKit
import SwiftUI
import …

How to fix share and action extension not showing up in iOS 14

Issue #775

My share sheet and action extension not showing up in iOS 14, built-in Xcode 12.3. The solution is to restart test device, and it shows up again.

Also make sure your extension targets have the same version and build number, and same …

How to add home screen quick action in SwiftUI

Issue #774

Start by defining your quick actions. You can use UIApplicationShortcutIcon(type:) for predefined icons, or use UIApplicationShortcutIcon(systemImageName:) for SFSymbol

enum QuickAction: String {
    case readPasteboard
    case clear …

How to use EquatableView in SwiftUI

Issue #773

From John Harper ’s tweet

SwiftUI assumes any Equatable.== is a true equality check, so for POD views it compares each field directly instead (via reflection). For non-POD views it prefers the view’s == but falls back to its own …

How to add new property in Codable struct in SwiftUI

Issue #772

I use Codable structs in my apps for preferences, and bind them to SwiftUI views. If we add new properties to existing Codable, it can’t decode with old saved json as we require new properties. We can either do cutom decoding with …

How to show close button in NSTextField in AppKit

Issue #771

Use NSSearchField instead

How to handle escape in NSTextField in SwiftUI

Issue #770

Handle cancelOperation somewhere up in responder chain

class MyWindow: NSWindow {
    let keyHandler = KeyHandler()

    override func cancelOperation(_ sender: Any?) {
        super.cancelOperation(sender)
        keyHandler.onEvent(.esc) …

How to fit ScrollView to content in SwiftUI

Issue #769

If we place ScrollView inside HStack or VStack, it takes all remaining space. To fit ScrollView to its content, we need to get its content size and constrain ScrollView size.

Use a GeometryReader as Scrollview content background, and get …

How to show modal window in SwiftUI for macOS

Issue #768

Use custom NSWindow, set level in becomeKey and call NSApp.runModal to show modal

final class ModalWindow: NSWindow {
    override func becomeKey() {
        super.becomeKey()

        level = .statusBar
    }

    override func close() { …

How to use custom Key for NSCache

Issue #766

Need to use a class, best is to subclass from NSObject

let cache = NSCache<Key, UIImage>()
final class Key: NSObject {
    override func isEqual(_ object: Any?) -> Bool {
        guard let other = object as? Key else { …

How to show multiple popover in SwiftUI

Issue #765

In SwiftUI currently, it’s not possible to attach multiple .popover to the same View. But we can use condition to show different content

struct ClipboardCell: View {
    enum PopoverStyle {
        case raw
        case preview
    } …

How to handle keyDown in SwiftUI for macOS

Issue #764

Use a custom KeyAwareView that uses an NSView that checks for keyDown method. In case we can’t handle certain keys, call super.keyDown(with: event)

import SwiftUI
import KeyboardShortcuts

struct KeyAwareView: NSViewRepresentable { …

How to extend custom View in SwiftUI

Issue #763

I usually break down a big struct into smaller views and extensions. For example I have a ClipboardCell that has a lot of onReceive so I want to move these to another component.

One way to do that is to extend ClipboardCell

struct …

How to use Sparkle for macOS app

Issue #762

Install Sparkle

  • For now, the latest stable version is 1.24.0 which supports CocoaPods OK, but still, have issues with SPM. Support non sandboxed apps
  • Version 2.0.0 is in beta and supports sandboxed apps

To install, use CocoaPods

platform …

How to use ScrollViewReader in SwiftUI

Issue #761

Explicitly specify id

ScrollView {
    ScrollViewReader { proxy in
        LazyVStack(spacing: 10) {
            ForEach(items) { item in
                Cell(item: item)
                    .id(item.id)
            }
        }
        . …

How to handle keyDown in NSResponder

Issue #760

import AppKit
import Omnia

class MyWindow: NSWindow {
    override func keyDown(with event: NSEvent) {
        super.keyDown(with: event)

        if isKey(NSDeleteCharacter, event: event) {
            NotificationCenter.default.post( …

How to handle NSSearchToolbarItem in macOS 11

Issue #758

extension NSToolbarItem.Identifier {
    static let searchItem: NSToolbarItem.Identifier = NSToolbarItem.Identifier("SearchItem")
}

let searchItem = NSSearchToolbarItem(itemIdentifier: .searchItem)

extension AppDelegate: …

How to do launch at login for macOS apps

Issue #757

  • Use SMLoginItemSetEnabled from Service Management framework
  • Use a helper background app that checks and invokes our main application
  • Copy our helper app into Library/LoginItems
helper_dir="$BUILT_PRODUCTS_DIR/$CONTENTS_FOLDER_PATH …

How to fix overlapped navigation titles in SwiftUI

Issue #756

extension NavigationLink {
    func fixOverlap() -> AnyView {
        if UIDevice.current.userInterfaceIdiom == .phone {
            return self.isDetailLink(false).erase()
        } else {
            return self.erase()
        } …

How to create and notarize dmg file

Issue #753

  • Archive and export app from Xcode
  • Create dmg
  • Use create-dmg It is said that we don’t need to notarize the app, we can just notarize the whole dmg
  • Send dmg to notarize

This takes a while

xcrun altool -t osx -f PastePal.dmg --primary …

How to use module import in nodejs

Issue #752

Use esm

npm install esm

In our code, import as normal

const fs = require('fs');
// intended to be run after babel, and in ./dist folder
import factory from 'data'
const shell = require('shelljs')

Then use esm to …

How to use relative file module with Create React app

Issue #751

Declare data/package.json to make it into node module

{
    "name": "data",
    "version": "0.1.0",
    "private": true,
    "homepage": ".",
    "main": "main.js"
} …

How to open downloaded app from browser in Big Sur

Issue #750

Recently when distributing staging releases of my app PastePal via GitHub release or Google Drive, people had hard time opening it

Screenshot 2021-01-15 at 10 26 26

The displayed error is

You do not have permission to open the application

The more verbose error when …

How to add alternative app icons for iOS

Issue #749

Some apps want to support alternative app icons in Settings where user can choose to update app icon. Here’s some must do to make it work, as of Xcode 12.2

  • In Info.plist, must declare CFBundleIcons with both CFBundlePrimaryIcon and …

How to make popup button in SwiftUI for macOS

Issue #748

There is said to be PopUpButtonPickerStyle and MenuPickerStyle but these don’t seem to work.

There’s Menu button it shows a dropdown style. We fake it by fading this and overlay with a button. allowsHitTesting does not work, …

How to use UITextView in SwiftUI

Issue #747

Need to use Coordinator conforming to UITextViewDelegate to apply changes back to Binding

import SwiftUI
import UIKit

struct MyTextView: UIViewRepresentable {
    @Binding
    var text: String

    final class Coordinator: NSObject, …

How to check app going to background in SwiftUI

Issue #746

From iOS 13, the default is to support multiple scene, so the the old UIApplicationDelegate lifecycle does not work. Double check your Info.plist for UIApplicationSceneManifest key

<key>UIApplicationSceneManifest</key> …

How to use selection in List in SwiftUI

Issue #745

I used to use selection with Binding where wrappedValue is optional, together with tag in SwiftUI for macOS, and it shows current selection

@Binding
var selection: Tag? = .all

List(section: $selection) {
    Text("All")
         . …

How to simplify communication patterns with closure in Swift

Issue #744

As someone who builds lots of apps, I try to find quick ways to do things. One of them is to avoid repetitive and cumbersome APIs. That’s why I built Anchors to make Auto Layout more convenient, Omnia to add missing extensions. The next …

How to use Apple keyboard key symbols

Issue #743

•  = Apple logo • ⌘ = Command • ⇧ = Shift • ⌫ = Backspace/Delete • ⇪ = Caps lock • ⌥ = Option/Alt • ⌃ = Control • ⎋ = Escape • ←↑→↓ = Arrow Keys • ↩ = Return

How to make Auto Layout more convenient in iOS

Issue #742

Auto Layout has been around since macOS 10.7 and iOS 6.0 as a nicer way to do layouts over the old resizing masks. Besides some rare cases when we need to manually specify origins and sizes, Auto Layout is the preferred way to do …

How to form product idea

Issue #740

How to gain product ideas?

  1. Scratch your own itch. If you don’t have any itch to scratch, stop here. This is awkward. Go travelling. Go exploring the world. The world always has problems and needs solution. Image

  2. Build any …

How to join AppStore Small Business Program

Issue #739

New program reduces App Store commission to 15 percent for small businesses earning up to $1 million per year

  1. “earning up to $1 million” means proceeds, not sales. This is what we get after Apple’s cut. “up …

How to deep work

Issue #738

Just found out the book Deep Work by Cal Newport and it has some interesting guidelines Here’s a very good summary of the book

https://www.youtube.com/watch?v=gTaJhjQHcf8&ab_channel=ProductivityGame

  1. Put a boundary on …

How to make tiled image in SwiftUI

Issue #737

Use resizingMode of .tile with a tile image from https://www.transparenttextures.com/

Image("transparentTile")
    .resizable(capInsets: .init(), resizingMode: .tile)
    .scaleEffect(2)
    .aspectRatio(contentMode: .fit)
    . …

How to use WebView in SwiftUI

Issue #736

struct MyWebView: NSViewRepresentable {
    let url: URL
    @Binding
    var isLoading: Bool

    func makeCoordinator() -> Coordinator {
        Coordinator(parent: self)
    }

    func makeNSView(context: Context) -> WKWebView { …

How to use GeometryReader in SwiftUI

Issue #735

From my previous post How to use flexible frame in SwiftUI we know that certain views have different frame behaviors. 2 of them are .overlay and GeometryReader that takes up whole size proposed by parent.

By default GeometryReader takes up …

How to use flexible frame in SwiftUI

Issue #734

In SwiftUI there are fixed frame and flexible frame modifiers.

Fixed frame Positions this view within an invisible frame with the specified size.

Use this method to specify a fixed size for a view’s width, height, or both. If you only …

How to disable scrolling in NSTextView for macOS

Issue #733

NSTextView has this handy method to make scrollable NSTextView NSTextView.scrollableTextView(). The solution is to get to the responder outside enclosing NSScrollView, in my case it is the SwiftUI hosting view

class DisabledScrollTextView: …

How to override attribute string in Swift

Issue #732

Use NSMutableAttributedString and add attribute for whole range

let a: NSAttributedString
let m: NSMutableAttributedString = NSMutableAttributedString(attributedString: a)
let range = NSRange(location: 0, length: a.length)
m.addAttribute(. …

How to make view appear with delay in SwiftUI

Issue #731

Sometimes we don’t want to show progress view right away

HUDProgressView()
    .transition(
        AnyTransition.asymmetric(
            insertion: AnyTransition.opacity.animation(Animation.default.delay(1)),
            removal: …

How to make attributed string Text in SwiftUI for macOS

Issue #730

Use NSTextField with maximumNumberOfLines

import AppKit
import SwiftUI

struct AttributedText: NSViewRepresentable {

    let attributedString: NSAttributedString

    init(_ attributedString: NSAttributedString) {
        self. …

How to do copy paste delete in Swift for macOS

Issue #729

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
    @IBAction func copy(_ sender: Any) {
        print("copy", sender)
    }


    @IBAction func paste(_ sender: Any) {
        print("paste", sender) …

How to make simple NSItemProvider in Swift

Issue #728

NSItemProvider(object: StringProvider(string: string))

class StringProvider: NSObject, NSItemProviderWriting {
    let string: String
    init(string: String) {
        self.string = string
        super.init()
    }

    static var …

How to use hashtag raw string in Swift

Issue #727

Use # in Swift 5 to specify raw string, for example regular expression

#"^#?(?:[0-9a-fA-F]{3}){1,2}$"#

Read more

How to make UserDefaults property wrapper

Issue #726

@propertyWrapper
struct UserDefault<Value> {
    let key: String
    let defaultValue: Value
    let container: UserDefaults = .standard

    var wrappedValue: Value {
        get {
            return container.object(forKey: key) as …

How to use Set to check for bool in Swift

Issue #725

When you want to check for existence using Bool, consider using Set over Dictionary with Bool, as Set guarantee uniqueness. If using Dictionary instead, the value for key is Optional<Bool> where we have to check for both optional and …

How to make visual effect blur in SwiftUI for macOS

Issue #724

We can use .blur modifier, but with VisualEffectView gives us more options for material and blending mode.

public struct VisualEffectView: NSViewRepresentable {
    let material: NSVisualEffectView.Material
    let blendingMode: …

How to make simple HUD in SwiftUI

Issue #723

Use @ViewBuilder to build dynamic content for our HUD. For blur effect, here I use NSVisualEffectView, but we can use .blur modifier also

struct HUD<Content>: View where Content: View {
    let content: () -> Content

    init(@ …

How to instrument SwiftUI app

Issue #722

With Xcode 12, we can fire up Instrument to profile our app. Select SwiftUI template

Screenshot 2020-12-26 at 00 02 03

There are many profiles in that template, I find SwiftUI and Time Profile very useful. Here’s the profile I run for my app PastePal

SwiftUI View …

How to force set frame explicitly for NSWindow

Issue #721

For setFrame to take effect

mainWindow.setFrame(rect, display: true)

we can remove auto save frame flag

mainWindow.setFrameAutosaveName("MyApp.MainWindow")

How to rotate NSStatusItem

Issue #720

NSStatusItem is backed by NSButton, we can animate this inner button. We need to specify position and anchorPoint for button’s layer so it rotates around its center point

guard
    let button = statusItem.button
else { return }

let …

How to show image and text in menu item in SwiftUI for macOS

Issue #719

From SwiftUI 2 for macOS 11.0, we have access to Menu for creating menu and submenu. Usually we use Button for interactive menu items and Text for disabled menu items.

The easy way to customize menu with image is to call Menu with content …

How to make sharing menu in SwiftUI for macOS

Issue #718

Use NSSharingService.sharingServices(forItems:) with an array of one empty string gives a list of sharing items. There we show image and title of each menu item.

We should cache sharing items as that can cause performance issue

import …

How to make stepper with plus and minus buttons in SwiftUI for macOS

Issue #717

Try to use predefined system colors in Human Interface Guidelines for macOS

Here we use this color unemphasizedSelectedTextBackgroundColor for button background

Screenshot 2020-12-21 at 06 24 16
HStack(spacing: 1) {
    makeUnderListButton(action: {}, icon: .plus) …

How to fix Picker not showing selection in SwiftUI

Issue #716

I have an enum that conforms to CaseIterable that I want to show in Picker

enum Position: String, Codable, CaseIterable, Identifiable {
    var id: String { rawValue }
    case left
    case right
    case bottom
    case top
}

Picker( …

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 do didSet for State and Binding in SwiftUI

Issue #714

Below is an example of a parent ContentView with State and a child Sidebar with a Binding.

The didSet is only called for the property that is changed.

When we click Button in ContentView, that changes State property, so only the didSet in …

How to add toolbar programatically in macOS

Issue #713

To setup toolbar, we need to implement NSToolbarDelegate that provides toolbar items. This delegate is responsible for many things

  • Set visible and allowed items with toolbarDefaultItemIdentifiers
  • Provide item with itemForItemIdentifier …

How to declare network Error with enum in Swift

Issue #712

Describe all possible paths in your program with enum. This is great to track down bugs and to not miss representing potential cases in UI. Errors can come from app layer, backend layer to network issues.

Enum is handy in both UIKit and …

How to programatically select row in List in SwiftUI

Issue #711

List has a selection parameter where we can pass selection binding. As we can see here selection is of type optional Binding<Set<SelectionValue>>? where SelectionValue is any thing conforming to Hasable

@available(iOS 13.0, …

How to show sidebar in SwiftUI for macOS

Issue #710

Starting from macOS 11, we can use List with SidebarListStyle inside NavigationView to declare master detail view. The SidebarListStyle makes list translucent. It automatically handles selection and marks selected row in list with accent …

How to mock UNNotificationResponse in unit tests

Issue #708

The best way to test is to not have to mock at all. The second best way is to have your own abstraction over the things you would like to test, either it is in form of protocol or some function injection.

But in case you want a quick way …

How to support right click menu to NSStatusItem

Issue #707

The trick is to set the button oinside of statusItem to send actions on both leftMouseUp and rightMouseUp.

Another thing to note is we use popUpMenu on NSStatusItem, although it is marked as deprecated on macOS 10.14. We can set menu but …

How to convert struct to Core Data NSManagedObject

Issue #706

Use Mirror and set key value as NSManagedObject subclasses from NSObject

import CoreData

final class ManagedObjectConverter {
    func convert<M>(m: M, context: NSManagedObjectContext) throws -> NSManagedObject {
        let …

How to format ISO date string in Javascript

Issue #705

Supposed we have date in format ISO8601 and we want to get rid of T and millisecond and timezone Z

const date = new Date()
date.toDateString() // "Sat Dec 05 2020"
date.toString() // "Sat Dec 05 2020 06:58:19 GMT+0100 (Central …

How to declare Error in Swift

Issue #704

We used to declare enum that conforms to Error, but any type like struct or class can conform to Error as well.

enum NetworkError: Error {
    case failToCreateRequest
    case failToParseResponse
    case failToReachServe
}

struct …

How to convert from paid to free with IAP

Issue #703

What is receipt

Read When to refresh a receipt vs restore purchases in iOS?

From iOS 7, every app downloaded from the store has a receipt (for downloading/buying the app) at appStoreReceiptURL. When users purchases something via In App …

How to disable NSTextView in SwiftUI

Issue #702

The trick is to use an overlay

MessageTextView(text: $input.message)
    .overlay(obscure)

var obscure: AnyView {
    if store.pricingPlan.isPro {
        return EmptyView().erase()
    } else {
        return Color.black.opacity(0.01). …

How to add under highlight to text in css

Issue #701

Use mark. This does not work for multiline

<p>
    <mark css={css`
        display: inline-block;
        line-height: 0em;
        padding-bottom: 0.5em;
        `}>{feature.title}
    </mark>
</p>

Another way is …

How to use default system fonts in React apps

Issue #700

In index.css

body {
    margin: 0;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica …

How to make simple overlay container in React

Issue #699

Use term ZStack like in SwiftUI, we declare container as relative position. For now it uses only 2 items from props.children but can be tweaked to support mutiple

class App extends React.Component {
    render() {
        return ( …

How to search using regular expression in Xcode

Issue #698

Xcode has powerful search. We can constrain search to be scoped in workspace, project or some folders. We can also constrain case sensitivity.

Another cool thing that people tend to overlook is, besides searching based on text, we can …

How to write to temporary file in Swift

Issue #697

Use temporaryDirectory from FileManager and String.write

func writeTempFile(books: [Book]) -> URL {
    let url = FileManager.default.temporaryDirectory
        .appendingPathComponent(UUID().uuidString)
        .appendingPathExtension( …

How to use functions with default arguments in Swift

Issue #696

Which methods do you think are used here

import Cocoa

struct Robot {
    let a: Int
    let b: Int
    let c: Int

    init(a: Int = 1, c: Int = 3) {
        self.a = a
        self.b = 0
        self.c = c
        print("Init with a= …

How to check IAP Transaction error

Issue #695

Inspect SKPaymentTransaction for error. In Swift, any Error can be safely bridged into NSError there you can check errorDomain and code

private func handleFailure(_ transaction: SKPaymentTransaction) {
    guard let error = transaction. …

How to use nested ObservableObject in SwiftUI

Issue #694

I usually structure my app to have 1 main ObservableObject called Store with multiple properties in it.

final class Store: ObservableObject {
    @Published var pricingPlan: PricingPlan()
    @Published var preferences: Preferences()
} …

How to check dark mode in AppKit for macOS apps

Issue #693

AppKit app has its theme information stored in UserDefaults key AppleInterfaceStyle, if is dark, it contains String Dark.

Another way is to detect appearance via NSView

struct R {
    static let dark = DarkTheme()
    static let light = …

How to check dark mode with color scheme in SwiftUI

Issue #692

Use colorScheme environment, for now it has 2 cases dark and light

struct MainView: View {
    @Environment(\.colorScheme) var colorScheme

    var body: some View {
        Text(colorScheme == .dark ? "Dark Mode" : "Light …

How to avoid multiple match elements in UITests from iOS 13

Issue #691

Supposed we want to present a ViewController, and there exist both UIToolbar in both the presenting and presented view controllers.

From iOS 13, the model style is not full screen and interactive. From UITests perspective there are 2 …

How to use accessibility container in UITests

Issue #690

Use accessibilityElements to specify containment for contentView and buttons. You can use Accessibility Inspector from Xcode to verify.

class ArticleCell: UICollectionViewCell {
    let authorLabel: UILabel
    let dateLabel: UILabel …

How to make full size content view in SwiftUI for macOS

Issue #689

func applicationDidFinishLaunching(_ aNotification: Notification) {
    // extend to title bar
    let contentView = ContentView()
        // .padding(.top, 24) // can padding to give some space
        .edgesIgnoringSafeArea(.top)

    // …

How to override styles in SwiftUI

Issue #688

In the app I’m working on Elegant Converter, I usually like preset theme with a custom background color and a matching foreground color.

Thanks to SwiftUI style cascading, I can just declare in root MainView and it will be inherited …

When to use function vs property in Swift

Issue #687

Although I do Swift, I often follow Kotlin guideline https://kotlinlang.org/docs/reference/coding-conventions.html#functions-vs-properties

In some cases functions with no arguments might be interchangeable with read-only properties. …

How to use CoreData safely

Issue #686

I now use Core Data more often now. Here is how I usually use it, for example in Push Hero

From iOS 10 and macOS 10.12, NSPersistentContainer that simplifies Core Data setup quite a lot. I usually use 1 NSPersistentContainer and its …

How to pass ObservedObject as parameter in SwiftUI

Issue #685

Since we have custom init in ChildView to manually set a State, we need to pass ObservedObject. In the ParentView, use underscore _ to access property wrapper type.

struct ChildView: View {
    @ObservedObject
    var store: Store

    @ …

How to do equal width in SwiftUI

Issue #684

In SwiftUI, specifying maxWidth as .infinity means taking the whole available width of the container. If many children ask for max width, then they will be divided equally. This is similar to weight in LinearLayout in Android or css …

What define a good developer

Issue #683

I always find myself asking this question “What define a good developer?” I ’ve asked many people and the answers vary, they ’re all correct in certain aspects

Good programmer is someone who has a solid knowledge …

How to test push notifications in simulator and production iOS apps

Issue #682

After has recently reminded about his updating APNs provider API which makes me realised a lot has changed about push notifications, both in terms of client and provider approach.

The HTTP/2-based Apple Push Notification service (APNs) …

How to style multiline Text in SwiftUI for macOS

Issue #681

Only need to specify fixedSize on text to preserve ideal height.

The maximum number of lines is 1 if the value is less than 1. If the value is nil, the text uses as many lines as required. The default is nil.

Text(longText)
    .lineLimit …

How to clear List background color in SwiftUI for macOS

Issue #680

For List in SwiftUI for macOS, it has default background color because of the enclosing NSScrollView via NSTableView that List uses under the hood. Using listRowBackground also gives no effect

The solution is to use a library like …

How to avoid reduced opacity when hiding view with animation in SwiftUI

Issue #679

While redesigning UI for my app Push Hero, I ended up with an accordion style to toggle section.

Screenshot 2020-10-01 at 06 58 33

It worked great so far, but after 1 collapsing, all image and text views have reduced opacity. This does not happen for other elements like …

How to dynamically add items to VStack from list in SwiftUI

Issue #678

Use enumerated to get index so we can assign to item in list. Here is how I show list of device tokens in my app Push Hero

private var textViews: some View {
    let withIndex = input.deviceTokens.enumerated().map({ $0 })
    let binding: …

How to unwrap Binding with Optional in SwiftUI

Issue #677

The quick way to add new properties without breaking current saved Codable is to declare them as optional. For example if you use EasyStash library to save and load Codable models.

import SwiftUI

struct Input: Codable {
    var bundleId: …

How to make custom toggle in SwiftUI

Issue #676

I’ve used the default SwiftUI to achieve the 2 tab views in SwiftUI. It adds a default box around the content and also opinionated paddings. For now on light mode on macOS, the unselected tab has wrong colors.

The way to solve this …

How to use Binding in function in Swift

Issue #675

Use wrappedValue to get the underlying value that Binding contains

extension View {
    func addOverlay(shows: Binding<Bool>) -> some View {
        HStack {
            self
            Spacer()
        }
        .overlay( …

How to use HSplitView to define 3 panes view in SwiftUI for macOS

Issue #674

Specify minWidth to ensure miminum width, and use .layoutPriority(1) for the most important pane.

import SwiftUI

struct MainView: View {
    @EnvironmentObject var store: Store

    var body: some View {
        HSplitView { …

How to draw arc corner using Bezier Path

Issue #673

To draw rounded 2 corners at top left and top right, let’s start from bottom left

let path = UIBezierPath()
// bottom left
path.move(to: CGPoint(x: 0, y: bounds.height))
// top left corner
path.addArc(withCenter: CGPoint(x: radius, y …

How to stitch and sort array in Swift

Issue #672

Supposed we want to stitch magazines array into books array. The requirement is to sort them by publishedDate, but must keep preferredOrder of books. One way to solve this is to declare an enum to hold all possible cases, and then do a …

How to make dynamic font size for UIButton

Issue #671

Use adjustsFontForContentSizeCategory

A Boolean that indicates whether the object automatically updates its font when the device’s content size category changes.

If you set this property to YES, the element adjusts for a new …

How to test for view disappear in navigation controller

Issue #670

To test for viewWillDisappear during UINavigationController popViewController in unit test, we need to simulate UIWindow so view appearance works.

final class PopTests: XCTestCase {
    func testPop() {
        let window = UIWindow(frame: …

How to use useEffect in React hook

Issue #669

Specify params as array [year, id], not object {year, id}

const { year, id } = props
useEffect(() => {
        const loadData = async () => {
            try {
                
            } catch (error) {
                console. …

How to sign in with apple for web with firebase

Issue #668

Authenticate Using Apple with JavaScript

Use Firebase JS SDK

Configure Sign in with Apple for the web

Go to Certificates, …

How to use firebase cloud functions

Issue #667

Use node 10

Edit package.json

"engines": {
    "node": "10"
},

Secret key

Go to settings/serviceaccounts/adminsdk, download secret key in form of json and place it in lib/config.json

const serviceAccount = require( …

How to go back to home in React

Issue #665

Usually in header we have logo that takes user back to the home page

// index.js
import {
    BrowserRouter as Router,
    Switch,
    Route,
    Link
} from 'react-router-dom'

<Router>
    <Switch>
        <Route …

How to format date in certain timezone with momentjs

Issue #664

Use moment-timezone https://momentjs.com/timezone/docs/

npm install moment-timezone

// public/index.html
<script src="moment.js"></script>
<script src="moment-timezone-with-data.js"></script>

Need …

How to style video js

Issue #663

/** @jsx jsx */
import { css, jsx } from '@emotion/core'
import React from 'react';
import videojs from 'video.js'

export default class VideoPlayer extends React.Component {
    componentDidMount() {
        // …

How to use setState in React

Issue #662

Use spread operator

import React, { Component, useState, useEffect } from 'react';

const [state, setState] = useState({
        message: '',
        chats: [],
        sending: false
})

setState(prevState => ({ …

How to handle evens in put with React

Issue #661

Use Bulma css

<input
    class="input is-rounded"
    type="text"
    placeholder="Say something"
    value={value}
    onChange={(e) => { onValueChange(e.target.value) }}
    onKeyDown={(e) => {
        if …

How to handle events for input with React

Issue #661

Use Bulma css

<input
    class="input is-rounded"
    type="text"
    placeholder="Say something"
    value={value}
    onChange={(e) => { onValueChange(e.target.value) }}
    onKeyDown={(e) => {
        if …

How to set text color for UIDatePicker

Issue #660

Apply tintColor does not seem to have effect.

datePicker.setValue(UIColor.label, forKeyPath: "textColor")
datePicker.setValue(false, forKey: "highlightsToday")

How to auto scroll to top in react router 5

Issue #659

Use useLocation https://reacttraining.com/react-router/web/guides/scroll-restoration

import React, { useEffect } from 'react';
import {
    BrowserRouter as Router,
    Switch,
    Route,
    Link,
    useLocation,
    withRouter
} …

How to use dynamic route in react router 5

Issue #658

Declare routes, use exact to match exact path as finding route is from top to bottom. For dynamic route, I find that we need to use render and pass the props manually.

Declare Router as the rooter with Header, Content and Footer. Inside …

How to use folder as local npm package

Issue #657

Add library folder src/library

src
    library
        res.js
    screens
        Home
            index.js
package.json

Declare package.json in library folder

{
    "name": "library",
    "version": "0.0.1" …

How to make switch statement in SwiftUI

Issue #656

Lately I’ve been challenging myself to declare switch statement in SwiftUI, or in a more generalized way, execute any anonymous function that can return a View

Use Switch and Case views

Note that this approach does not work yet, as …

How to update multiple state properties with React hooks

Issue #655

Declare state and setState

export default function Showcase(props) {
    const factory = props.factory
    const [state, setState] = useState(
        {
            factory,
            selectedFilter: { name: '' }
        }
    ) …

How to make white label React app

Issue #654

Use shelljs to execute shell commands, and fs to read and write. In public/index.html specify some placeholder and we will replace those in our script

const fs = require('fs');
const shell = require('shelljs')

let …

How to make white label React app for landing pages

Issue #654

A good landing page is one of the most crucial part of a successful launch. Recently I started creating landing pages for my apps, I was lazy that I ended creating a white label React app as a landing template and then write a script to …

How to show lightbox in React

Issue #653

Use https://github.com/biati-digital/glightbox

Configure css. Specify class='glightbox for html elements

<link rel="stylesheet" href="css/blueimp-gallery.min.css" />

Install package

npm install glightbox
import …

How to animate on scroll in React

Issue #652

Use https://github.com/michalsnik/aos

Add link to head

<head>
    <link rel="stylesheet" href="https://unpkg.com/aos@next/dist/aos.css" />
</head>

Jus before closing body tag

<script src= …

How to copy folder in nodej

Issue #651

Use shelljs

npm install shelljs
const shell = require('shelljs')
shell.exec(`cp -a source_path/. destination_path`)

The -a option is an improved recursive option, that preserve all file attributes, and also preserve symlinks.

The …

How to use babel 7 in node project

Issue #650

Install

npm install @babel/core
npm install @babel/cli
npm install @babel/preset-env

Configure .babelrc

{
  "presets": ["@babel/preset-env"]
}

In package.json, transpile using npx babel then node dist/index.js …

How to conditionally apply css in emotion js

Issue #649

Check flag then define css

const Picture = (feature) => {
    const shadowCss = feature.shadow ? css`
        border-radius: 5px;
        box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
    ` : css`` …

How to scroll to element in React

Issue #648

In a similar fashion to plain old javascript, note that href needs to have valid hash tag, like #features

<a
    href='#features'
    onClick={() => {
        const options = {
            behavior: 'smooth'
        } …

How to use emotion for inline css in React

Issue #647

emotion can be used in framework agnostic or with React. To use with React, follow https://emotion.sh/docs/introduction#react

npm i @emotion/core

Note that you must have /** @jsx jsx */ at the top of the file, and the unused jsx in import …

How to test DispatchQueue in Swift

Issue #646

Sync the DispatchQueue

Pass DispatchQueue and call queue.sync to sync all async works before asserting

Use mock

Use DispatchQueueType and in mock, call the work immediately

import Foundation

public protocol DispatchQueueType {
    func …

How to use webpack to bundle html css js

Issue #645

Install webpack

npm init
npm install webpack webpack-cli --save-dev
vim webpack.config.js
module.exports = {
    entry: "./index.js",
    mode: 'production',
    output: {
        filename: "./index.js"
    }
}

To …

How to assert asynchronously in XCTest

Issue #644

import XCTest

extension XCTestCase {
    /// Asynchronously assertion
    func XCTAssertWait(
        timeout: TimeInterval = 1,
        _ expression: @escaping () -> Void,
        _: String = "",
        file _: StaticString = …

How to make simple filter menu in css

Issue #643

Use material icons

<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
div#filter-container {
    display: flex;
    align-items: center;
    justify-content: center;
    margin-top: …

How to make simple grid gallery in css

Issue #642

Specify container with flex-wrap and justify-content, and item with float: left

div.cards {
  display: flex;
  justify-content: center;
  flex-direction: row;
  flex-wrap: wrap;
  margin-top: 10%;
}

div.card {
  overflow: hidden;
  float: …

How to add independent page in hexo

Issue #641

Create a new page

hexo new page mydemo

Remove index.md and create index.html, you can reference external css and js in this index.html. Hexo has hexo new page mydemo --slug but it does not support page hierarchy

Specify no layout so it is …

How to use async function as parameter in TypeScript

Issue #640

async function useCache(
    collectionName: string, 
    key: string, 
    response: functions.Response<any>, 
    fetch: () => Promise<any>
): Promise<any> {
    const existing = await db.collection(collectionName). …

How to format percent in Swift

Issue #639

Never use String(format: "%.2f %%", 1.2 because each region can have different separator and placement of percent sign.

Use NumberFormatter instead

let formatter = NumberFormatter()
formatter.numberStyle = .percent
formatter. …

How to declare commands in Xcode extensions

Issue #638

Use commandDefinitions in XCSourceEditorExtension.

import Foundation
import XcodeKit

class SourceEditorExtension: NSObject, XCSourceEditorExtension {
    func extensionDidFinishLaunching() {

    }
    
    var commandDefinitions: [[ …

How to declare commands in Xcode extenstions

Issue #638

Use commandDefinitions in XCSourceEditorExtension.

import Foundation
import XcodeKit

class SourceEditorExtension: NSObject, XCSourceEditorExtension {
    func extensionDidFinishLaunching() {

    }
    
    var commandDefinitions: [[ …

How to disable ring type in TextField in SwiftUI

Issue #636

Normally we can just wrap NSTextField

struct SearchTextField: NSViewRepresentable {
    @Binding var text: String
    var hint: String
    var onCommit: (String) -> Void

    func makeNSView(context: NSViewRepresentableContext< …

How to handle enter key in NSTextField

Issue #635

textField.delegate = self
NSTextFieldDelegate

func control(_ control: NSControl, textView: NSTextView, doCommandBy commandSelector: Selector) -> Bool {
    if (commandSelector == #selector(NSResponder.insertNewline(_:))) {
        // …

How to decode with default case for enum in Swift

Issue #634

public enum Weapon: String, Decodable {
    case sword = "SWORD"
    case gun = "GUN"
    case unknown = "UNKNOWN"

    public init(from decoder: Decoder) throws {
        let rawValue = try decoder. …

How to conditionally apply modifier in SwiftUI

Issue #633

Use autoclosure and AnyView

@available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
public extension View {
    func applyIf<T: View>(_ condition: @autoclosure () -> Bool, apply: (Self) -> T) -> AnyView {
        if …

How to toggle with animation in SwiftUI

Issue #632

Use Group

private func makeHeader() -> some View {
    Group {
        if showsSearch {
            SearchView(
                onSearch: onSearch
            )
            .transition(.move(edge: .leading))
        } else { …

How to show context popover from SwiftUI for macOS

Issue #630

For SwiftUI app using NSPopover, to show context popover menu, we can ask for windows array, get the _NSPopoverWindow and calculate the position. Note that origin of macOS screen is bottom left

(lldb) po NSApp.windows
▿ 2 elements
  - 0 : …

How to make segmented control in SwiftUI for macOS

Issue #629

Use Picker with SegmentedPickerStyle.

Picker(selection: $preferenceManager.preference.display, label: EmptyView()) {
    Image("grid")
        .resizable()
    .padding()
        .tag(0)
    Image("list")
        .resizable …

How to iterate over XCUIElementQuery in UITests

Issue #628

extension XCUIElementQuery: Sequence {
    public typealias Iterator = AnyIterator<XCUIElement>
    public func makeIterator() -> Iterator {
        var index = UInt(0)
        return AnyIterator {
            guard index < …

How to check if NSColor is light

Issue #627

Algorithm from https://www.w3.org/WAI/ER/WD-AERT/#color-contrast

extension NSColor {
    var isLight: Bool {
        guard
            let components = cgColor.components,
            components.count >= 3
        else { return false } …

How to trigger onAppear in SwiftUI for macOS

Issue #626

SwiftUI does not trigger onAppear and onDisappear like we expect. We can use NSView to trigger

import SwiftUI

struct AppearAware: NSViewRepresentable {
    var onAppear: () -> Void

    func makeNSView(context: …

How to force refresh in ForEach in SwiftUI for macOS

Issue #625

For some strange reasons, content inside ForEach does not update with changes in Core Data NSManagedObject. The workaround is to introduce salt, like UUID just to make state change

struct NoteRow: View {
    let note: Note
    let id: UUID …

How to access bookmark url in macOS

Issue #624

By default the approaches above grant you access while the app remains open. When you quit the app, any folder access you had is lost.

To gain persistent access to a folder even on subsequent launches, we’ll have to take advantage of a …

How to force FetchRequest update in SwiftUI

Issue #623

Listen to context changes notification and change SwiftUI View state

let changes = [NSDeletedObjectsKey: ids]
NSManagedObjectContext.mergeChanges(
    fromRemoteContextSave: changes,
    into: [context]
)
try context.save()

struct …

How to batch delete in Core Data

Issue #622

Read Implementing Batch Deletes

If the entities that are being deleted are not loaded into memory, there is no need to update your application after the NSBatchDeleteRequest has been executed. However, if you are deleting objects in the …

How to update FetchRequest with predicate in SwiftUI

Issue #621

Make subview that accepts FetchRequest. Trigger search by setting property

struct SideView: View {
    @Environment(\.managedObjectContext)
    var context

    @State var search: Search?

    var body: some View {
        VStack(alignment …

How to make TextField focus in SwiftUI for macOS

Issue #620

For NSWindow having levelother than .normal, need to override key and main property to allow TextField to be focusable

class FocusWindow: NSWindow {
    override var canBecomeKey: Bool  { true }
    override var canBecomeMain: Bool { …

How to show popover for item in ForEach in SwiftUI

Issue #618

Create custom Binding

List {
    ForEach(self.items) { (item: item) in
        ItemRowView(item: item)
            .popover(isPresented: self.makeIsPresented(item: item)) {
                ItemDetailView(item: item)
            }
    }
} …

How to make tooltip in SwiftUI for macOS

Issue #617

On macOS 11, we can use .help modifier to add tooltip

Button()
    .help("Click here to open settings")

If you support macOS 10.15, then create empty NSView and use as overlay. Need to updateNSView in case we toggle the state of …

How to make translucent SwiftUI List in macOS

Issue #615

List {
    ForEach(books) { (book: Book) in
        BookRow(book: book)
    }
}
.listStyle(SidebarListStyle())  

How to make tab view in SwiftUI

Issue #614

struct MyTabView: View {
    @EnvironmentObject
    var preferenceManager: PreferenceManager

    var body: some View {
        VOrH(isVertical: preferenceManager.preference.position.isVertical) {
            OneTabView(image: …

How to return VStack or HStack in SwiftUI

Issue #613

struct VOrH<Content>: View where Content: View {
    let isVertical: Bool
    let content: () -> Content

    init(isVertical: Bool, @ViewBuilder content: @escaping () -> Content) {
        self.isVertical = isVertical …

How to present NSWindow modally

Issue #612

Use runModal

This method runs a modal event loop for the specified window synchronously. It displays the specified window, makes it key, starts the run loop, and processes events for that window. (You do not need to show the window …

How to use Picker with enum in SwiftUI

Issue #611

enum WindowPosition: String, CaseIterable {
    case left
    case top
    case bottom
    case right
}
Picker(selection: $preference.position, label: Text("Position")) {
    ForEach(WindowPosition.allCases, id: \.self) { …

How to use visual effect view in NSWindow

Issue #610

Set NSVisualEffectView as contentView of NSWindow, and our main view as subview of it. Remember to set frame or autoresizing mask as non-direct content view does not get full size as the window

let mainView = MainView()
    .environment(\. …

How to animate NSWindow

Issue #609

Use animator proxy and animate parameter

var rect = window.frame
rect.frame.origin.x = 1000
NSAnimationContext.runAnimationGroup({ context in
    context.timingFunction = CAMediaTimingFunction(name: .easeIn)
    window.animator().setFrame( …

How to find active application in macOS

Issue #608

An NSRunningApplication instance for the current application.

NSRunningApplication.current

The running app instance for the app that receives key events.

NSWorkspace.shared.frontmostApplication

 

How to compare for nearly equal in Swift

Issue #607

Implement Equatable and Comparable and use round

struct RGBA: Equatable, Comparable {
    let red: CGFloat
    let green: CGFloat
    let blue: CGFloat
    let alpha: CGFloat

    init(_ red: CGFloat, _ green: CGFloat, _ blue: CGFloat, _ …

How to conform to Hashable for class in Swift

Issue #606

Use ObjectIdentifier

A unique identifier for a class instance or metatype.

final class Worker: Hashable {
    static func == (lhs: Worker, rhs: Worker) -> Bool {
        return ObjectIdentifier(lhs) == ObjectIdentifier(rhs)
    } …

How to edit selected item in list in SwiftUI

Issue #605

I use custom TextView in a master detail application.

import SwiftUI

struct TextView: NSViewRepresentable {
    @Binding var text: String

    func makeCoordinator() -> Coordinator {
        Coordinator(self)
    }

    func makeNSView …

How to log in SwiftUI

Issue #604

I see that the modifier needs to do something on the content, otherwise it is not getting called! This logs on the modifier, when the View is created. A View won’t be recreated unless necessary

struct LogModifier: ViewModifier { …

How to mask with UILabel

Issue #603

Need to set correct frame for mask layer or UILabel, as it is relative to the coordinate of the view to be masked

let aView = UIView(frame: .init(x: 100, y: 110, width: 200, height: 100))

let textLayer = CATextLayer()
textLayer. …

How to avoid pitfalls in SwiftUI

Issue #602

Identify by unique id

ForEach(store.blogs.enumerated().map({ $0 }), id: \.element.id) { index, blog in

}
```

## 

How to use application will terminate in macOS

Issue #601

On Xcode 11, applicationWillTerminate is not called because of default automatic termination on in Info.plist. Removing NSSupportsSuddenTermination to trigger will terminate notification

func applicationWillTerminate(_ notification: …

How to sync multiple CAAnimation

Issue #600

Use same CACurrentMediaTime

final class AnimationSyncer {
    static let now = CACurrentMediaTime()

    func makeAnimation() -> CABasicAnimation {
        let animation = CABasicAnimation(keyPath: "opacity")
        animation. …

How to use TabView with enum in SwiftUI

Issue #599

Specify tag

enum Authentication: Int, Codable {
    case key
    case certificate
}


TabView(selection: $authentication) {
    KeyAuthenticationView()
        .tabItem {
            Text("🔑 Key")
        }
        .tag( …

How to build SwiftUI style UICollectionView data source in Swift

Issue #598

It’s hard to see any iOS app which don’t use UITableView or UICollectionView, as they are the basic and important foundation to represent data. UICollectionView is very basic to use, yet a bit tedious for common use cases, but …

How to make round border in SwiftUI

Issue #597

TextView(font: R.font.text!, lineCount: nil, text: $text, isFocus: $isFocus)
.padding(8)
.background(R.color.inputBackground)
.cornerRadius(10)
.overlay(
    RoundedRectangle(cornerRadius: 10)
        .stroke(isFocus ? R.color. …

How to mock in Swift

Issue #596

Unavailable init

UNUserNotificationCenter.current().getNotificationSettings(completionHandler: { (settings: UNNotificationSettings) in
    let status: UNAuthorizationStatus = .authorized
    settings.setValue(status.rawValue, forKey: …

How to change background color in List in SwiftUI for macOS

Issue #595

SwiftUI uses ListCoreScrollView and ListCoreClipView under the hood. For now the workaround, is to avoid using List

List {
    ForEach
}

use

VStack {
    ForEach
}

How to add drag and drop in SwiftUI

Issue #594

In some case, we should not use base type like UTType.text but to be more specific like UTType.utf8PlainText

import UniformTypeIdentifiers

var dropTypes: [UTType] {
    [
        .fileURL,
        .url,
        .utf8PlainText,
        . …

How to weak link Combine in macOS 10.14 and iOS 12

Issue #593

#if canImport(Combine) is not enough, need to specify in Other Linker Flags

OTHER_LDFLAGS = -weak_framework Combine

Read more

How to make radio button group in SwiftUI

Issue #592

Use picker with Radio style

Hard to customize

Picker(selection: Binding<Bool>.constant(true), label: EmptyView()) {
    Text("Production").tag(0)
    Text("Sandbox").tag(1)
}.pickerStyle(RadioGroupPickerStyle())

Use …

How to set font to NSTextField in macOS

Issue #591

Use NSTextView instead

How to make borderless material NSTextField in SwiftUI for macOS

Issue #590

Use custom NSTextField as it is hard to customize TextFieldStyle

import SwiftUI

struct MaterialTextField: View {
    let placeholder: String
    @Binding var text: String
    @State var isFocus: Bool = false

    var body: some View { …

How to make focusable NSTextField in macOS

Issue #589

Use onTapGesture

import SwiftUI

struct MyTextField: View {
    @Binding
    var text: String
    let placeholder: String
    @State
    private var isFocus: Bool = false

    var body: some View {
        FocusTextField(text: $text, …

How to observe focus event of NSTextField in macOS

Issue #589

becomeFirstResponder

class FocusAwareTextField: NSTextField {
    var onFocusChange: (Bool) -> Void = { _ in }

    override func becomeFirstResponder() -> Bool {
        let textView = window?.fieldEditor(true, for: nil) as? …

How to change caret color of NSTextField in macOS

Issue #588

class FocusAwareTextField: NSTextField {
    var onFocus: () -> Void = {}
    var onUnfocus: () -> Void = {}

    override func becomeFirstResponder() -> Bool {
        onFocus()
        let textView = window?.fieldEditor(true, …

How to make TextView in SwiftUI for macOS

Issue #587

Use NSTextVIew

From https://github.com/twostraws/ControlRoom/blob/main/ControlRoom/NSViewWrappers/TextView.swift

import SwiftUI

/// A wrapper around NSTextView so we can get multiline text editing in SwiftUI.
struct TextView: …

How to use custom font in SwiftUI

Issue #586

In macOS

Add fonts to target. In Info.plist, just need to specify font locations, most of the time they are at Resources folder

ATSApplicationFontsPath (String - macOS) identifies the location of a font file or directory of fonts in the …

How to use Firebase Crashlytics in macOS app

Issue #585

New Firebase Crashlytics

Follow the new Firebase Crashlytics guide Get started with Firebase Crashlytics using the Firebase Crashlytics SDK

CocoaPods

Specify FirebaseCore for community managed macOS version of Firebase

platform :osx, …

How to test drag and drop in UITests

Issue #583

In UITests, we can use press from XCUIElement to test drag and drop

let fromCat = app.buttons["cat1"].firstMatch
let toCat = app.buttons["cat2"]
let fromCoordinate = fromCat.coordinate(withNormalizedOffset: CGVector(dx: 0, …

How to set corner radius in iOS

Issue #582

Use View Debugging

Run on device, Xcode -> Debug -> View debugging -> Rendering -> Color blended layer On Simulator -> Debug -> Color Blended Layer

Corner radius

How to work with SceneDelegate in iOS 12

Issue #580

Events

open url

Implement scene(_:openURLContexts:) in your scene delegate.

If the URL launches your app, you will get …

How to handle radio group for NSButton

Issue #579

Use same action, or we can roll our own implementation

An NSButton configured as a radio button (with the -buttonType set to NSRadioButton), will now operate in a radio button group for applications linked on 10.8 and later. To have the …

How to specify locale in Swift

Issue #578

Locale

Read Language and Locale IDs

zh-Hans_HK
[language designator]-[script designator]_[region designator]

Language IDs

A language ID identifies a language used in many regions, a dialect used in a specific region, or a script used in …

How to workaround URLSession issue in watchOS 6.1.1

Issue #577

https://stackoverflow.com/questions/59724731/class-avassetdownloadtask-is-implemented-in-both-cfnetwork-and-avfoundation

objc[45250]: Class AVAssetDownloadTask is implemented in both /Applications/Xcode.app/Contents/Developer/Platforms/ …

How to generate XCTest test methods

Issue #576

Code

See Spek

Override testInvocations to specify test methods

https://developer.apple.com/documentation/xctest/xctestcase/1496271-testinvocations

Returns an array of invocations representing each test method in the test case.

Because …

How to use ObjC in Swift Package Manager

Issue #575

Create Objc target

Check runtime

Check for example _runtime(_ObjC) or os(macOS if you plan to use platform specific feature …

How to expression cast type in lldb in Swit

Issue #574

expr -l Swift -- import UIKit
expr -l Swift -- let $collectionView = unsafeBitCast(0x7fddd8180000, to: UICollectionView.self)
expr -l Swift -- $collectionView.safeAreaInsets

How to use Applications folder in macOS

Issue #573

There are 2 Applications folder

  • /System/Applications: contains Notes, Books, Calculator, …
  • /Applications: contains Safari, Xcode, Keynote, …

How to fix library not found with SPM and CocoaPods in Xcode

Issue #572

After migrating a few pods to use SPM, some libraries fail to load. This is because the workspace now uses both SPM and cocoapods

code signature in … not valid for use in process using Library Validation: mapped file has no Team ID …

How to make rotation in same direction in iOS

Issue #571

From CGFloat.pi / 2 to -CGFloat.pi / 2 + epsilon

How to get updated safeAreaInsets in iOS

Issue #570

Use viewSafeArea

@available(iOS 11.0, *)
override func viewSafeAreaInsetsDidChange() {
    super.viewSafeAreaInsetsDidChange()

    self.collectionView.reloadData()
}

Use …

How to disable implicit decoration view animation in UICollectionView

Issue #569

From documentation https://developer.apple.com/documentation/uikit/uicollectionviewlayout/1617726-initiallayoutattributesforappear

This method is called after the prepare(forCollectionViewUpdates:) method and before the …

How to make simple tracker via swizzling in Swift

Issue #568

Code

Swizzle viewDidAppear

import UIKit

var mapping: [String: (UIViewController) -> Void] = [:]
var hasSwizzled = false

public func track< …

How to make simple adapter for delegate and datasource for UICollectionView and UITableView

Issue #567

Code

Make open Adapter

import UIKit

public protocol AdapterDelegate: class {

  /// Apply model to view
  func configure(model: Any, view: UIView, indexPath: IndexPath)

  /// Handle …

How to make progress HUD in Swift

Issue #566

Code

Create a container that has blur effect

public class HUDContainer: UIVisualEffectView, AnimationAware {
    private let innerContentView: UIView & AnimationAware
    public let label = UILabel()
    public var …

How to build static site using Publish

Issue #564

Code

Steps

Step 1: Create executable

swift package init --type executable

Step 2: Edit package

// swift-tools-version:5.1
// The …

How to use iTMSTransporter

Issue #563

Transporter app

The new Transporter app for macOS makes it easy to upload your binary to App Store Connect. To get started, download Transporter from the Mac App Store, and simply drag and …

How to use Timer and RunLoop in Swift

Issue #562

Run Timer in command line application

Timer.scheduledTimer(withTimeInterval: seconds, repeats: false, block: { _ in
    completion(.success(()))
})

RunLoop.current.run()

Read more …

How to parse xcrun simctl devices

Issue #559

public class GetDestinations {
    public init() {}

    public func getAvailable(workflow: Workflow) throws -> [Destination] {
        let processHandler = DefaultProcessHandler(filter: { $0.starts(with: "name=") })
        let …

How to parse xcrun instruments devices

Issue #558

public class GetDestinations {
    public init() {}

    public func getAvailable(workflow: Workflow) throws -> [Destination] {
        let processHandler = DefaultProcessHandler(filter: { $0.starts(with: "name=") })
        let …

How to use test environment variables with shared scheme in Xcode

Issue #557

Use ProcessInfo to access environment variables.

ProcessInfo().environment["username"]

Duplicate main shared scheme TestApp to TestAppWithCredentials, but don’t share this TestAppWithCredentials scheme

How to generate xml in Swift

Issue #556

Instead of learning XMLParser, we can make a lightweight version

import Foundation

public protocol XmlItem {
    func toLines() -> [String]
}

public struct XmlString: XmlItem {
    public let key: String
    public let value: String …

How to use synthetic property in Kotlin Android Extension

Issue #555

Synthetic properties generated by Kotlin Android Extensions plugin needs a view for Fragment/Activity to be set before hand.

In your case, for Fragment, you need to use view.btn_K in onViewCreated

override fun onCreateView(inflater: …

How to use KVO in Swift

Issue #554

A class must inherit from NSObject, and we have 3 ways to trigger property change

Use setValue(value: AnyObject?, forKey key: String) from NSKeyValueCoding

class MyObjectToObserve: NSObject {
    var myDate = NSDate()
    func …

How to use precondition and assert in Swift

Issue #553

Read Swift asserts - the missing manual

                            debug	release	  release
    function	            -Onone	-O	     -Ounchecked
    assert()	            YES	    NO	      NO
    assertionFailure()	    YES	    NO	      NO** …

How to get ISO string from date in Javascript

Issue #552

type Components = {
  day: number,
  month: number,
  year: number
}

export default class DateFormatter {
  // 2018-11-11T00:00:00
  static ISOStringWithoutTimeZone = (date: Date): string => {
    const components = DateFormatter. …

How to use regular expression in Swift

Issue #551

Find matches

import Foundation

public extension String {
    func matches(pattern: String) throws -> [String] {
        let regex = try NSRegularExpression(pattern: pattern)
        let results = regex.matches(in: self, options: [], …

Why is didSelectItem not called in UICollectionView

Issue #550

Check

  • shouldHighlightItem -> shouldSelectItem -> didSelectItem
  • Gesture recognizer
  • isUserInteractionEnabled

How to keep command line tool running with async in Swift

Issue #549

Use Semaphore

public class Sequence: Task {
    public func run(workflow: Workflow, completion: @escaping TaskCompletion) {
        let semaphore = DispatchSemaphore(value: 0)
        runFirst(tasks: tasks, workflow: workflow, completion: …

How to sync an async function in Swift

Issue #547

func sync<T>(_ work: (@escaping ([T]) -> Void) -> Void) -> [T] {
    let semaphore = DispatchSemaphore(value: 1)
    var results = [T]()
    work { values in
        results = values
        semaphore.signal()
    } …

How to do localization in Xcode

Issue #545

Xcode 10 Localization catalog and XclocReader

Xcode 11 localized screenshots

How to use xcodebuild

Issue #544

man xcodebuild

man xcodebuild

XCODEBUILD(1)             BSD General Commands Manual            XCODEBUILD(1)

NAME
     xcodebuild -- build Xcode projects and workspaces

SYNOPSIS
     xcodebuild [-project name.xcodeproj] [[-target …

How to use Derived data in Xcode

Issue #543

Workspace

Workspace has its own DerivedData folder

DerivedData
  ModuleCache.noindex
  workspace_name
    Build
      Products
        Debug-iphonesimulator
          Cat
          Dog
          Dog2
    Index
    Info.plist
    Logs …

How to not use protocol extension in Swift

Issue #542

With protocol extension

See code Puma

Build is UsesXcodeBuild is UsesCommandLine
/// Any task that uses command line
public protocol UsesCommandLine: AnyObject {}

public extension UsesCommandLine {
    func runBash(
        workflow: …

How to use test scheme in Xcode

Issue #541

Scheme action

A scheme, either for app or test, consists of actions

Run

Used when Cmd+R. The executable specifies which app target to run

Screenshot 2019-12-14 at 23 39 48

Test

Used when Cmd+U. The tests specifies which test target to run

Screenshot 2019-12-14 at 23 40 09

Test target recognises app …

How to set language and locale with xcodebuild

Issue #540

testLanguage and testRegion

 -testLanguage language
       Specifies ISO 639-1 language during testing. This overrides the setting for the test action of a
       scheme in a workspace.

 -testRegion region
       Specifies ISO 3166-1 …

How to take screenshots for UITest in Xcodee

Issue #539

XCUIScreenshot

extension XCTestCase {
    func takeScreenshot(name: String) {
        let screenshot = XCUIScreen.main.screenshot()
        let attach = XCTAttachment(screenshot: screenshot)
        attach.lifetime = .keepAlways …

How to fix UIToolbar Auto Layout issues

Issue #538

Hierarchy

UIToolbar -> _UIToolbarContentView -> _UIButtonBarStackVie

UIToolbarContentView

_UIToolbarContentView's width should equal 0
_UIToolbarContentView's height should equal 0

Workaround that fixes 1 warning

toolbar. …

How to use passed launch arguments in UITests

Issue #537

Specify launch arguments

In xcodebuild, specify launch arguments.

You can specify this under Launch Arguments in Run action of the app scheme or UITest scheme

Screenshot 2019-12-10 at 23 27 02
-AppleLanguages (jp) -AppleLocale (jp_JP)
(lldb) po ProcessInfo().arguments
▿ …

How to add padding to left right view in UITextField

Issue #536

extension UITextField {
    func setLeftView(_ view: UIView, padding: CGFloat) {
        view.translatesAutoresizingMaskIntoConstraints = true

        let outerView = UIView()
        outerView.translatesAutoresizingMaskIntoConstraints = …

How to set date color in UIDatePicker in iOS 13

Issue #535

datePicker.setValue(UIColor.green, forKey: "textColor")
datePicker.setValue(false, forKey: "highlightsToday")

In iOS 14

if #available(iOS 14.0, *) {
    datePicker.preferredDatePickerStyle = .wheels
    datePicker.tintColor …

How to use bundle with rbenv

Issue #534

Workaround

/Users/khoa/.rbenv/shims/bundler install

How to show localized text in SwiftUI

Issue #533

struct ContentView: View {
    @Environment(\.locale) var locale: Locale

    var body: some View {
        VStack {
            Text(LocalizedStringKey("hello"))
                .font(.largeTitle)
            Text(flag(from: …

How to show flag emoji from country code in Swift

Issue #532

func flag(from country: String) -> String {
    let base : UInt32 = 127397
    var s = ""
    for v in country.uppercased().unicodeScalars {
        s.unicodeScalars.append(UnicodeScalar(base + v.value)!)
    }
    return s
} …

How to work with git

Issue #531

Expand commits in Sublime Merge

{
	"expand_merge_commits_by_default": true,
	"translate_tabs_to_spaces": true
}

local hooks .git/hooks vs hooksPath

git config core.hooksPath ~/.git-templates/hooks

Only hooksPath gets run. …

How to fix cropped image in UIImageView

Issue #530

Although UIImageView frame is correct, image is still cropped. Watch out for any layer.mask within view

How to use decoration view in UICollectionView

Issue #529

indexPath

https://developer.apple.com/documentation/uikit/uicollectionviewlayoutattributes/1617786-layoutattributesfordecorationvie

It is up to you to decide how to use the indexPath parameter to identify a given decoration view. …

How to do lense in Swift

Issue #528

What is lense

https://www.schoolofhaskell.com/school/to-infinity-and-beyond/pick-of-the-week/a-little-lens-starter-tutorial

A lens is a first-class reference to a subpart of some data type. For instance, we have _1 which is the lens that …

How to convert from callback to Future Publisher in Combine

Issue #527

import Foundation
import Combine

public typealias TaskCompletion = (Result<(), Error>) -> Void

public protocol Task: AnyObject {
    var name: String { get }
    func run(workflow: Workflow, completion: TaskCompletion)
}

public …

How to make init with closure in Swift

Issue #526

public class Build: UsesXcodeBuild {
    public var arguments = [String]()
    
    public init(_ closure: (Build) -> Void = { _ in }) {
        closure(self)
    }
}

Use function builder

public class Workflow {
    public var …

How to test a developing package with Swift Package Manager

Issue #525

Use macOS Command Line project

Example Puma

  • Create a new macOS project, select Command Line Tool Screenshot 2019-11-30 at 22 40 35
  • Drag Puma.xcodeproj as a sub project of our test project
  • Go to our TestPuma target, under Link Binary with Libraries, select Puma framework …

How to use method from protocol extension in Swift

Issue #524

/// Any task that uses command line
public protocol UsesCommandLine: AnyObject {
    var program: String { get }
    var arguments: Set<String> { get set }
}

public extension UsesCommandLine {
    func run() throws {
        let …

How to organize dependencies in Swift Package Manager

Issue #523

In Puma I want to make build tools for iOS and Android, which should share some common infrastructure. So we can organize dependencies like.

Puma -> PumaAndroid, PumaiOS -> PumaCore -> xcbeautify, Files, Colorizer

// …

How to provide configurations in Swift

Issue #522

Sometimes ago I created Puma, which is a thin wrapper around Xcode commandline tools, for example xcodebuild

There’s lots of arguments to pass in xcodebuild, and there are many tasks like build, test and archive that all uses this …

How to use SurveyMonkey in React Native

Issue #521

#import <React/RCTBridgeModule.h>

@interface RNSurveyManager : NSObject <RCTBridgeModule>

@end
#import "RNSurveyManager.h"
#import <React/RCTLog.h>
#import <SurveyMonkeyiOSSDK/SurveyMonkeyiOSSDK.h> …

How to allow unnotarized app to run on macOS Catalina

Issue #520

Remove quarantine

xattr -d com.apple.quarantine /Applications/Flipper.app

How to use flipper

Issue #519

Run the app

brew install watchman


git clone https://github.com/facebook/flipper.git
cd flipper
yarn
yarn start

How to test UserDefaults in iOS

Issue #518

let userDefaults = UserDefaults(suiteName: suiteName)
userDefaults.removePersistentDomain(forName: suiteName)

https://developer.apple.com/documentation/foundation/userdefaults/1417339-removepersistentdomain

Calling this method is …

How to use ForEach with ScrollView in SwiftUI

Issue #517

Use ScrollView -> VStack -> ForEach -> Content

struct SearchScreen: View {
    @State var searchObjects: [SearchObject] = [
        SearchObject(name: "By name", search: { CountryManager.shared.search(byName: $0) }), …

How to modify data inside array in SwiftUI

Issue #516

Suppose we have an array of SearchObject, and user can enter search query into text property.

class SearchObject: ObservableObject {
    let name: String
    let search: (String) -> [Country]
    var text: String = ""

    init( …

How to use index in SwiftUI list

Issue #515

Use enumerated and id: \.element.name

struct CountriesView: View {
    let countries: [Country]

    var body: some View {
        let withIndex = countries.enumerated().map({ $0 })

        return List(withIndex, id: \.element.name) { …

How to setup multiple git accounts for GitHub and Bitbucket

Issue #514

Generate SSH keys

ssh-keygen -t rsa -C "onmyway133@gmail.com" -f "id_rsa_github"
ssh-keygen -t rsa -C "onmyway133bitbucket@gmail.com" -f "id_rsa_bitbucket"

pbcopy < ~/.ssh/id_rsa_github.pub
pbcopy < …

How to use objectWillChange in Combine

Issue #513

A publisher that emits before the object has changed

Use workaround DispatchQueue to wait another run loop to access newValue

.onReceive(store.objectWillChange, perform: {
    DispatchQueue.main.async {
        self.reload()
    }
}) …

How to show list with section in SwiftUI

Issue #511

struct CountriesView: View {
    let groups: [Group]

    init(countries: [Country]) {
        self.groups = CountryManager.shared.groups(countries: countries)
    }

    var body: some View {
        List {
            ForEach(groups) { …

How to group array by property in Swift

Issue #510

Use Dictionary(grouping:by:)

func groups(countries: [Country]) -> [Group] {
    let dictionary = Dictionary(grouping: countries, by: { String($0.name.prefix(1)) })
    let groups = dictionary
        .map({ (key: String, value: [Country …

How to make full width list row in SwiftUI

Issue #508

We need to use frame(minWidth: 0, maxWidth: .infinity, alignment: .leading). Note that order is important, and padding should be first, and background after frame to apply color to the entire frame

struct BooksScreen: View {
    @ …

How to make full screen TabView in SwiftUI

Issue #507

View extends to the bottom, but not to the notch. We need to add .edgesIgnoringSafeArea(.top) to our TabView to tell TabView to extend all the way to the top.

Note that if we use edgesIgnoringSafeArea(.all) then TabView ’s bar will …

How to map error in Combine

Issue #506

When a function expects AnyPublisher<[Book], Error> but in mock, we have Just

func getBooks() -> AnyPublisher<[Book], Error> {
    return Just([
        Book(id: "1", name: "Book 1"),
        Book(id: …

How to fix unable to infer complex closure return type in SwiftUI

Issue #505

Make sure all String are passed into Text, not Optional<String>

VStack {
    Text(data.title)
    Text(data.description!)
    Text(data.text!)
}

How to make Swift Package Manager package for multiple platforms

Issue #504

https://twitter.com/NeoNacho/status/1181245484867801088?s=20

There’s no way to have platform specific sources or targets today, so you’ll have to take a different approach. I would recommend wrapping all OS specific files in #if os and …

How to make simple Redux for SwiftUI

Issue #502

Mutation is used to mutate state synchronously. Action is like intent, either from app or from user action. Action maps to Mutation in form of Publisher to work with async action, similar to redux-observable

AnyReducer is a type erasure …

How to use Firebase in macOS

Issue #501

  • Use Catalyst
  • Add to CocoaPods
platform :ios, '13.0'

target 'MyApp' do
  use_frameworks!

  pod 'FirebaseCore'
  pod 'Firebase/Firestore'

end

Troubleshooting

Select a team for …

How to use Xcode

Issue #499

Build setting

Build Library For Distribution

It turns on all the features that are necessary to build your library in such a way that it can be distributed

What does this error actually mean? Well, when the …

Links for Xcode

Issue #499

Build setting

Build Library For Distribution

It turns on all the features that are necessary to build your library in such a way that it can be distributed

What does this error actually mean? Well, when the …

How to access view in fragment in Kotlin

Issue #497

Synthetic properties generated by Kotlin Android Extensions plugin needs a view for Fragment/Activity to be set before hand.

In your case, for Fragment, you need to use view.btn_K in onViewCreated

override fun onCreateView(inflater: …

How to refresh receipt and restore in app purchase in iOS

Issue #496

Read this Restoring Purchased Products to understand the purposes between the 2.

From iOS 7, every app downloaded from the store has a receipt (for downloading/buying the app) at appStoreReceiptURL. When users purchases something via In …

How to edit hexo theme hiero

Issue #494

Code

Remove max-width from source/css/style.styl

.outer
  clearfix()
  // max-width: (column-width + gutter-width) * columns + gutter-width
  margin: 40px auto
  padding: 0 gutter-width …

How to use Firebase RemoteConfig

Issue #493

Declare in Podfile

pod 'Firebase/Core'
pod 'Firebase/RemoteConfig'

Use RemoteConfigHandler to encapsulate logic. We introduce Key with CaseIterable and defaultValue of type NSNumber to manage default values.

import Firebase …

How to apply translations to Localizable.strings

Issue #492

Suppose we have a base Localizable.strings

"open" = "Open";
"closed" = "Closed";

After sending that file for translations, we get translated versions.

"open" = "Åpen";
"closed" = …

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 …

How to add trailing image to UILabel in iOS

Issue #470

Use NSTextAttachment inside NSAttributedString

extension UILabel {
    func addTrailing(image: UIImage) {
        let attachment = NSTextAttachment()
        attachment.image = image

        let attachmentString = NSAttributedString( …

How to handle different states in a screen in iOS

Issue #469

If there are lots of logics and states inside a screen, it is best to introduce parent and child container, and switch child depends on state. Each child acts as a State handler.

In less logic case, we can introduce a Scenario class that …

How to reload data without using onAppear in SwiftUI in watchOS

Issue #468

From onAppeear

Adds an action to perform when the view appears.

In theory, this should be triggered every time this view appears. But in practice, it is only called when it is pushed on navigation stack, not when we return to it.

So if …

How to use EnvironmentObject in SwiftUI for watchOS

Issue #467

Declare top dependencies in ExtensionDelegate

class ExtensionDelegate: NSObject, WKExtensionDelegate {
    let storeContainer = StoreContainer()

    func applicationDidEnterBackground() {
        storeContainer.save()
    }
}

Reference …

How to structure classes

Issue #466

iOS

View Controller -> View Model | Logic Handler -> Data Handler -> Repo

Android

Activity -> Fragment -> View Model | Logic Handler -> Data Handler -> Repo

How to make generic store for Codable in Swift

Issue #465

Use EasyStash

import EasyStash

final class Store<T: Codable & ItemProtocol>: Codable {
    var items = [T]()

    func bookmark(item: T) {
        items.append(item)
    }

    func unbookmark(item: T) {
        guard let index …

How to declare escaping in completion in callback in Swift

Issue #464

typealias Completion = (Result<User, Error>) -> Void

func validate(completion: @escaping Completion, then: (String, String, @escaping Completion) -> Void) {}

How to zoom to fit many coordinates in Google Maps in iOS

Issue #463

func zoom(location1: CLLocation, location2: CLLocation) {
    let bounds = GMSCoordinateBounds(coordinate: location1.coordinate, coordinate: location2.coordinate)
    let update = GMSCameraUpdate.fit(bounds, withPadding: 16)
    mapView. …

How to use Codable to store preferences in Swift

Issue #462

Using object, we don’t need to care about nil vs false like in UserDefaults, our object is the source of truth

class StoringHandler<T: Codable> {
    private let key: Storage.Keys
    private let storage = Deps.storage …

How to custom UIAlertController in iOS

Issue #461

With UIAlertController we can add buttons and textfields, and just that

func addAction(UIAlertAction)
func addTextField(configurationHandler: ((UITextField) -> Void)?)

To add custom content to UIAlertController, there are some …

How to find subview recursively in Swift

Issue #460

extension UIView {
    func findRecursively<T: UIView>(type: T.Type, match: (T) -> Bool) -> T? {
        for view in subviews {
            if let subview = view as? T, match(subview) {
                return subview …

How to deal with multiple scenarios with Push Notification in iOS

Issue #459

Here are my notes when working with Push Notification

How to register

Register to receive push notification

registerForRemoteNotificationTypes is deprecated in iOS 8+

UIApplication.sharedApplication().registerForRemoteNotifications() …

How to use Apple certificate in Xcode 11

Issue #458

For push notification, we can now use just Production Certificate for 2 environments (production and sandbox) instead of Development and Production certificates.

Now for code signing, with Xcode 11 …

How to create watch only watchOS app

Issue #457

From Creating Independent watchOS Apps

The root target is a stub, and acts as a wrapper for your project, so that you can submit it to the App Store. The other two are identical to the targets found in a traditional watchOS project. They …

How to use provisional notification in iOS 12

Issue #456

From WWDC 2018 What’s New in User Notifications

Instead, the notifications from your app will automatically start getting delivered.

Notifications that are delivered with provisional authorization will have a prompt like this on …

How to check if push notification is actually enabled in iOS

Issue #455

There are times we want to log if user can receive push notification. We may be tempted to merely use isRegisteredForRemoteNotifications but that is not enough. From a user ’s point of view, they can either receive push notification …

How to use UserNotificationsUI in iOS

Issue #454

From documentation https://developer.apple.com/documentation/usernotificationsui

Customize how local and remote notifications appear on the user’s device by adding a notification content app extension to the bundle of your iOS app. Your …

How to use AnyHashable in Swift

Issue #453

From documentation

A type-erased hashable value.

DiscussionThe AnyHashable type forwards equality comparisons and hashing operations to an underlying hashable value, hiding its specific underlying type.You can store mixed-type keys in …

How to register for alert push notification in iOS

Issue #452

Use UserNotifications framework

import FirebaseMessaging
import UserNotifications

final class PushHandler: NSObject {
    private let center = UNUserNotificationCenter.current()
    private let options: UNAuthorizationOptions = [.alert] …

How to flat map publisher of publishers in Combine

Issue #451

For some services, we need to deal with separated APIs for getting ids and getting detail based on id.

To chain requests, we can use flatMap and Sequence, then collect to wait and get all elements in a single publish

FlatMap

Transforms …

How to make container view in SwiftUI

Issue #450

Following the signatures of ScrollView and Group, we can create our own container

public struct ScrollView<Content> : View where Content : View {

    /// The content of the scroll view.
    public var content: Content

}
extension …

How to show web content as QR code in SwiftUI in watchOS

Issue #449

WatchKit does not have Web component, despite the fact that we can view web content

A workaround is to show url as QR code

import SwiftUI

struct QRCodeView: View {
    let …

How to load remote image in SwiftUI

Issue #448

Use ObservableObject and onReceive to receive event. URLSession.dataTask reports in background queue, so need to .receive(on: RunLoop.main) to receive events on main queue.

For better dependency injection, need to use ImageLoader from …

How to do navigation in SwiftUI in watchOS

Issue #447

NavigationView is not available on WatchKit, but we can just use NavigationLink

List(services.map({ AnyService($0) })) { anyService in
    NavigationLink(destination:
        ItemsView(service: anyService.service)
            . …

How to use protocol in List in SwiftUI

Issue #446

Suppose we have Service protocol, and want to use in List

protocol Service {
    var name: String { get }
}
struct MainView: View {
    let services = [
        Car()
        Plane()
    ]

    var body: some View {
        List(services) …

How to support Swift Package Manager for existing projects

Issue #445

How to add SPM

How to fix unreadable ASCII characters in Swift

Issue #444

To avoid compiler error Unprintable ASCII character found in source file, from Swift 5, we can use isASCII.

Run this from the generator app that generates Swift code.

let normalized = weirdString.filter({ $0.isASCII })

For more check, see …

Support

Thanks for downloading my app. For any inquiries, please contact me at onmyway133@gmail.com

How to style NSTextView and NSTextField in macOS

Issue #443

let textField = NSTextField()
textField.focusRingType = .none
let textView = NSTextView()
textView.insertionPointColor = R.color.caret
textView.isRichText = false
textView.importsGraphics = false
textView.isEditable = true
textView. …

How to center NSWindow in screen

Issue #442

On macOS, coordinate origin is bottom left

let window = NSWindow(contentRect: rect, styleMask: .borderless, backing: .buffered, defer: false)

window.center()
let frame = window.frame
window.setFrameOrigin(CGPoint(x: frame.origin.x, y: 100 …

How to remove duplicates based on property in array in Swift

Issue #441

Make object conform to Equatable and Hashable and use Set to eliminate duplications. Set loses order so we need to sort after uniquing

struct App: Equatable, Hashable {
    static func == (lhs: App, rhs: App) -> Bool {
        return …

How to get url of app on the iOS AppStore

Issue #440

http://itunes.apple.com/[country]/app/[App–Name]/id[App-ID]?mt=8

For example https://apps.apple.com/nl/app/cutters/id1466739130

How to log Error in Swift

Issue #439

Use localizedDescription

We need to provide NSLocalizedDescriptionKey inside user info dictionary, otherwise the outputt string may not be what we want.

NSError …

How to handle NSTextField change in macOS

Issue #438

Storyboard

In Storyboard, NSTextField has an Action option that specify whether Send on Send on Enter only` should be the default behaviour.

textfield

Code

In code, NSTextFieldDelegate notifies whenever text field value changes, and target action …

How to add section header to NSCollectionView in macOS

Issue #437

Normal

Use Omnia for itemId extension

HeaderCell.swift

final class HeaderCell: NSView, NSCollectionViewSectionHeaderView {
    let label: NSTextField = withObject(NSTextField(labelWithString: "")) {
        $0.textColor = R.color. …

How to show log in Apple Script

Issue #436

Open Script Editor, use log command and look for 4 tabs in bottom panel Result, Messages, Events and Replies

log "hello world"

How to show context menu from NSButton in macOS

Issue #435

Use NSMenu and popUp

func showQuitMenu() {
    let menu = NSMenu()
    let aboutItem = NSMenuItem(
        title: "About",
        action: #selector(onAboutTouched(_:)),
        keyEquivalent: ""
    )

    let quitItem = …

How to handle UICollectionView reloadData with selected index path

Issue #434

When calling collectionView.reloadData(), selected indexpath stays the same, but be aware that order of data may have changed

let selectedData = ...
let indexPathForSelectedData = ...

collectionView.scrollToItem(
    at: …

How to make checked NSButton in AppKit

Issue #433

  • Use Omnia for convenient style and isOn property
let checkButton = NSButton(checkboxWithTitle: "", target: nil, action: nil)
checkButton.stylePlain(title: "Autosave", color: R.color.text, font: R.font.text)
checkButton. …

How to save files in sandboxed macOS app

Issue #432

Read Container Directories and File System Access

When you adopt App Sandbox, your application has access to the following locations:

The app container directory. Upon first launch, the operating system creates a special directory for …

How to add AdMob to Android app

Issue #431

Use AdMob with Firebase

build.gradle

buildscript {
    repositories {
        google()
        jcenter()
        
    }
    dependencies {
        classpath …

How to notarize electron app

Issue #430

Use electron builder

npm install electron-builder@latest --save-dev

package.json

{
  "name": …

How to use marked in WKWebView in AppKit

Issue #429

Use https://github.com/markedjs/marked

<!doctype html>
<html>
<head>
  <meta charset="utf-8"/>
  <title>Marked in the browser</title>
</head>
<body>
  <div id="content" …

How to enable NSMenuItem in AppKit

Issue #428

Need to set target

let item = NSMenuItem(
    title: title,
    action: #selector(onMenuItemClicked(_:)),
    keyEquivalent: ""
)

item.target = self

Sometimes, need to check autoenablesItems

Indicates whether the menu …

How to use generic NSCollectionView in macOS

Issue #427

See CollectionViewHandler

Use ClickedCollectionView to detect clicked index for context menu. Embed NSCollectionView inside NSScrollView to enable scrolling

import AppKit

public class CollectionViewHandler<Item: Equatable, Cell: …

How to use throttle and debounce in RxSwift

Issue #426

throttle

https://rxmarbles.com/#throttle

throttle

Returns an Observable that emits the first and the latest item emitted by the source Observable during sequential time windows of a specified duration. This operator makes sure that no two …

How to use flatMap and compactMap in Swift

Issue #425

flatMap: map and flatten array of arrays compactMap: map and flatten array of optionals

How to use Firebase ID token

Issue #424

One confusing point here that people often do not realize is that even though the custom token itself expires after one hour, a modern client SDK authenticated …

How to use custom domain for GitHub pages

Issue #423

In DNS settings

Add 4 A records

A @ 185.199.110.153
A @	185.199.111.153
A @	185.199.108.153
A @	185.199.109.153

and 1 CNAME record

CNAME www learntalks.github.io

In GitHub

  • Select custom domain and type learntalks.com

In source …

How to constrain to views inside UICollectionViewCell in iOS

Issue #422

To constrain views outside to elements inside UICollectionViewCell, we can use UILayoutGuide.

Need to make layout guide the same constraints as the real elements

let imageViewGuide = UILayoutGuide()
collectionView.addLayoutGuide( …

How to secure CVC in STPPaymentCardTextField in Stripe for iOS

Issue #421

private func maskCvcIfAny() {
    guard
        let view = paymentTextField.subviews.first(where: { !($0 is UIImageView) }),
        let cvcField = view.subviews
			.compactMap({ $0 as? UITextField })
			.first(where: { $0.tag == 2 …

How to replace all occurrences of a string in Javascript

Issue #420

const normalized = string
   .replace(/\//g, '')
   .replace(/\"/g, '')
   .replace(/\(/g, '')
   .replace(/\)/g, '')

How to read and write file using fs in node

Issue #419

function write(json) {
    const data = JSON.stringify(json)
    const year = json.date.getFullYear()
    const directory = `collected/${slugify(className)}/${year}`

    fs.mkdirSync(directory, { recursive: true })
    fs.writeFileSync( …

How to get videos from vimeo in node

Issue #418

Code

Path for user users/nsspain/videos Path for showcase https://developer.vimeo.com/api/reference/albums#get_album Path for Channels, Groups and Portfolios

const Vimeo = require('vimeo').Vimeo
const vimeoClient = new Vimeo( …

How to get videos from youtube in node

Issue #417

class Youtube {
    async getVideos(playlistId, pageToken) {
        const options = {
            key: clientKey,
            part: 'id,contentDetails,snippet',
            playlistId: playlistId,
            maxResult: 100, …

How to convert callback to Promise in Javascript

Issue #416

// @flow

const toPromise = (f: (any) => void) => {
  return new Promise<any>((resolve, reject) => {
    try {
      f((result) => {
        resolve(result)
      })
    } catch (e) {
      reject(e)
    }
  })
}
const …

How to fix electron issues

Issue #415

Electron require() is not defined

https://stackoverflow.com/questions/44391448/electron-require-is-not-defined

function createWindow () {
  win = new BrowserWindow({
    title: 'MyApp',
    width: 600, 
    height: 500, …

How to easily parse deep json in Swift

Issue #414

Codable is awesome, but sometimes we just need to quickly get value in a deepy nested JSON. In the same way I did for Dart How to resolve deep json object in Dart, let’s make that in Swift.

See …

How to speed up GMSMarker in Google Maps for iOS

Issue #412

  • Google Maps with a lot of pin, and no clustering can have bad performance if there are complex view in the marker.
  • The workaround is to use manual layout and rasterization

shouldRasterize

When the value of this property is true, the …

How to support drag and drop in UICollectionView iOS

Issue #411

See DragAndDrop example

class ViewController: UIViewController, UICollectionViewDropDelegate, UICollectionViewDragDelegate {

    // MARK: - UICollectionViewDragDelegate

    func collectionView(_ collectionView: UICollectionView, …

How to support drag and drop in NSView

Issue #410

import AppKit
import Anchors

class DraggingView: NSView {
    var didDrag: ((FileInfo) -> Void)?
    let highlightView = NSView()

    override init(frame frameRect: NSRect) {
        super.init(frame: frameRect) …

How to use NSStepper in Appkit

Issue #409

let stepper = NSStepper()
let textField = NSTextField(wrappingLabelWithString: "\(myLocalCount)")

stepper.integerValue = myLocalCount
stepper.minValue = 5
stepper.maxValue = 24
stepper.valueWraps = false

stepper.target = self …

How to handle shortcut in AppKit

Issue #408

Podfile

pod 'MASShortcut'
let shortcut = MASShortcut(keyCode: kVK_ANSI_K, modifierFlags: [.command, .shift])

MASShortcutMonitor.shared()?.register(shortcut, withAction: {
    self.showPopover(sender: self.statusItem.button)
})

How to select file in its directory in AppKit

Issue #407

https://developer.apple.com/documentation/appkit/nsworkspace/1524399-selectfile

In macOS 10.5 and later, this method does not follow symlinks when selecting the file. If the fullPath parameter contains any symlinks, this method selects …

How to use NSProgressIndicator in AppKit

Issue #406

let progressIndicator = NSProgressIndicator()
progressIndicator.isIndeterminate = true
progressIndicator.style = .spinning
progressIndicator.startAnimation(nil)

How to show save panel in AppKit

Issue #405

Enable Read/Write for User Selected File under Sandbox to avoid bridge absent error

func showSave(
    name: String,
    window: NSWindow
) async -> URL? {
    let panel = NSSavePanel()
    panel.directoryURL = FileManager.default. …

How to animate NSView using keyframe

Issue #404

let animation = CAKeyframeAnimation(keyPath: "position.y")
animation.values = [50, 20, 50]
animation.keyTimes = [0.0, 0.5, 1.0]
animation.duration = 2
animation.repeatCount = Float.greatestFiniteMagnitude
animation.autoreverses = …

How to quit macOS on last window closed

Issue #403

https://developer.apple.com/documentation/appkit/nsapplicationdelegate/1428381-applicationshouldterminateafterl?language=objc

The application sends this message to your delegate when the application’s last window is closed. It sends this …

How to test Date with timezone aware in Swift

Issue #402

I want to test if a date has passed another date

let base =  Date(timeIntervalSince1970: 1567756697)
XCTAssertEqual(validator.hasPassed(event: event, date: base), true)

My hasPassed is using Calendar.current

func minuteSinceMidnight(date: …

How to sign executable for sandbox

Issue #401

Find identity

security find-identity 

Sign with entitlements and identity. For macOS, use 3rd Party Mac Developer Application

codesign -f -s "3rd Party Mac Developer Application: Khoa Pham (123DK123F2)" --entitlements …

How to make simple traffic simulation in Javascript

Issue #400

How to make collaborative drawing canvas with socketio and node

Issue #399

Client

App.js

import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';
import Main from './Main'

class App extends …

How to generate changelog for GitHub releases with rxjs and node

Issue #398

How to

Technical

Dependencies

const Rx = require('rxjs/Rx')
const Fetch = require('node-fetch') …

How to do launch screen in Android

Issue #397

We recommend that, rather than disabling the preview window, you follow the common Material Design patterns. You can use the activity’s windowBackground theme attribute to provide a simple custom drawable for the starting activity. …

How to add header to NavigationView in Android

Issue #396

Use app:headerLayout

<com.google.android.material.navigation.NavigationView
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:id="@+id/navigationView" …

How to do simple analytics in iOS

Issue #395

Prefer static enum to avoid repetition and error. The Log should have methods with all required fields so the call site is as simple as possible. How to format and assign parameters is encapsulated in this Analytics.

import Foundation …

Back to static site

Issue #394

It’s been a while since I wrote Hello world, again, the ease of GitHub issue indeed motivates me to write more.

In the mean time I also wrote on https://medium.com/@onmyway133 and https://dev.to/onmyway133 and got some traction.

Then …

How to get all GitHub issues using GraphQL

Issue #393

https://developer.github.com/v4/explorer/

query { 
  repository(owner: "onmyway133", name: "blog") { 
    issues(orderBy: {field: UPDATED_AT, direction: ASC}, last: 100) {
      edges {
        cursor
        node { …

How to use Hexo to deploy static site

Issue #392

It’s been a long journey since https://github.com/onmyway133/blog/issues/1, next step is to keep GitHub issues as source, and mirror those to a static site.

Use 2 repos

How to use type coersion in Javascript

Issue #391

People who make fun of Javascript probably don’t understand implicit type coersion and when to use triple equal. Javascript is very unexpected, but when we work with this language, we need to be aware.

  1. Coercion–Automatically …

How to safely access deeply nested object in Javascript

Issue #390

An object ’s property can be null or undefined.

Accessing step by step is tedious

props.user &&
props.user.posts &&
props.user.posts[0] &&
props.user.posts[0].comments

Dynamic parsing path is too clever and …

How to use flow type in Javascript

Issue #389

Prefer flow over TypeScript for simplicity

Javascript primitive types number and string are too general and do not express the domain objects. Because lack of type alias in Javascript, we can use flow

export type Centimeter = number
export …

How to use flow in Kotlin

Issue #388

Asynchronous Flow

https://kotlinlang.org/docs/reference/coroutines/flow.html

Using List result type we can only return all the values at once. To represent the stream of values that are being asynchronously computed we can use Flow type …

How to choose Firebase vs Google Analytics

Issue #387

Google Analytics is shutting down. From Firebase Analytics console, we can choose to upgrade to Google Analytics, no code change is needed.

https://support.google.com/firebase/answer/9167112?hl=en

In October 2019, we will start to sunset …

How to use lessThan and greaterThan in Auto Layout in iOS

Issue #386

When it comes to right and bottom side, we should use negative values, and use lessThan, as it means less than a negative value

How to create bounce animation programmatically in Android

Issue #383

Right click res -> New -> Android Resource Directory, select anim and name it anim Right click res/anim -> New -> Android Resource file, name it bounce

<?xml version="1.0" encoding="utf-8"?>
<set …

How to use point in dp programmatically in Android

Issue #382

import android.content.Context

fun Int.toDp(context: Context?): Int {
    if (context != null) {
        val scale = context.resources.displayMetrics.density
        return (this.toFloat() * scale + 0.5f).toInt()
    } else { …

How to create constraints programmatically with ConstraintLayout in Android

Issue #381

From API < 17, there is ViewCompat.generateViewId() For API 17, there is View.generateViewId()

Note that to use ConstraintSet, all views under ConstraintLayout inside xml must have unique id

val imageView = ImageView(context)
imageView. …

How to use custom font as resource in Android

Issue #380

Downloadable fonts

https://developer.android.com/guide/topics/ui/look-and-feel/downloadable-fonts

Android 8.0 (API level 26) and Android Support Library 26 introduce support for APIs to request fonts from a provider application instead of …

How to get Hacker News top stories using parallel coroutine and Retrofit

Issue #379

interface Api {
    @GET("topstories.json?print=pretty")
    suspend fun getTopStories(): List<Int>

    @GET("item/{id}.json?print=pretty")
    suspend fun getStory(@Path("id") id: Int): Item
}
class Repo { …

How to show generic list in Fragment in Android

Issue #378

After having a generic RecyclerView, if we want to show multiple kinds of data in Fragment, we can use generic.

We may be tempted to use interface or protocol, but should prefer generic.

class FeedFragment() : Fragment() {
    override fun …

How to manage OneSignal push notification in iOS

Issue #377

OneSignal is an alternative for Parse for push notifications but the sdk has many extra stuff and assumptions and lots of swizzling.

We can just use Rest to make API calls. From https://github.com/onmyway133/Dust

Every official push …

How to do throttle and debounce using DispatchWorkItem in Swift

Issue #376

https://github.com/onmyway133/Omnia/blob/master/Sources/Shared/Debouncer.swift

import Foundation

public class Debouncer {
    private let delay: TimeInterval
    private var workItem: DispatchWorkItem?

    public init(delay: TimeInterval …

How to simplify UIApplication life cycle observation in iOS

Issue #375

final class LifecyclerHandler {
    private var observer: AnyObject!
    var action: (() -> Void)?
    private let debouncer = Debouncer(delay: 1.0)

    func setup() {
        observer = NotificationCenter.default.addObserver( …

How to do UITests with Google Maps on iOS

Issue #374

Interact with GMSMapView

Add accessibilityIdentifier to the parent view of GMSMapView. Setting directly onto GMSMapView has no effect

accessibilityIdentifier = "MapView"
let map = app.otherElements.matching(identifier: …

Make to make rounded background UIButton in iOS

Issue #373

UIButton.contentEdgeInsets does not play well with Auto Layout, we need to use intrinsicContentSize

final class InsetButton: UIButton {
    required init(text: String) {
        super.init(frame: .zero)

        titleLabel?.textColor = . …

How to make scrolling UIScrollView with Auto Layout in iOS

Issue #371

Scrolling UIScrollView is used in common scenarios like steps, onboarding. From iOS 11, UIScrollView has contentLayoutGuide and frameLayoutGuide

Docs

https://developer.apple.com/documentation/uikit/uiscrollview/2865870-contentlayoutguide …

How to use Product Hunt GraphQL API with Retrofit

Issue #370

Define response model

import com.squareup.moshi.Json

data class Response(
    @field:Json(name="data") val data: ResponseData
)

data class ResponseData(
    @field:Json(name="posts") val posts: Posts
)

data class Posts( …

How to fix Auto Layout issues in iOS

Issue #369

UITemporaryLayoutHeight and UITemporaryLayoutWidth

  • Demystify warnings with https://www.wtfautolayout.com/
  • Reduce priority
  • Use Auto Layout directly instead of using manual frame layout, specially for scrolling pager …

How to simplify anchor with NSLayoutConstraint in iOS

Issue #368

See https://github.com/onmyway133/Omnia/blob/master/Sources/iOS/NSLayoutConstraint.swift

extension NSLayoutConstraint {

    /// Disable auto resizing mask and activate constraints
    ///
    /// - Parameter constraints: constraints to …

How to get trending repos on GitHub using Retrofit

Issue #367

https://api.github.com/search/repositories?sort=stars&order=desc&q=language:javascript,java,swift,kotlin&q=created:>2019-08-21
interface Api {
    @GET("https://api.github.com/search/repositories")
    suspend fun …

How to use Retrofit in Android

Issue #366

Code uses Retrofit 2.6.0 which has Coroutine support

app/build.gradle

implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.2.0-alpha01"

implementation "com.squareup.moshi:moshi:$Version.moshi"

implementation …

How to handle link clicked in WKWebView in iOS

Issue #365

import WebKit
import SafariServices

final class WebViewHandler: NSObject, WKNavigationDelegate {
    var show: ((UIViewController) -> Void)?
    let supportedSchemes = ["http", "https"]

    func webView(_ webView: …

How to use AppFlowController in iOS

Issue #364

AppFlowController.swift

import UIKit
import GoogleMaps
import Stripe

final class AppFlowController: UIViewController {
    private lazy var window = UIWindow(frame: UIScreen.main.bounds)

    func configure() {
        GMSServices. …

How to use ViewModel and ViewModelsProviders in Android

Issue #363

ViewModels a simple example

https://medium.com/androiddevelopers/viewmodels-a-simple-example-ed5ac416317e

Rotating a device is one of a few configuration changes that an app can go through during its lifetime, including keyboard …

How to declare UIGestureRecognizer in iOS

Issue #362

let tapGR = UITapGestureRecognizer(target: self, action: #selector(handleTap(_:)))

@objc private func handleTap(_ gr: UITapGestureRecognizer) {
    didTouch?()
}

We need to use lazy instead of let for gesture to work

lazy var tapGR = …

How to use function builder in Swift 5.1

Issue #361

protocol Task {}

struct Build: Task {}
struct Test: Task {}

@_functionBuilder
public struct TaskBuilder {
    public static func buildBlock(_ tasks: Task...) -> [Task] {
        tasks
    }
}

public func run(@TaskBuilder builder: () …

How to simplify get GRPC streaming in Swift

Issue #360

Given a streaming service

service Server {
  rpc GetUsers(GetUsersRequest) returns (stream GetUsersResponse);
}

To get a response list in Swift, we need to do observe stream, which is a subclass of ClientCallServerStreaming

func getUsers( …

How to inject view model with Koin in Android

Issue #359

app/build.gradle

implementation "org.koin:koin-core:$Version.koin"
implementation "org.koin:koin-androidx-scope:$Version.koin"
implementation "org.koin:koin-androidx-viewmodel:$Version.koin"

MyApplication.kt

import …

How to use coroutine LiveData in Android

Issue #358

app/build.gradle

implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.2.0-alpha01"
import androidx.lifecycle.ViewModel
import androidx.lifecycle.liveData
import kotlinx.coroutines.Dispatchers

class MainViewModel : ViewModel …

How to declare generic RecyclerView adapter in Android

Issue #357

generic/Adapter.kt

package com.onmyway133.generic

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView

abstract class Adapter<T>(var items: …

How to use Payment Intent and Setup Intents with Stripe in iOS

Issue #356

StripeHandler.swift

From Stripe 16.0.0 https://github.com/stripe/stripe-ios/blob/master/CHANGELOG.md#1600-2019-07-18

Migrates STPPaymentCardTextField.cardParams property type from STPCardParams to STPPaymentMethodCardParams

final class …

How to format currency in Swift

Issue #355

final class CurrencyFormatter {
    func format(amount: UInt64, decimalCount: Int) -> String {
        let formatter = NumberFormatter()
        formatter.minimumFractionDigits = 0
        formatter.maximumFractionDigits = decimalCount …

How to simplify struct mutating in Swift

Issue #354

In Construction, we have a build method to apply closure to inout struct.

We can explicitly define that with withValue

func withValue<T>(_ value: T, closure: (inout T) -> Void) -> T {
    var mutableValue = value
    closure …

How to not resign first responder for UITextField in iOS

Issue #353

When using STPPaymentCardTextField from stripe-ios, the default behavior is when we touch outside to dismiss keyboard, it checks and focus on number text field is it is invalid

STPPaymentCardTextField.m

- (STPFormTextField *) …

How to use React JSX with Babel in Electron

Issue #352

For a normal electron app created with npm init, we can use all features of ES6, but not the JSX syntax for React. We can use just Babel to transpile JSX, as used in IconGenerator

.babelrc

{
  "plugins": [ …

How to define version property in gradle

Issue #351

From Gradle tips and recipes, Configure project-wide properties

For projects that include multiple modules, it might be useful to define properties at the project level and share them across all modules. You can do this by adding extra …

How to use Firebase PhoneAuth in iOS

Issue #350

Read Authenticate with Firebase on iOS using a Phone Number

Disable swizzling

Info.plist

<key>FirebaseAppDelegateProxyEnabled</key>
<string>NO</string>

Enable remote notification

Enable Capability -> Background …

How to use Navigation component with DrawerLayout in Android

Issue #349

Screenshot_1565169686

build.gradle

dependencies {
    classpath 'android.arch.navigation:navigation-safe-args-gradle-plugin:1.0.0-alpha05'
}

app/build.gradle

apply plugin: 'androidx.navigation.safeargs'

dependencies {
    def navigationVersion …

How to make digit passcode input in Swift

Issue #347

Add a hidden UITextField to view hierarchy, and add UITapGestureRecognizer to activate that textField.

Use padding string with limit to the number of labels, and prefix to get exactly n characters.

code

DigitView.swift

import UIKit

final …

How to make credit card input UI in Swift

Issue #346

We have FrontCard that contains number and expiration date, BackCard that contains CVC. CardView is used to contain front and back sides for flipping transition.

We leverage STPPaymentCardTextField from Stripe for working input fields, …

How to stop implicit animation when title change on UIButton

Issue #345

UIButton with system type has implicit animation for setTitle(_:for:)

Use this method to set the title for the button. The title you specify derives its formatting from the button’s associated label object. If you set both a title and an …

How to use addSubview in iOS

Issue #344

addSubview can trigger viewDidLayoutSubviews, so be careful to just do layout stuff in viewDidLayoutSubviews

This method establishes a strong reference to view and sets its next responder to the receiver, which is its new superview. …

How to run app on beta iOS devices

Issue #343

Xcode 10.3 with iOS 13

sudo ln -s /Applications/Xcode-beta.app/Contents/Developer/Platforms/iPhoneOS.platform/DeviceSupport/13.0 /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/DeviceSupport

Xcode 10.3 with iOS 13.1 …

How to submit electron app to AppStore

Issue #342

Before

Install electron as dev npm install electron --save-dev Update electron-packager npm install electron-packager@latest --save-dev Use no space in app name

Package with electron-packager

Follow …

How to format hour minute from time interval in Swift

Issue #340

Use DateComponentsFormatter

https://nshipster.com/formatter/#datecomponentsformatter

Results in no padding 0

func format(second: TimeInterval) -> String? {
    let formatter = DateComponentsFormatter()
    formatter.unitsStyle = . …

My First Post

Hugo on Vercel

How to cache URLSession response

Issue #339

For simple cases, we don’t need to. Let’s use urlCache

The URL cache for providing cached responses to requests within the session.

Accessing Cached Data

The URL Loading System caches responses both in memory and on disk, …

How to use ext in gradle in Android

Issue #338

Gradle uses Groovy and it has ext, also known as ExtraPropertiesExtension

Additional, ad-hoc, properties for Gradle domain objects.

Extra properties extensions allow new properties to be added to existing domain objects. They act like …

How to do custom presentation with UIViewPropertyAnimator in iOS

Issue #337

Normally we just present from any UIViewController in any UINavigationController in UITabBarController and it will present over tabbar

present(detailViewController, animated: true, completion: nil)

If we have animation with …

How to show dropdown in AppKit

Issue #336

Use NSPopUpButton

var pullsDown: Bool

A Boolean value indicating whether the button displays a pull-down or pop-up menu.

func addItem(withTitle: String) Adds an item with the specified title to the end of the menu.

Should disable …

How to scan up to character in Swift

Issue #335

This is useful when we want to get the first meaningful line in a big paragraph

let scanner = Scanner(string: text)
var result: NSString? = ""
scanner.scanUpTo("\n", into: &result)
return result as String?

How to use NSSecureCoding in Swift

Issue #334

NSSecureCoding has been around since iOS 6 and has had some API changes in iOS 12

A protocol that enables encoding and decoding in a manner that is robust against object substitution attacks. …

How to simplify pager interaction with Rx

Issue #333

In a traditional pager with many pages of content, and a bottom navigation with previous and next button. Each page may have different content, and depending on each state, may block the next button.

The state of next button should state …

How to use moveItem in NSCollectionView in AppKit

Issue #332

From moveItem(at:to:)

Moves an item from one location to another in the collection view.

After rearranging items in your data source object, use this method to synchronize those changes with the collection view. Calling this method lets …

How to show dropdown from NSSegmentedControl in AppKit

Issue #331

From NSSegmentedControl

The features of a segmented control include the following: A segment can have an image, text (label), menu, tooltip, and tag. A segmented control can contain images or text, but not both.

let languageMenu = NSMenu( …

How to make scrollable NSTextView in AppKit

Issue #330

When adding NSTextView in xib, we see it is embedded under NSClipView. But if we try to use NSClipView to replicate what’s in the xib, it does not scroll.

To make it work, we can follow Putting an NSTextView Object in an NSScrollView …

How to handle keyboard for UITextField in scrolling UIStackView in iOS

Issue #329

Firstly, to make UIStackView scrollable, embed it inside UIScrollView. Read How to embed UIStackView inside UIScrollView in iOS

It’s best to listen to keyboardWillChangeFrameNotification as it contains frame changes for Keyboard in …

How to make simple form validator in Swift

Issue #328

Sometimes we want to validate forms with many fields, for example name, phone, email, and with different rules. If validation fails, we show error message.

We can make simple Validator and Rule

class Validator {
    func validate(text: …

How to organise test files

Issue #327

In terms of tests, we usually have files for unit test, UI test, integeration test and mock.

Out of sight, out of mind.

Unit tests are for checking specific functions and classes, it’s more convenient to browse them side by side …

How to deal with weak in closure in Swift

Issue #326

Traditionally, from Swift 4.2 we need guard let self

addButton.didTouch = { [weak self] in
    guard
        let self = self,
        let product = self.purchasedProduct()
    else {
        return

    self.delegate?.productViewController …

How to make material UITextField with floating label in iOS

Issue #325

  • Use UILabel as placeholder and move it
  • When label is moved up, scale it down 80%. It means it has 10% padding on the left and right when shrinked, so offsetX for translation is 10%
  • Translation transform should happen before scale
  • Ideally …

How to embed UIStackView inside UIScrollView in iOS

Issue #324

view.addSubview(scrollView)
scrollView.addSubview(stackView)

NSLayoutConstraint.on([
    scrollView.pinEdges(view: view),
    stackView.pinEdges(view: scrollView)
])

NSLayoutConstraint.on([
    stackView.widthAnchor.constraint(equalTo: …

How to animate NSCollectionView changes

Issue #323

Use proxy animator()

let indexPath = IndexPath(item: index, section: 0)
collectionView.animator().deleteItems(at: Set(arrayLiteral: indexPath))

let indexPath = IndexPath(item: 0, section: 0)
collectionView.animator().insertItems(at: Set( …

How to handle right click in AppKit

Issue #322

lazy var gr = NSClickGestureRecognizer(target: self, action: #selector(onPress(_:)))

gr.buttonMask = 0x2
gr.numberOfClicksRequired = 1
view.addGestureRecognizer(gr)

How to show context menu in NSCollectionView

Issue #321

Detect locationInWindow in NSEvent

class ClickedCollectionView: NSCollectionView {
    var clickedIndex: Int?

    override func menu(for event: NSEvent) -> NSMenu? {
        clickedIndex = nil

        let point = convert(event. …

How to customize NSTextView in AppKit

Issue #320

Scrollable

textview

Embed image or NSTextAttachmentCellProtocol

  • Select TextView
  • Select Rich Text and Graphics
  • Select Size Inspector -> Resizable and tick both Horizontally …

How to make custom controller for View in iOS

Issue #318

I do UI in code, and usually separate between View and ViewController.

class ProfileView: UIView {}

class ProfileViewController: UIViewController {
	override func loadView() {
        self.view = ProfileView()
    }
}

But in places where …

How to make UIPanGestureRecognizer work with horizontal swipe in UICollectionView

Issue #315

extension PanCollectionViewController: UIGestureRecognizerDelegate {
    func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        let velocity = panGR.velocity(in: panGR.view)
        return abs( …

How to use media query in CSS

Issue #314

<meta name="viewport" content="width=device-width, initial-scale=1.0">

@media only screen and (max-width: 600px) {
  div.container {
    margin: 0 auto;
      width: 70%;
      margin-top: 60%;
    }     
}

body { …

How to use new APIs in iOS

Issue #313

iOS 10

UserNotifications

Push user-facing notifications to the user’s device from a server, or generate them locally from your app.

UIViewPropertyAnimator

A class that animates changes to views and allows the dynamic modification …

Links for WWDC

Issue #313

iOS 10

UserNotifications

Push user-facing notifications to the user’s device from a server, or generate them locally from your app.

UIViewPropertyAnimator

A class that animates changes to views and allows the dynamic modification …

What is new in iOS

Issue #313

iOS 10

UserNotifications

Push user-facing notifications to the user’s device from a server, or generate them locally from your app.

UIViewPropertyAnimator

A class that animates changes to views and allows the dynamic modification …

What's new in iOS

Issue #313

iOS 10

UserNotifications

Push user-facing notifications to the user’s device from a server, or generate them locally from your app.

UIViewPropertyAnimator

A class that animates changes to views and allows the dynamic modification …

How to avoid crash when closing NSWindow for agent macOS app

Issue #312

class ClosableWindow: NSWindow {
    override func close() {
        self.orderOut(NSApp)
    }
}

let window = ClosableWindow(
    contentRect: rect,
    styleMask: [.titled, .closable],
    backing: .buffered,
    defer: false
}

window. …

How to get cell at center during scroll in UICollectionView

Issue #311

See Omnia https://github.com/onmyway133/Omnia/blob/master/Sources/iOS/UICollectionView.swift#L30

extension HorizontalUsersViewController: UIScrollViewDelegate {
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) { …

How to show location in Apple Maps and Google Maps app in iOS

Issue #309

Apple Maps

let placemark = MKPlacemark(coordinate: coordinate, addressDictionary: nil)
let mapItem = MKMapItem(placemark: placemark)
mapItem.name = shop.name
mapItem.openInMaps(launchOptions: [:])

Google Maps …

How to make convenient touch handler for UIButton in iOS

Issue #308

If you don’t want to use https://github.com/onmyway133/EasyClosure yet, it’s easy to roll out a closure based UIButton. The cool thing about closure is it captures variables

final class ClosureButton: UIButton {
    var …

How to format distance in iOS

Issue #307

import MapKit

let formatter = MKDistanceFormatter()
formatter.unitStyle = .abbreviated
formatter.units = .metric
distanceLabel.text = formatter.string(fromDistance: distance) // 700m, 1.7km

How to mock grpc model in Swift

Issue #306

let json: [String: Any] = [
    "id": "123",
    "name": "Thor",
    "isInMarvel": true
]

let data = try JSONSerialization.data(withJSONObject: json, options: [])
let string = String(data: data, …

Favorite WWDC 2019 sessions

Issue #305

w1

This year I’m lucky enough to get the ticket to WWDC and I couldn’t be more satisfied. 5 conference days full of awesomeness, talks, labs and networking, all make WWDC special and memorial conference for every attendee.

As …

Support IP handover in rtpproxy for VoIP applications

Issue #304

If you do VoIP applications, especially with open sources like pjsip, you may encounter kamalio and rtpproxy to serve SIP requests. Due to limitation of NAT traversals, rtpproxy is needed to work around NAT. All SIP handshake requests go …

How to test drive view in iOS

Issue #303

Instead of setting up custom framework and Playground, we can just display that specific view as root view controller

window.rootViewController = makeTestPlayground()

func makeTestPlayground() -> UIViewController {
    let content = …

How to make carousel layout for UICollectionView in iOS

Issue #302

Based on AnimatedCollectionViewLayout

final class CarouselLayout: UICollectionViewFlowLayout {
    override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
        guard let attributes = super. …

How to make simple pan to dismiss view in iOS

Issue #301

Make it more composable using UIViewController subclass and ThroughView to pass hit events to underlying views.

class PanViewController: UIViewController {
    var animator = UIViewPropertyAnimator(duration: 0, curve: .easeOut)
    lazy …

How to style NSButton in AppKit

Issue #297

let button = NSButton()
button.wantsLayer = true
button.isBordered = false
button.setButtonType(.momentaryChange)
button.attributedTitle = NSAttributedString(
    string: "Click me",
    attributes: [
        NSAttributedString.Key …

How to highlight selection of NSCollectionViewItem

Issue #296

Original answer https://stackoverflow.com/a/54793979/1418457


In your NSCollectionViewItem subclass, override isSelected and change background color of the layer. Test in macOS 10.14 and Swift 4.2

class Cell: NSCollectionViewItem { …

How to debounce action in Flutter

Issue #293

Answer https://stackoverflow.com/a/55119208/1418457


This is useful to throttle TextField change event. You can make Debouncer class using Timer

import 'package:flutter/foundation.dart';
import 'dart:async';

class …

How to add indicator under tab bar buttons in iOS

Issue #288

selectionIndicatorImage

Use this property to specify a …

Understanding socket and port in TCP

Issue #287

When digging into the world of TCP, I get many terminologies I don’t know any misconceptions. But with the help of those geeks on SO, the problems were demystified. Now it’s time to sum up and share with others :D

What defines a unique …

How to use Gradle Kotlin DSL in Android

Issue #285

kts

settings.gradle.kts

include(":app")

build.gradle.kts

import org.gradle.kotlin.dsl.apply
import org.gradle.kotlin.dsl.dependencies
import org.gradle.kotlin.dsl.kotlin
import org.gradle.kotlin.dsl.*
import …

Learning VoIP, RTP and SIP (aka awesome pjsip)

Issue #284

Before working with Windows Phone and iOS, my life involved researching VoIP. That was to build a C library for voice over IP functionality for a very popular app, and that was how I got started in open source.

The library I was working …

How to deal with CSS responsiveness in Wordpress

Issue #283

Original post https://medium.com/fantageek/dealing-with-css-responsiveness-in-wordpress-5ad24b088b8b


During the alpha test of LearnTalks, some of my friends reported that the screen is completely blank in search page, and this happened in …

How to use One Dark theme and Fira Code font for IDEs

Issue #281

A good theme and font can increase your development happiness a lot. Ever since using Atom, I liked its One Dark theme. The background and text colors are just elegant and pleasant to the eyes.

One Dark

Original designed for Atom, …

How to fix wrong status bar orientation in iOS

Issue #280

Original post https://medium.com/fantageek/how-to-fix-wrong-status-bar-orientation-in-ios-f044f840b9ed


When I first started iOS, it was iOS 8 at that time, I had a bug that took my nearly a day to figure out. The issue was that the status …

What is create-react-native-app

Issue #279

Original post https://medium.com/fantageek/what-is-create-react-native-app-9f3bc5a6c2a3


As someone who comes to React Native from iOS and Android background, I like React and Javascript as much as I like Swift and Kotlin. React Native is …

How to use Bitrise CI for React Native apps

Issue #277

Original post https://codeburst.io/using-bitrise-ci-for-react-native-apps-b9e7b2722fe5


After trying Travis, CircleCI and BuddyBuild, I now choose Bitrise for my mobile applications. The many cool steps and workflows make Bitrise an ideal …

How to make Unity games in pure C#

Issue #275

Original post https://codeburst.io/making-unity-games-in-pure-c-2b1723cdc71f


As an iOS engineers, I ditched Storyboard to avoid all possible hassles and write UI components in pure Swift code. I did XAML in Visual Studio for Windows Phone …

20 recommended utility apps for macOS

Issue #274

Original post https://hackernoon.com/20-recommended-utility-apps-for-macos-in-2018-ea494b4db72b


Depending on the need, we have different apps on the mac. As someone who worked mostly with development, below are my indispensable apps. They …

How to run Android apps in Bitrise

Issue #273

Original post https://hackernoon.com/using-bitrise-ci-for-android-apps-fa9c48e301d8


CI, short for Continuous Integration, is a good practice to move fast and confidently where code is integrated into shared repository many times a day. …

How to make tag selection view in React Native

Issue #271

Original post https://hackernoon.com/how-to-make-tag-selection-view-in-react-native-b6f8b0adc891


Besides React style programming, Yoga is another cool feature of React Native. It is a cross-platform layout engine which implements Flexbox …

Getting to know some pragmatic programming language features

Issue #270

As you know, in the Pragmatic Programmer, section Your Knowledge Portfolio, it is said that

Learn at least one new language every year. Different languages solve the same problems in different ways. By learning several different …

How to add app icons and splash screens to a React Native app in staging and production

Issue #268

React Native was designed to be “learn once, write anywhere,” and it is usually used to build cross platform apps for iOS and Android. And for each app that we build, there are times we need to reuse the same code, build and tweak it a bit …

How to convert your Xcode plugins to Xcode extensions

Issue #267

Original post https://medium.freecodecamp.org/how-to-convert-your-xcode-plugins-to-xcode-extensions-ac90f32ae0e3


Xcode is an indispensable IDE for iOS and macOS developers. From the early days, the ability to build and install custom …

Get to know different JavaScript environments in React Native

Issue #266

Original post https://medium.freecodecamp.org/get-to-know-different-javascript-environments-in-react-native-4951c15d61f5


React Native can be very easy to get started with, and then at some point problems occur and we need to dive deep …

How to Make Linear Gradient View with Bridging in React Native

Issue #264

Original post https://medium.com/react-native-training/react-native-bridging-how-to-make-linear-gradient-view-83c3805373b7


React Native lets us build mobile apps using only Javascript. It works by providing a common interface that talks …

How to dismiss keyboard with react-navigation in React Native apps

Issue #263

Original post https://medium.com/react-native-training/how-to-dismiss-keyboard-with-react-navigation-in-react-native-apps-4b987bbfdc48


Showing and dismiss keyboard seems like a trivial thing to do in mobile apps, but it can be tricky in …

Learn iOS best practices by building a simple recipes app

Issue #258

I started iOS development when iOS 7 had been announced. And I have learned a bit, through working, advice from colleagues and the iOS community.

In this article, I’d like to share a lot of good practices by taking the example of a simple …

How to setup Android projects

Issue #257

checkstyle (Java)

Checkstyle is a development tool to help programmers write Java code that adheres to a coding standard. It automates the process of checking …

How to structure your project and manage static resources in React Native

Issue #256

Original post https://medium.freecodecamp.org/how-to-structure-your-project-and-manage-static-resources-in-react-native-6f4cfc947d92


React and React Native are just frameworks, and they do not dictate how we should structure our projects. …

How to overlay view on another view in React Native

Issue #254

Original post https://stackoverflow.com/a/54108708/1418457


Make our own convenient OverlayContainer. The trick is to use absolute with 100% size

// @flow

import React from 'react'
import { View, StyleSheet } from …

How to remove Cartography in iOS

Issue #252

Read more https://medium.com/flawless-app-stories/how-to-make-auto-layout-more-convenient-in-ios-df3b42fed37f

Description

This is a script to remove Cartography, and use plain NSLayoutAnchor syntax. Use Constraint.on() from Sugar. It will …

How to sort strings with number in Javascript

Issue #251

function sort() {
    const string = 
`
- Favorite WWDC 2017 sessions https://github.com/onmyway133/blog/issues/56
- Favorite WWDC 2018 sessions https://github.com/onmyway133/blog/issues/245
- How to do clustering with Google Maps in iOS …

How to move tab bar icons down in iOS

Issue #250

UITabBarItem subclasses from UIBarItem which has imageInsets. We need both top and bottom to avoid shrinking

viewController1.tabBarItem.imageInsets = UIEdgeInsets(top: 10, left: 0, bottom: -10, right: 0)

How to test LaunchScreen in iOS

Issue #249

Making splash screen with LaunchScreen.storyboard is now the default way to do in iOS. Testing it with UITests is a bit tricky as this screen is showed the system, and if we test that, we are just testing the system.

What we should test is …

How to use remote theme for GitHub Pages

Issue #248

Visit https://github.com/onmyway133/onmyway133.github.io


https://github.blog/2017-11-29-use-any-theme-with-github-pages/

Starting today, you can use any of the hundreds of community-curated themes on GitHub.com. To build your site with …

How to enable black mode in Google Maps in iOS

Issue #246

Use GMSMapStyle https://developers.google.com/maps/documentation/android-sdk/styling Export styling json from https://mapstyle.withgoogle.com/

let mapStyleUrl = Bundle.main.url(forResource: "mapStyle", withExtension: "json" …

Favorite WWDC 2018 sessions

Issue #245

Original post https://medium.com/fantageek/my-favourite-wwdc-2018-sessions-363d3fc9c9d5


Favourite WWDC 2018 sessions

This year I failed the lottery ticket to WWDC, and I also missed the keynote live stream because I was sailing on the …

How to get running window informations in macOS

Issue #243

From https://developer.apple.com/documentation/coregraphics/1455137-cgwindowlistcopywindowinfo

Generates and returns information about the selected windows in the current user session.

struct MyWindowInfo {
    let frame: CGRect
    let …

How to show full screen window programmatically in macOS

Issue #242

let window = NSWindow(contentRect: mainScreen.frame, styleMask: .borderless, backing: .buffered, defer: false)
window.level = .floating
window.contentView = NSView()
window.makeKeyAndOrderFront(NSApp)
NSApp.activate(ignoringOtherApps: true …

Useful git commands for everyday use!

Issue #239

Do you know that questions about git get the most views on StackOverflow? I’ve searched a lot on Google how to execute certain actions with git, and this actually slowed me down a lot. There are some actions that we tend to use a lot, so …

How to work around app damaged warning in macOS

Issue #238

“App” is damaged and can’t be opened. You should move it to the Trash.

👉 Disable gate keeper

sudo spctl --master-disable
spctl --status

Current workaround is to remove Launch At Login handling code.

How to fix UITargetAppPath should be provided in iOS tests

Issue #237

Go to test target, under Host Application, select another app target, and select the preferred app target again

How to shake NSView in macOS

Issue #233

Animation on macOS using CAAnimation

Shake

let midX = box.layer?.position.x ?? 0
let midY = box.layer?.position.y ?? 0

let animation = CABasicAnimation(keyPath: "position")
animation.duration = 0.06
animation.repeatCount = 4 …

How to fit UIBezierPath in frame in iOS

Issue #232

From https://github.com/xhamr/paintcode-path-scale with some updates

extension CGRect {
    var center: CGPoint {
        return CGPoint( x: self.size.width/2.0,y: self.size.height/2.0)
    }
}

extension CGPoint {
    func vector(to p1: …

How to use CAReplicatorLayer to make activity indicator in iOS

Issue #230

CAReplicatorLayer is a layer that creates a specified number of sublayer copies with varying geometric, temporal, and color transformations

Here we use instanceTransform which applies transformation matrix around the center of the …

How to do rotation for CALayer in iOS

Issue #229

Use keypath

let animation = CASpringAnimation(keyPath: #keyPath(CALayer.transform))
animation.fromValue = 0
animation.valueFunction = CAValueFunction(name: CAValueFunctionName.rotateZ)
animation.timingFunction = CAMediaTimingFunction(name: …

How to not use isRemovedOnCompletion for CAAnimation in iOS

Issue #228

CAAnimation is about presentation layer, after animation completes, the view snaps back to its original state. If we want to keep the state after animation, then the wrong way is to use CAMediaTimingFillMode.forward and …

How to make simple search box in iOS

Issue #227

final class SearchBox: UIView {
    lazy var textField: UITextField = {
        let textField = UITextField()
        let imageView = UIImageView(image: R.image.search()!)
        imageView.frame.size = CGSize(width: 20 + 8, height: 20) …

How to capture video in iOS simulator

Issue #226

Take screenshot

xcrun simctl io booted screenshot image.png

Record video

xcrun simctl io booted recordVideo video.mp4

How to use custom fonts in iOS

Issue #225

<key>UIAppFonts</key>
<array>
    <string>OpenSans-Bold.ttf</string>
    <string>OpenSans-BoldItalic.ttf</string>
    <string>OpenSans-ExtraBold.ttf</string>
    <string> …

How to create UITabBarController programmatically in iOS

Issue #224

let tabBarController = UITabBarController()

let navigationController1 = UINavigationController(rootViewController: viewController1)
let navigationController2 = UINavigationController(rootViewController: viewController2)
let …

How to run AppleScript in macOS

Issue #223

Surround script by single quotes

let script = 
"""
tell application "XcodeWay"
    activate
end tell
"""

let command = "osascript -e '\(script)'"

let process = Process()
process.launchPath …

How to make simple networking client in Swift

Issue #222

For more mature networking, visit https://github.com/onmyway133/Miami

final class NetworkClient {
    let session: URLSession
    let baseUrl: URL

    init(session: URLSession = .shared, baseUrl: URL) {
        self.session = session …

How to ignore App Transport Security in iOS

Issue #221

Ignore a host

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSExceptionDomains</key>
    <dict>
        <key>example.com</key>
        <dict>
            <key> …

How to run simple http server in Go

Issue #220

Handle url parameter

package main

import (
  "net/http"
  "log"
)

func handleGreeting(w http.ResponseWriter, r *http.Request) {
  messages, ok := r.URL.Query()["message"]
    
  if !ok || len(messages[0]) < 1 { …

How to use Stripe and Apple Pay in iOS

Issue #219

Show basic add card in iOS

import UIKit
import Stripe

final class MainController: UIViewController {

    func showPayment() {
        let addCardViewController = …

How to fix not found zlib problem in macOS Mojave

Issue #217

https://developer.apple.com/documentation/xcode_release_notes/xcode_10_release_notes

The command line tools will search the SDK for system headers by default. However, some software may fail to build correctly against the SDK and require …

How to use Sonarqube in Swift projects

Issue #216

Install Sonarqube

https://docs.sonarqube.org/latest/setup/get-started-2-minutes/

What is Personal team in Xcode

Issue #215

https://developer.apple.com/library/archive/qa/qa1915/_index.html

Xcode 7 and Xcode 8 allow you to select the free personal team provided with your Apple ID for signing your app. This team allows you to build apps for your personal use on …

How to map from Swift 5 Resul to RxSwift PublishSubject

Issue #214

extension Result {
    func to(subject: PublishSubject<Success>) {
        switch self {
        case .success(let value):
            subject.onNext(value)
        case .failure(let error):
            subject.onError(error) …

How to update NSMenuItem while NSMenu is showing in macOS

Issue #213

Use Runloop

timer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true, block: { [weak self] _ in
    let date = Date()
    self?.updateStopItem(seconds: finishDate.timeIntervalSince1970 - date.timeIntervalSince1970)
}) …

How to use Timer in Swift

Issue #212

Pre iOS 10

func schedule() {
    DispatchQueue.main.async {
      self.timer = Timer.scheduledTimer(timeInterval: 20, target: self,
                                   selector: #selector(self.timerDidFire(timer:)), userInfo: nil, repeats: …

How to overload functions in Swift

Issue #211

Function

Functions in Swift are distinguishable by

  • parameter label
  • parameter type
  • return type

so that these are all valid, and works for subscript as well

struct A {

  // return type
  func get() -> String { return "" } …

Understanding AVFoundation and MediaPlayer frameworks in iOS

Issue #210

Depending on what features we want to achieve, we need to go with either AVFoundation or MediaPlayer framework. As someone who worked with many apps that involve media playback, here are some of my observations

MPMoviePlayerController vs …

How to handle reachability in iOS

Issue #209

Here are what I learn about reachability handling in iOS, aka checking for internet connection. Hope you will find it useful, too.

This post starts with techniques from Objective age, but many of the concepts still hold true

The naive way …

Dealing with CSS responsiveness in Wordpress

Issue #208

During the alpha test of LearnTalks, some of my friends reported that the screen is completely blank in the search page, and this happened in mobile only. This article is how I identify the problem and found a workaround for the issue, it …

How to build with xcodebuild and automatic code signing

Issue #207

Team ID

Error

Message: "xcodebuild: error: invalid option '-teamID'

Use =

xcodebuild teamID=T78DK947F3

Does not work 😢

DEVELOPMENT_TEAM

https://pewpewthespells.com/blog/migrating_code_signing.html

xcodebuild DEVELOPMENT_TEAM= …

How to launch app at start up in macOS

Issue #205

ServiceManagement framework

https://developer.apple.com/documentation/servicemanagement/1501557-smloginitemsetenabled

SMLoginItemSetEnabled

Enable a helper application located in the main application bundle’s “Contents/Library/LoginItems” …

How to fix mismatched deployment between app and test target in Xcode

Issue #204

  • My macOS app target has deployment target 10.12, but when running test, I get
Compiling for OS X 10.11, but module 'MyApp' has a minimum deployment target of OS X 10.12: …

How to notarize macOS app

Issue #203

New Notarization Requirements

https://developer.apple.com/news/?id=04102019a

With the public release of macOS 10.14.5, we require that all developers creating a Developer ID certificate for the first time notarize their apps, and that all …

How to resize image using imagemagick

Issue #202

mogrify -resize 1280x800 s2.png

How to use shared AppGroup UserDefaults in macOS and Xcode extension

Issue #201

  • Go to both app and extension target, under Capabilities, enable AppGroup

  • Specify $(TeamIdentifierPrefix)group.com.onmyway133.MyApp

  • $(TeamIdentifierPrefix) will expand to something like T78DK947F3., with .

  • Then using is like a …

How to check file under Library in macOS

Issue #200

let home = NSSearchPathForDirectoriesInDomains(.applicationScriptsDirectory, .userDomainMask, true).first!
let path = home.appending(".XcodeWayExtensions/XcodeWayScript.scpt")
let exists = FileManager.default.fileExists(atPath: …

How to parse json in Go

Issue #199

Unmarshal using encoding/json

  • property in struct needs to be first letter capitalized
import (
	"net/http"
	"encoding/json"
	"io/ioutil"
	"fmt"
)

type MyJsonObject struct {
	Id string `json:"id"` …

How to resolve deep json object in Dart

Issue #198

If we are not on the edge with GRPC and Protocol Buffer, then most likely we are going to deal with Restful and JSON. In one of my Flutter apps I needed to consume JSON

JSON and serialization

The guide at …

How to generate grpc protobuf files

Issue #197

protoc

https://grpc.io/docs/quickstart/go.html

Install the protoc compiler that is used to generate gRPC service code. The simplest way to do this is to download pre-compiled binaries for your platform(protoc--.zip) from here: …

How to cache CocoaPods

Issue #196

CocoaPods vs Carthage

CocoaPods will build and compile our frameworks every time whenever you are doing the clean build or run pod install or pod update for the project.

Cache Carthage …

How to build a networking in Swift

Issue #195

Miami

Concerns

Parameter encoding is confusing …

How to construct URL with URLComponents and appendPathComponent in Swift

Issue #193

var components = URLComponents(string: "https://google.com/")
components?.path = "abc/"
components?.url

-> nil

var components = URLComponents(string: "https://google.com/")
components?.path = "/abc/" …

How to catch error in ApolloClient

Issue #192

Read https://www.apollographql.com/docs/react/features/error-handling How to catch actual error https://github.com/apollographql/apollo-client/issues/4016 🤔

import { Observable } from 'apollo-link'
import ApolloClient from …

How to do clustering with Google Maps in iOS

Issue #191

Basic with Google Maps

Add to Podfile pod 'GoogleMaps'

Add a custom marker

import GoogleMaps

final class StopMarker: GMSMarker {
    let stop: Stop

    init(stop: Stop) {
        self.stop = stop
        super.init()
        self.title = …

How to handle file picker in React

Issue #190

render() {
  <Button color="inherit" onClick={this.onImagePress} >Image</Button>
  <input ref="fileInput" type="file" id="myFile" multiple accept="image/*" style={{display: …

How to fix ApiException 10 in Flutter for Android

Issue #188

Get error com.google.android.gms.common.api.ApiException: 10 with google_sign_in package.

Read https://developers.google.com/android/guides/client-auth

Certain Google Play services (such as Google Sign-in and App Invites) require you to …

How to get Google account photo in Flutter

Issue #187

If you use Flutter, then you can access it via people.googleapis.com endpoint, code uses google_sign_in library

import 'package:google_sign_in/google_sign_in.dart';

Future<String> getPhotoUrl(GoogleSignInAccount account, …

Getting activity name through HKWorkoutActivityType in HealthKit

Issue #186

After fetching workouts with HKObjectType.workoutType() , we get HKWorkoutActivityType , which is an enum enum HKWorkoutActivityType : UInt . As of Swift 4.2, there are no way to get enum case as String because this enum has type UInt . …

Curry in Swift and Javascript

Issue #185

You may encounter curry in everyday code without knowing it. Here is a bit of my reflections on curry and how to apply it in Javascript and Swift.

Taking one parameter in Haskell

In Haskell, all function officially takes only 1 parameter. …

How to fix SSLPeerUnverifiedException in Android

Issue #184

Get error javax.net.ssl.SSLPeerUnverifiedException: No peer certificate in Android API 16 to API 19

Getting started

Read about HTTPS and SSL https://developer.android.com/training/articles/security-ssl Check backend TLS …

How to zoom in double in MapKit

Issue #183

func zoomInDouble(coordinate: CLLocationCoordinate2D) {
    let region = mapView.region
    let zoomInRegion = MKCoordinateRegion(
        center: coordinate,
        span: MKCoordinateSpan(
            latitudeDelta: region.span. …

How to select cluster annotation in MapKit

Issue #182

func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
    guard let coordinate = view.annotation?.coordinate else {
        return
    }
    
    if (view.annotation is MKClusterAnnotation) {
        zoomInDouble( …

How to cluster annotations in MapKit in iOS 11

Issue #181

https://developer.apple.com/documentation/mapkit/mkannotationview/decluttering_a_map_with_mapkit_annotation_clustering

final class AnnotationView: MKMarkerAnnotationView {
    override init(annotation: MKAnnotation?, reuseIdentifier: …

Understanding CanvasRenderingContext2D and UIBezierPath

Issue #180

CanvasRenderingContext2D

https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/bezierCurveTo

The CanvasRenderingContext2D.bezierCurveTo() method of the Canvas 2D API adds a cubic Bézier curve to the current sub-path. …

How to get properties of JSValue in JavascriptCore

Issue #179

let rough = context.objectForKeyedSubscript("myObject")
myObject.toDictionary()

How to run ffmpeg in macOS app

Issue #178

Install ffmpeg, which installs ffprobe

brew install ffmpeg

Find location of installed ffmpeg

which ffmpeg

Add all executables to project

Get error

unable to obtain file audio codec with ffprobe

Run in verbose mode

ffmpeg -v

Get

[debug] …

How to get path to resource in running macOS app

Issue #177

This is useful to refer to another executable in a running executable in Process

Bundle.main.path(forResource: "ffmpeg", ofType: "")!

How to run executable in macOS

Issue #176

Enable executable

chmod +x executable

Add executable file to target Use Process with correct launchPad

import Foundation

protocol TaskDelegate: class {
  func task(task: Task, didOutput string: String)
  func taskDidComplete(task: Task)
} …

How to print current directory using Process in macOS

Issue #175

let process = Process()
process.launchPath = "/bin/pwd"
process.arguments = []

Should be the same as FileManager.default.currentDirectoryPath

How to change NSTextField backgroundColor in NSPopover

Issue #174

Disable vibrancy mode of NSPopover

let popover = NSPopover()
popover.appearance = NSAppearance(named: NSAppearance.Name.aqua)

How to make scrollable vertical NSStackView

Issue #173

You might need to flip NSClipView

import AppKit
import Anchors
import Omnia

final class ScrollableStackView: NSView {
    final class FlippedClipView: NSClipView {
        override var isFlipped: Bool {
            return true
        } …

How to make view take up full width in vertical NSStackView

Issue #172

https://stackoverflow.com/questions/51644692/nsstackview-subviews-not-resizing-sub-stack-views/55220837#55220837

If you want child view inside vertical NSStackView to fill its parent width, then reduce contentCompressionResistancePriority …

How to load top level view from xib in macOS

Issue #171

var views: NSArray?
NSNib(nibNamed: NSNib.Name("ProfileView"), bundle: nil)?.instantiate(withOwner: nil, topLevelObjects: &views)
let profileView = views!.compactMap({ $0 as? ProfileView }).first!

How to fix MethodError - undefined method `real_path` with CocoaPods?

Issue #170

abc

I’m using cocoapods 1.6.0.beta.2 in a React Native apps and it has been working fine. The pods that I need is Firebase and FacebookSDK. Today after pod install, I got error

NoMethodError - undefined method `real_path' for …

Understanding weak and strong in Objective C

Issue #164

From my own blog post https://github.com/Fantageek/fantageek.github.io/blob/source/source/_posts/2014-06-27-understanding-weak-self-and-strong-self.markdown


Blocks are wonderful. To avoid retain cycle you often see the weakSelf - …

Interesting, how did you managed to plug GitHub issues into the dev.to platform

Issue #163

I’ve found your post https://dev.to/onmyway133/changing-electron-app-icon and fist thought there is some static site generator which uses GitHub’s issues as a datasource.

Am I right?

Trying IBM Watson

Issue #162

Using CircleCI 2.0

Issue #158

We ’ve been using CircleCI for many of our open source projects. Since the end of last year 2017, version 2.0 began to come out, and we think it’s good time to try it now together with Swift 4.1 and Xcode 9.3

The problem with …

Jitter buffer in VoIP

Issue #157

This post was from long time ago when I did pjsip


A jitter buffer temporarily stores arriving packets in order to minimize delay variations. If packets arrive too late then they are discarded. A jitter buffer may be mis-configured and be …

Some Windows Phone 7 development tips

Issue #156

This post was from long time ago when I did Windows Phone 7


Welcome back, today I will start writing all stuff, all the secret I ’ve revealed when developing for Windows Phone 7.

  1. When setting UIElement’s Visibility to …

How to calculate packet size in VoIP

Issue #155

As you have probably observed in your studies, there is a determined method for calculating VoIP packet sizes. The packet size depends on many different variables, so there is no great answer for an “average” packet size …

UITableViewCell and Model

Issue #154

The most common UI element in iOS is UITableView, and the most common task is to display the UITableViewCell using the model.

Although the title specifies UITableViewCell, but the problem involves other views (UICollectionView, custom …

Netcut and ARP

Issue #153

Bad people can use Netcut to limit other ’s internet access in same network

How does Netcut work

Netcut uses attacking technique called ARP Spoofing.

ARP (Address Resolution Protocol) is a link layer procotol, it is used for …

Make your own sliding menu on Android tutorial – Part 2

Issue #152

This is the part 2 of the tutorial. If you forget, here is the link to part 1.

Link to Github

In the first part, we learn about the idea, the structure of the project and how MainActivity uses the MainLayout. Now we learn how to actually …

Make your own sliding menu on Android tutorial - Part 1

Issue #151

This post was from long time ago when I did Android


I can’t deny that Facebook is so amazing, they made trends and people want to follow. That is the case of the sliding menu.

Searching many threads on SO, like these create android …

Dealing with updated pod in BuddyBuild

Issue #149

We’re using BuddyBuild as our CI. Today one of our dependencies gets a sweat update https://github.com/hyperoslo/BarcodeScanner/releases/tag/4.1.1. So we pod update BarcodeScanner in one of our projects that depends on it. All is …

Learning from Open Source Generic Factory

Issue #148

From https://github.com/devxoul/Pure/blob/master/Sources/Pure/FactoryModule.swift

public protocol FactoryModule: Module {

  /// A factory for `Self`.
  associatedtype Factory = Pure.Factory<Self>

  /// Creates an instance of a …

Using camelCase for abbreviations

Issue #147

Each language and platform has its own coding style guide. This goes true when it comes to abbreviations. I’ve had some debates about whether to use JSON or Json, URL or Url, HTTP or Http.

I personally prefer camelCase, so I’m …

Learning sine

Issue #146

To be written …

Old math equations become useful today 😍

sine

How to fix Uncaught Error Cannot find module react

Issue #145

This is my story through dealing with many dependencies when trying to package an electron app. Thanks to my friend https://github.com/timkurvers for helping me out ❤️ This is like a note for my future self

Fixing event

The other I got an …

How to use standalone UINavigationBar in iOS

Issue #144

There are times we want the same UIViewController to look good when it’s presented modally or pushed from UINavigationController stack. Take a look at BarcodeScanner and the PR https://github.com/hyperoslo/BarcodeScanner/pull/82

When …

How to deal with animation in UITests in iOS

Issue #143

Today I was writing tests and get this error related to app idle

    t =    23.06s         Assertion Failure: <unknown>:0: Failed to scroll to visible (by AX action) Button, 0x6000003827d0, traits: 8858370049, label: 'cart', …

How to generate QR code in AppKit

Issue #140

I need to generate QR code in https://github.com/onmyway133/AddressGenerator. Fortunately with CoreImage filter, it is very easy. Code is in Swift 4

import AppKit

final class QRCodeGenerator {
  func generate(string: String, size: CGSize) …

How to use Function Literals with Receiver in Kotlin

Issue #139

From https://kotlinlang.org/docs/reference/lambdas.html

class HTML {
    fun body() { ... }
}

fun html(init: HTML.() -> Unit): HTML {
    val html = HTML()  // create the receiver object
    html.init()        // pass the receiver …

How to use R.swift in UITest in iOS

Issue #138

Here is how to use R.swift in UITest target

  • Add Localizable.strings to UITest target
  • Declare pod
target 'MyAppUITests' do
  pod 'R.swift', '~> 4.0'
end
  • In UITest target settings, add $(FRAMEWORK_SEARCH_PATHS) …

Hiding back button in navigation bar in iOS

Issue #137

Use a custom NavigationController

import UIKit

class NavigationController: UINavigationController {
  override func viewDidLoad() {
    super.viewDidLoad()
    navigationBar.tintColor = .white
    navigationBar.barStyle = .black …

Using dlopen in iOS

Issue #133

With dlopen we can make uses of some private frameworks. It will be fun

From iPhone X home button

#import <dlfcn.h>

// somewhere in viewDidLoad
dlopen([binaryPath cStringUsingEncoding:NSUTF8StringEncoding], RTLD_NOW);
UIView *const …

Learning from Open Source Hit testing with UIWindow

Issue #132

From https://github.com/Flipboard/FLEX/blob/master/Classes/ExplorerInterface/FLEXWindow.m

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor]; …

How to make NSCollectionView programatically in Swift

Issue #131

Here’s how to create NSCollectionView programatically. We need to embed it inside NScrollView for scrolling to work. Code is in Swift 4

NSCollectionView

let layout = NSCollectionViewFlowLayout()
layout.minimumLineSpacing = 4 …

UnsafePointer in Swift

Issue #130

Code is in Swift 4

Constructing UnsafeMutablePointer

let byteCount = 32
let result = UnsafeMutablePointer<UInt8>.allocate(capacity: byteCount)

Data to UnsafePointer

extension Data {
  func toPointer() -> UnsafePointer<UInt8 …

How to prevent UIVisualEffectView crash

Issue #124

We all know that there’s a potential crash with UIVisualEffectView on iOS 11. The fix is to not add sub views directly to UIVisualEffectView, but to its contentView. So we should change

effectView.addSubview(button)

to

effectView. …

Understanding suspend function in Kotlin Coroutine in Android

Issue #123

Getting to know Coroutine

From https://kotlinlang.org/docs/reference/coroutines.html

To continue the analogy, await() can be a suspending function (hence also callable from within an async {} block) that suspends a coroutine until some …

Hashable and Set in Swift

Issue #122

From Set

You can create a set with any element type that conforms to the Hashable protocol. By default, most types in the standard library are hashable, including strings, numeric and Boolean types, enumeration cases without associated …

Generic declaration in Swift

Issue #121

These are different

class DiffService<T: MKAnnotation & Equatable>
class DiffService<T: MKAnnotation, Equatable>

A taste of MVVM and Reactive paradigm

Issue #120

Original post https://medium.com/flawless-app-stories/a-taste-of-mvvm-and-reactive-paradigm-5288a819cca1


A taste of MVVM and Reactive paradigm

I like Swift, like many other object oriented programming languages. Swift allows you to …

Collection Update

Issue #119

This is about collection update, how to provide correct IndexPath and a simple diffing algorithm

Commit as another user on GitHub

Issue #118

GitHub identifies users by email, so you can totally commit using someone ’s email. This is how to configure in SourceTree

sourcetree

Badge in GitHub

Issue #117

GitHub is so awesome. It is where people around the world collaborate with each other. It is more awesome to show more about you in your GitHub profile. How about a badge? a welcome text? It is doable with organization. GitHub takes time …

Case sensitivity in HTTP

Issue #115

Is URL case sensitive ?

According to HTML and URLs

URLs in general are case-sensitive (with the exception of machine names). There may be URLs, or parts of URLs, where case doesn’t matter, but identifying these may not be easy. …

Understanding let, apply, with, run in Kotlin

Issue #114

Picture worths thousand words. Code worths thousand pictures. I don’t understand much until I take a look at Standard.kt in Kotlin standard library.

/**
 * Calls the specified function [block] with `this` value as its receiver and …

Using Playground with CocoaPods

Issue #113

This is a follow up from my post Learning from Open Source: Using Playground on how to actually add a playground to your production project.

The idea is simple: create a framework so that Playground can access the code. This demo an iOS …

Using assembly

Issue #112

I’m very fascinated when people use assembly to perform investigation, that’s just mind blowing 💥 . Here are some of the favorite use cases

Talks

Issue #111

Please visit https://onmyway133.github.io/speaking

URL Routing with Compass

Issue #110

Medium version https://medium.com/@onmyway133/url-routing-with-compass-d59c0061e7e2


Apps often have many screens, and UIViewController works well as the basis for a screen, together with presentation and navigation APIs. Things are fine …

BuddyBuild and gradle.properties

Issue #109

People advise against storing keys inside build.gradles. We should store them on 1Password and populate our gradle.properties, so don’t track this file in git. Here is .gitignore file

*.iml

/build
/gradle.properties …

Communication between Fragment and Activity

Issue #108

There’s always need for communication, right 😉 Suppose we have OnboardingActivity that has several OnboardingFragment. Each Fragment has a startButton telling that the onboarding flow has finished, and only the last Fragment shows …

Coordinator and FlowController

Issue #106

Every new architecture that comes out, either iOS or Android, makes me very excited. I’m always looking for ways to structure apps in a better way. But after some times, I see that we’re too creative in creating architecture, …

Please reconsidering your choice of libraries

Issue #105

Are you willing to take vaccines you don’t know about?

I like open source. I ’ve made some and contributed to some. I also use other people ’s open source libraries and learn a lot from them 😇

Open source can help us …

How to make generic extension with associatedtype protocol in Swift

Issue #104

I like extensions, and I like to group them under 1 common property to easily access. This also makes it clear that these all belong to the same feature and not to confuse with Apple properties.

This is how …

How to make simple Binding in MVVM in iOS

Issue #103

If you use MVVM or any other kinds of helper classes, then there’s need to report back the result to the caller. In simple cases, without asynchronous chaining, RxSwift is a bit overkill, you can just implement your own Binding. …

How to use custom UINavigationBar in iOS

Issue #102

Today I was reading the project in IGListKit Tutorial: Better UICollectionViews, I encounter something I often overlook

let nav = UINavigationController(navigationBarClass: CustomNavigationBar.self, toolbarClass: nil)
nav. …

It's good to have a CI

Issue #101

I have Unit tests and UI tests pass on my simulator and device, locally. But when I make the build on Buddybuild, it fails with the reason Activity cannot be used after its scope has completed. People seem to have the same issue too. …

Diff algorithm

Issue #99

I’ve been searching for efficient ways to diff collections, here are some interesting papers that I find

Myers

How to use safeAreaLayoutGuide in iOS 10

Issue #98

The safeAreaLayoutGuide was introduced in iOS 11. And it is advised to stop using topLayoutGuide bottomLayoutGuide as these are deprecated.

To use safeAreaLayoutGuide, you need to do iOS version check

if #available(iOS 11.0, *) { …

Learning from Open Source Using Coordinator

Issue #97

The Coordinator pattern can be useful to manage dependencies and handle navigation for your view controllers. It can be seen from BackchannelSDK-iOS, take a look at BAKCreateProfileCoordinator for example

@implementation …

Learning from Open Source Managing dependencies

Issue #96

Another cool thing about ios-oss is how it manages dependencies. Usually you have a lot of dependencies, and it’s good to keep them in one place, and inject it to the objects that need.

The Environment is simply a struct that holds …

Learning from Open Source Using Playground

Issue #94

One thing I like about kickstarter-ios is how they use Playground to quickly protoyping views.

We use Swift Playgrounds for iterative development and styling. Most major screens in the app get a corresponding playground where we can see a …

Indenting Swift code

Issue #93

Hi, here is how I indent my code. Let me know what you think 😉

Using 2 spaces indentation

When possible, configure your editor to use 2 spaces for tab size. You will love it ❤️

spaces

Move first parameter to new line

If there are many parameters, …

Testing keychain in iOS

Issue #92

Today I was upgrading Keychain to swift 4, and take this opportunity to fix the test. The tests pass on macOS, but on iOS, I get -25300 error for

var status = SecItemCopyMatching(query as CFDictionary, nil)

It is because there is no …

Learning from Open Source Making macOS app in code

Issue #91

I’m familiar with the whole app structure that Xcode gives me when I’m creating new macOS project, together with Storyboard. The other day I was reading touch-bar-simulator and see how it declares app using only code. See this …

String manipulation in Apple Script

Issue #89

Today I find that AppleScript allows us to import Foundation, with that we have lots of power, including NSString. See my script

use scripting additions
use framework "Foundation"
property NSString : a reference to current …

How to call function inside Apple Script

Issue #88

I ’ve been using Apple Script to for my Finder extension FinderGo. Because of sandboxing, all scripts must lie inside Application Scripts folder.

Today, I was rewriting my Xcode extension XcodeWay. Before Xcode 8, we could use Xcode …

How to use Input and output container in Swift

Issue #87

This is a very nifty trick from ios-oss which was built around MVVM pattern. It uses protocol to define input and output, and a container protocol to contain them. Take …

Fixing login hanging in macOS High Sierra

Issue #86

Today I met a strange problem. After I enter my password, the progress bar runs to the end, and it is stuck there forever. No matter how many times I try to restart.

I finally need to go to Recovery mode by pressing Cmd+R at start up. I …

What about performance?

Issue #85

That’s the question I hear often when people are introduced to a new framework. It’s a valid concern. But it seems to me that they ask this just for fun. To my surprise, most people just don’t care, and the frameworks with …

Dear SDK developers

Issue #84

Dear SDK developers,

  • Please don’t do swizzling. Give us instructions on where to call your APIs instead of doing unnecessary swizzling. You’re making it for developers to use, and they know how to write code. Most of the time, …

How to migrate Codable object in Swift

Issue #83

As of swift 4 migration, we updated Cache to fully take advantage of Codable. It works for most cases, as we should usually declare our entity as typed safe object instead of array or json dictionary. And by conforming to Codable, it is …

How to handle alert in UITests in iOS

Issue #82

Usually in an app, you have the onboarding with steps that require push notification and location permission to be turned on. And you want to automate these steps via UITests

Firstly, you need to add interruption handler …

How to integrate library via custom podspec

Issue #81

Today I am about to integrate a library that does not support Cocoapods yet. It would be cumbersome to do it manually, because you have to configure xcconfig, framework search path, assets, and these steps are not well documented.

You can …

How to handle Swift version with Cocoapods

Issue #80

Today I was migrating Imaginary to Swift 4. But I get

    - ERROR | [OSX] xcodebuild: Returned an unsuccessful exit code.
    - ERROR | [OSX] xcodebuild:  Cache/Source/Mac/NSImage+Extensions.swift:32:64: error: 'png' has been …

Ikigai

Issue #79

I really like the concept of Ikigai

Ikigai (生き甲斐, pronounced [ikiɡai]) is a Japanese concept that means “a reason for being.” It is similar to the French phrase Raison d’être. Everyone, according to Japanese culture, has …

What if there is no inheritance?

Issue #77

My question

What if there is no inheritance, so that everybody has the same start, does that make life fairer?

Ad Hominem

Issue #76

I use Twitter a lot, mostly to follow people I like. They tweet cool things about tech and life. I learned a lot.

Please don’t show me the evil sides of the world ~ Michael Learn To Rock - How Many Hours

But there’s also bad …

Sync and async code in Swift

Issue #75

We should use DispatchQueue to build thread safe code. The idea is to prevent two read and write from happening at the same time from 2 different threads, which cause data corruption and unexpected behaviors. Note that you should try to …

How to check generic type in Swift

Issue #74

When dealing with generic, you shouldn’t care about the types. But if you need, you can

 func isPrimitive<T>(type: T.Type) -> Bool {
  let primitives: [Any.Type] = [
    Bool.self, [Bool].self,
    String.self, [String].self, …

How to use Given When Then in Swift tests

Issue #73

Spec

Using spec testing framework like Quick is nice, which enables BDD style.

describe("the 'Documentation' directory") {
  it("has everything you need to get started") {
    let sections = Directory( …

Understanding Instance property vs parameter in Swift

Issue #72

The other day I was refactor my code. I have

extension MainController: TabBarViewDelegate {

  func buttonDidPress index: Int) {
    let initialIndex = tabBarView.selectedIndex
    let wholeAppContentView = updateWholeAppContentView() …

How to push to GitHub gist

Issue #71

Creating a new gist

  • Go to https://gist.github.com/ and create a new gist
  • Note that you need to include filename + extension to enable automatic language markup
  • Click Add file to add more files

Cloning the gist

  • If you’ve enabled 2 …

How to observe object deinit in Swift

Issue #70

  • Today I was browsing through Suas-iOS and the subscription links to life cycle of another object
subscription.linkLifeCycleTo(object: self)

It observes the deinit of another job, interesting approach 👍 , take a look in …

How to deal with NODE_MODULE_VERSION in electron

Issue #69

NODE_MODULE_VERSION

Today I was trying to install sharp with yarn add sharp to work in my electron app, but I get the following error

Uncaught Error: The module …

How to support copy paste in electron

Issue #67

After running electron-packager, the app does not accept copy, paste anymore. This is because the release build does not have menu with key binding to the clipboard by default. We can solve this by manually declaring the menu

const {app} = …

How to change app icon in electron

Issue #66

Generate icns

  • Generate .iconset
  • Run iconutil -c icns "Icon.iconset". Note that icon names must be first letter lowsercased, and use _ instead of -

icns

Use icns

  • In main.js, specify icon
win = new BrowserWindow({
  width: 800, …

How to do implement notification in iOS with Firebase

Issue #64

Note: This applies to Firebase 4.0.4

Preparing push notification certificate

Go to Member Center -> Certificates -> Production

Certificate

You can now use 1 certificate for both sandbox and production environment push

Auth Key

  • If you …

How to use JSON Codable in Swift 4

Issue #63

Codable in Swift 4 changes the game. It deprecates lots of existing JSON libraries.

Generic model

API responses is usually in form of an object container with a key. Then it will be either nested array or object. We can deal with it by …

Learning flexbox

Issue #62

CSS

Flexbox

When to use hamburger menu in iOS

Issue #61

You can read everywhere that hamburger is mostly not recommended. Every navigation structure has their use cases, and for hamburger, I find these posts to have their points

How to deal with windows-1252 encoding in Node

Issue #60

Today I use node-fetch and cheerio to fetch a webpage. It looks good in Chrome and Sublime Text when it displays html entities like &#7901

However, it does not render correctly in iTerm, Terminal and Visual Studio Code. It just shows …

Pixel and point

Issue #59

TL;DR: Don’t use nativeScale and nativeBounds, unless you’re doing some very low level stuff

What is point and pixel

From …

Optional of optional in Swift

Issue #58

Do you know that an optional can itself contain an optional, that contains another optional? In that case, we need to unwrap multiple times

optionals

You mostly see it when you try to access window

let window = UIApplication.shared.delegate?.window …

Package node.js application

Issue #57

I like node.js because it has many cool packages. I wish the same goes for macOS. Fortunately, the below solutions provide a way to package node.js modules and use them inside macOS applications. It can be slow, but you save time by using …

Favorite WWDC 2017 sessions

Issue #56

  1. Introducing Core ML
  • Core ML
  1. Introducing ARKit: Augmented Reality for iOS
  • ARKit
  1. What’s New in Swift
  • String
  • Generic
  • Codable
  1. Advanced Animations with UIKit
  • Multiple animation
  • Interactive animation …

How to implement a tracker in Swift

Issue #55

I’m trying to implement a tracker, so the idea is that it can inject subscription upon method calls. It is best suit for logging, analytics, and it leverages RxCocoa

Usage

track(ListController.self) { _ in
  print("")
}

track …

How to change year in Date in Swift

Issue #54

Today I’m trying to change the year of a Date object to 2000 in Swift.

let date = Date()

Firstly, I tried with date(bySetting:) but it does not work with past year. It simply returns nil

Calendar.current.date(bySetting: .year, value: …

How to test for viewDidLoad in iOS

Issue #52

Suppose we have the following view controller

class ListController: UIViewController {
  override func viewDidLoad() {
    super.viewDidLoad()
    view.backgroundColor = .white
  }
}

Get to know viewDidLoad

We know that viewDidLoad is …

How to initialize Enums With Optionals in Swift

Issue #49

Today someone showed me https://medium.com/@_Easy_E/initializing-enums-with-optionals-in-swift-bf246ce20e4c which tries to init enum with optional value.

enum Planet: String {
  case mercury
  case venus
  case earth
  case mars
  case …

How to run UI Test with system alert in iOS

Issue #48

Continue my post https://github.com/onmyway133/blog/issues/45. When you work with features, like map view, you mostly need permissions, and in UITests you need to test for system alerts.

Add interruption monitor

This is the code. Note that …

How to master Auto Layout Visual Format Language

Issue #47

No, you don’t

How to run UITests with map view in iOS

Issue #45

Mock a location

You should mock a location to ensure reliable test

Create the gpx file

Go to Xcode -> File -> New -> GPX File

gpx

It looks like

<?xml version="1.0"?>
<gpx version="1.1" creator="Xcode" …

How to run UI Test with Facebook login

Issue #44

Today I’m trying to run some UITest on my app, which uses Facebook login. And here are some of my notes on it.

Challenges

  • The challenges with Facebook is it uses Safari controller, we we deal mostly with web view for now. …

Debugging iOS app

Issue #43

How to use assertionFailure and Optimization Level in iOS

Issue #39

We used to use assertionFailure to mark programmer error or something that shouldn’t happen.

From assertionFailure

Indicates that an internal sanity check failed.

Use this function to stop the program, without impacting the …

How to perform platform check with typealias and @available

Issue #38

The other day, I was building Anchors which needs to support iOS and macOS. What’s clever way to not use #if os(iOS) || os(tvOS) in all files? Use typealias

This is the first version. I’m trying to support iOS 8, macOS 10.10 …

How to use Controller and View in iOS

Issue #37

I like to write UI in code, and with Auto Layout, it is an easy task. However that leaves ViewController with a lots of code. One way we can do is to separate V from C in MVC, by using a dedicated view

We can do that with generic, that …

How to use MainController in iOS

Issue #36

Usually in an app, we have these flows: onboarding, login, main. And we usually set OnboardingController, LoginController and MainController as the root view controller respectively depending on the state.

I find it useful to have the …

How to handle Auto Layout with different screen sizes

Issue #35

Auto Layout is awesome. Just declare the constraints and the views are resized accordingly to their parent ’s bounds changes. But sometimes it does not look good, because we have fixed values for padding, width, height, and even fixed …

NSApplicationDelegate and notification

Issue #34

In an iOS project, we often see this in AppDelegate

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(application: UIApplication, didFinishLaunchingWithOptions …

How to define SDK and Deployment Target in iOS

Issue #33

I see that my answer to the question What’s the meaning of Base SDK, iOS deployment target, Target, and Project in xcode gets lots of views, so I think I need to elaborate more about it

Good read

App backed by website in iOS 9

Issue #32

iOS 9 introduces new ways for your app to work better, backed by your websites

Smart App Banners

If the app is already installed on a user’s device, the banner intelligently changes its action, …

Disingenuousness

Issue #31

I’m very happy to be on open source movement, and it ’ll be great to hear about what people have achieved

How to do curry in Swift

Issue #30

Haskell is notorious for currying, and Swift has currying, too

I love ReactiveCocoa, RxSwift and I always take time to dig into it. The other day, I was practise making Signal based on this talk UIKonf 2015 - Jens Ravens: Functional …

How to use push notification in iOS

Issue #29

Here are my notes for working with Push Notification, updated for iOS 9

How to register

  • Register to receive push notification

registerForRemoteNotificationTypes is deprecated in iOS 8+

UIApplication.sharedApplication(). …

Understanding push and pull signal in reactive paradigm

Issue #28

The idea of Signal may originate from Elm Reactivity, and it has now been widely adopted in iOS

I once asked What are examples of hot and cold signal in ReactiveCocoa?

How to make iOS Stretchy Header with Auto Layout

Issue #27

Stretchy header is cool. People are familiar with changing frames to achieve this, like Design Teardown: Stretchy Headers. But with Auto Layout, we can achieve this with much nicer declarative constraints

The demo project is StretchyHeader …

How to group digits in Swift

Issue #26

When working on Scale I think it’s good to have a way to group the digit so that it is easier to reason

Luckily, Swift already supports this. See The Swift Programming Language - Numeric Literals

Numeric literals can contain extra …

How to make a simple resolver in Swift

Issue #25

The Marvel world

Ant Man

We know Ant Man is Hank Pym

struct AntManSuit {
    let name: String
}

struct HankPym {
    let suit = AntManSuit(name: "Ant Man ID #101")

    func fight() {
        print("Fighting with the suit named …

How to make lighter AppDelegate in iOS

Issue #24

There is Lighter View Controllers, and there is Lighter AppDelegate, too

Since working with iOS, I really like the delegate pattern, in which it helps us defer the decision to another party.

The iOS application delegates its event to …

How to debug Auto Layout

Issue #23

hasAmbiguousLayout

Returns whether the constraints impacting the layout of the view incompletely specify the location of the view.

exerciseAmbiguityInLayout

This method randomly changes the frame of a view with an ambiguous layout between …

How to create a piano using iOS 9 Auto Layout

Issue #22

In the beginning, people use frame and Autoresizing Mask, then they use Auto Layout, then iOS 9 encourages them to use NSLayoutAnchor, UILayoutGuide and UIStackView

For more convenient Auto Layout, check How to make Auto Layout more …

How to handle RefreshControl in iOS

Issue #20

The other day I was doing refresh control, and I saw this Swift Protocols with Default Implementations as UI Mixins

extension Refreshable where Self: UIViewController
{
    /// Install the refresh control on the table view
    func …

How to hack iOS apps

Issue #19

We need to care about security nowadays, here are some links I find useful to read more about this matter

How to deal with singleton in iOS

Issue #18

A single singleton

There are many classes that designed to be used as singleton, like UserDefaults.standard, FileManager.default, NotificationCenter.default or even our own classes like UserManager, Storage, … Singleton is a design …

Swift snippets

Issue #17

I always forget how to write correct #available( or #if swift(>=3.0) or just lazy to write required init?(coder aDecoder: NSCoder) every time I make a subclass. That’s why I made SwiftSnippets to save time for these tedious tasks. …

Composition in Realm

Issue #13

There is time we have models that have some kind of inheritance, like Dog, Cat, Mouse can be Animal. We can use composition to imitate inheritance, we just need to make sure it has unique primary key

Cat and Dog

These are pretty much basic …

How to do delegate with RxSwift

Issue #12

We can use DelegateProxy and DelegateProxyType to make beautiful delegate with RxSwift. But in some other cases, we can just create a custom class with PublishSubject.

This is how we can make rx out of UIApplication life cycle events

class …

How to group extension methods in Swift

Issue #11

Swift allows us to define more methods on existing class using extension.

extension UIView {

  func shake() {

  }

  func fade() {

  }

  func centerIn(anotherView: UIView) {

  }
}

If you ‘re afraid of the naming conflict, you can …

How to execute an action only once in Swift

Issue #10

There is time we want to execute an action only once. We can surely introduce a flag, but it will be nicer if we abstract that out using composition. Then we have

class Once {

  var already: Bool = false

  func run(@noescape block: () …

Law of Jante

Issue #9

The other day I was watching Inside Sweden’s Silicon Valley, and he mentions Law of Jante

It is pretty much this

  • You’re not to think you are anything special.
  • You’re not to think you are as good as we are.
  • You’re not to think you are …

Advices to students

Issue #8

Some say these are from the book Dumbing Down America of Charles Sykes, some say they are from Bill Gates ’s speech to high school students. I don’t know the origin, but they are cool enough, so I tend to share it again and again …

Check before you commit

Issue #7

Original post https://medium.com/@onmyway133/check-before-you-commit-5a7601cffc87


We usually have some experiment code that we don’t want they to step into our commit. I usually mark my experiment with // but sometimes forget to unstage …

Markdown editor

Issue #6

I like writing with markdown, it gives me comfortable experience with complete control over what I want to write.

I recommend vmd which renders exactly as GitHub. vmd is for rendering only, you need an editor to write, I use Sublime Text …

Primary key in Realm

Issue #4

Realm is great. But without primary key, it will duplicate the record, like https://github.com/realm/realm-java/issues/2730, http://stackoverflow.com/questions/32322460/should-i-define-the-primary-key-for-each-entity-in-realm, … So to …

How to take an app from a private framework in macOS

Issue #3

The other day I was browsing through https://github.com/sindresorhus/touch-bar-simulator, it was very brilliant of him to pull IDETouchBarSimulatorHostWindowController from DFRSupportKit.framework. This is worth checking out

Configuration closure in Swift

Issue #2

When I was reading through Swinject, I found something interesting https://github.com/Swinject/Swinject/blob/master/Sources/Container.swift

public convenience init(parent: Container? = nil, registeringClosure: (Container) -> Void) { …

Hello world, again

Issue #1

I’ve used Wordpress, then moved to GitHub Pages with Jekyll, Octopress, Hexo, Hugo. You can view my page here http://fantageek.com/. It was good with all the custom themes and Disqus

But then I was a bit lazy with all the commands …