Using SwiftUI and WidgetKit to make your app content indispensable

By using a combination of SwiftUI and WidgetKit, you can increase the visibility of your app’s content and enhance the user experience by placing one or more “widgets” on the user’s iOS Home screen, macOS Notification Center, and/or iOS Today View. Widgets should display your app’s most relevant content, allowing users to get important information with a glance. When a user taps/clicks a widget, they go straight to your app and should land on a page that gives them more details about what they just glanced at. Take a look at your iPhone’s Today View right now. There are widgets for weather, battery power, maps, stock exchanges — with many more available. For example, you can get an almost-instant read on the current atmospheric conditions by glancing at one of several available weather widgets. This time of year, before I leave the house, I glance at the weather widget to determine if I need a coat and hat before going outside. If I want to get the forecast for later in the day, I just tap on that weather widget to open the respective weather app.

Continue reading “Using SwiftUI and WidgetKit to make your app content indispensable”

New in iOS 12 – Adding a Custom UI and Interactivity Inside Local and Push Notifications

My original article – “New in iOS 12: Adding a Custom UI and Interactivity in Local and Push Notifications” – was published on appcoda.com.

INTRODUCTION

If you look at Apple’s “What’s New in iOS” 12 page, you’ll find a section entitled “Interactive Controls in Notifications,” which exclaims:

Notification content app extensions now support user interactivity in custom views. If the content of your app’s notifications needs to prompt user interaction, add controls like buttons and switches.

In this tutorial, I’m going to show you how to give your local and remote (push) notifications a custom user interface (UI). Users can now interact with a notification’s content area. iOS 12 has given us the ability to add a UIViewController subclass to notifications which we can customize. We can add controls like UIButton, UIImageView, and UISwitch to the view controller, wire up custom functionality using IBOutlet and IBAction, and arrange our custom UI using Auto Layout — all within the notification itself. We can provide support for more than a single tap. We can develop pretty much any type of user experience we want, within notification space limitations and timing considerations.

I’ll show you how a user can take action in response to a notification by interacting only with a customized notification interface, and conveniently not having to open up an app. I’ll be showcasing software released to developers just ten days ago (June 19), specifically iOS 12 beta 2 and Xcode 10 beta 2.

By the end of this tutorial, you’ll be able allow to your app users to get a notification, see a custom UI, click on a button, and get a confirmation — all inside a notification, like so:

Continue reading “New in iOS 12 – Adding a Custom UI and Interactivity Inside Local and Push Notifications”

New in iOS 12 – Implementing Provisional Authorization for Quiet Notifications in Swift 4.2

RELATED: Learn how to add a custom user interface INSIDE OF local and push notifications in “New in iOS 12 — Adding a Custom UI and Interactivity Inside Local and Push Notifications.”

With iOS 12, Apple fine-tuned the notification authorization process and expanded notification delivery options, giving developers the ability to build apps with high opt-in, reaction, and retention rates, thus leading to potentially higher revenues. The company announced these new features during a WWDC 2018 presentation entitled “What’s New in User Notifications.” App developers now have the ability to start sending notifications without explicit permission, i.e., on a trial basis. Apple calls this new notification management protocol “provisional authorization” which is closely related to a feature they’ve named “deliver quietly.” In this tutorial, I’ll show you how to encode these new notification features using software released to developers just fifteen days ago (June 19), specifically iOS 12 beta 2 and Xcode 10 beta 2 (which includes Swift 4.2).

To give you an idea of the code I’ll be writing and explaining in this tutorial, here are two videos of my sample app delivering a notification provisionally on an iPhone 8 Plus. Notice iOS 12 has multiple options for configuring how future notifications will be delivered:

Continue reading “New in iOS 12 – Implementing Provisional Authorization for Quiet Notifications in Swift 4.2”

Protocol Oriented Programming in Swift: Advanced Applications

The original article – Protocol Oriented Programming in Swift: Is it better than Object Oriented Programming? – was published on appcoda.com.

Introduction

We’re going to talk in-depth about protocol-oriented programming (POP) using Swift 4 in this article. This post is the second and final article in a two part series. If you haven’t read the first, introductory article, please do so before continuing onwards. Today, we’ll: discuss why Swift is considered a “protocol-oriented” language, compare POP and object-oriented programming (OOP), compare value semantics and reference semantics, consider local reasoning, implement delegation with protocols, use protocols as types, use protocol polymorphism, review my real-world POP Swift code, and finally, discuss why I’ve not bought 100% into POP. Download the source code from the article so you can follow along: There are 2 playgrounds and one project on GitHub, both in Xcode 9 format and written in Swift 4.

Continue reading “Protocol Oriented Programming in Swift: Advanced Applications”

Concurrency in iOS: Introduction to the abstract Operation class and using its BlockOperation subclass to run tasks in parallel

[Download the full Xcode 9 project, written in Swift 4, from GitHub.]

Swift tutorials by iosbrain.comI’m going to introduce you to iOS concurrency with simple Swift 4 code that uses an API based on the Operation abstract class. In more complex scenarios, you would subclass and customize Operation, but iOS provides the built-in BlockOperation subclass of Operation which we’ll use here. I’ll review the tenets of concurrency, emphasize why it is necessary in almost all apps, explain the basic infrastructure of Operation, compare it to Grand Central Dispatch (GCD), and then walk you through the Swift 4 code I wrote to implement concurrency in a real-live app based on BlockOperation. I’ll even show you how to graphically visualize your app’s CPU and thread usage with Xcode’s Debug Navigator. Here’s the app that we’ll build together:

Press the play button if you missed the first showing

We now live in the day and age of writing apps that can run on devices with CPUs that have multiple cores. We can go way beyond the notion of “multitasking” as a bunch of processes vying for a “timeslice” of the CPU. We can literally run processes simultaneously on different cores. As iOS developers, it is vitally important that we understand the concept of concurrency. Why?

Continue reading “Concurrency in iOS: Introduction to the abstract Operation class and using its BlockOperation subclass to run tasks in parallel”

Tutorial: delegates and delegation in Swift 4

The original article – Understanding Delegates and Delegation in Swift 4 was published on appcoda.com.

[Download Xcode 9 project with full Swift 4 source from GitHub.]

Introduction

I’m going to talk about “delegates” and “delegation.” I’ll lead you through a simple example of implementing the delegation design pattern in Swift 4, with full source code. My intent here is to show you how delegation works without getting bogged down in some crazy complex example. To help you become the best of the best, I’m going to introduce you to one of the greatest design tools to aid in object-oriented software development, UML. I’ll show you a UML diagram that I drew up to design and document the implementation of the delegation design pattern used in the sample app we’ll build together. Download the Xcode 9 project with full Swift 4 source from GitHub to follow along.

(Note: compare this post’s approach of using delegation with my next post’s approach of using NSNotificationCenterto accomplish the same goal.)

I’ll show you how to build a user interface (UI) helper, a class that downloads a file at a specified URL. Most importantly, I’ll show you how, through delegation, a UIViewController subclass can be notified by the helper that an image file has finished downloading, and then the view controller can display the image on screen. For the sake of simplicity and clarity, we’ll pretend that Swift has minimal support for downloading a file from a URL. We’ll manually wire up the notification that the file has finished downloading using the delegation design pattern. Here’s the app we’ll build:

Continue reading “Tutorial: delegates and delegation in Swift 4”

Tutorial: delegates and delegation in Objective-C

[Download Xcode 9 project with full Objective-C source from GitHub.]

Introduction

I’m going to talk about “delegates” and “delegation.” I’ll lead you through a simple example of implementing the delegation design pattern in Objective-C, with full source code, and then show you a more sophisticated scenario. My intent here is to show you how delegation works without getting bogged down in some crazy complex example. Download the Xcode 9 project with full Objective-C source from GitHub to follow along.

I’ll show you how to build a user interface (UI) component, a status/progress indicator, which you can display on screen for processing-intensive tasks… AND I’ll show you how you can customize the behavior of the indicator by using delegation. For example, when the indicator starts, you could disable your UI; when the indicator stops, you could re-enable your UI; and, when the user taps the indicator, you could cancel processing-intensive tasks. Here’s the app we’ll build:

Continue reading “Tutorial: delegates and delegation in Objective-C”

Fix for IBOutlet, IBAction connections disappearing in Xcode 9

You’ve been working on your billion dollar app happily for days or weeks. It’s Monday morning, you open up Xcode 9 to get back to work and — dang, bummer — all your IBOutlet and IBAction connections look like they’ve been disconnected (see image below):

Continue reading “Fix for IBOutlet, IBAction connections disappearing in Xcode 9”

Understanding Swift 4 protocols and using them in your apps

NOTE: Learn all about protocol-oriented programming in Swift here, here, and here.

[Download two Xcode 9 playgrounds with full Swift 4 source from GitHub.]

We’re going to talk about “protocols” in the Swift 4 language today. I’ll explain them conceptually, and then we’ll start coding protocols with a simple example. We’ll then create our own versions of the Apple built-in Equatable and Comparable protocols, and apply them to two real-world classes, one for tracking financial securities and one for representing geometric lines/vectors. Finally, we’ll test our geometric “Line” class in a type of Swift playground that supports rendering user interface components (like UIView) live in the simulator. But first, please ponder the layman’s definition of the word “protocol” before moving on:

… The official procedure or system of rules governing affairs of state or diplomatic occasions. …

The accepted or established code of procedure or behaviour in any group, organization, or situation. …

A procedure for carrying out a scientific experiment…

Swift Protocols

Apple’s “The Swift Programming Language (Swift 4.0.3)” documentation states:

Continue reading “Understanding Swift 4 protocols and using them in your apps”

Polymorphism in Swift 3: manipulate multiple related controls with one IBOutlet and one IBAction

How would you enable or disable multiple user interface controls using one IBOutlet and one IBAction? For example, you might need to disable a UITextBox and UISegmentedControl because a user’s login has expired. Perhaps a user hasn’t filled in some required fields on a form, so you want to disable several buttons. Watch the following video to see how I built a Swift 3 app to use a UISwitch to enable or disable four controls all at one time — and I demonstrated the object-oriented programming (OOP) principle of polymorphism:

Refresh your memory about OOP and inheritance.

Continue reading “Polymorphism in Swift 3: manipulate multiple related controls with one IBOutlet and one IBAction”