Configure Audio Session for background audio mode (iOS Project)

We use AVPlayer from AVFoundation to play audio in our applications. The moment we send the application to back ground mode, audio stops playing.

To fix this, we will need to configure AVAudioSession properly. We start by importing AVFoundation in AppDelegate or our iOS Project.

import AVFoundation

Next, add the following code in application didFinishLaunchingWithOptions:

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

    // enable playback category: this is required for background audio to function normally
    let audioSession = AVAudioSession.sharedInstance()
    try? audioSession.setCategory(AVAudioSessionCategoryPlayback, mode: AVAudioSessionModeDefault)
    try? audioSession.setActive(true, with: [])
    
    return true
}

Finally, enable Background Audio Mode in Project Settings.
Enable background audio mode in XCode Project Settings

Here is gist for the example

Working with live views in Swift Playground

Prototype faster in Swift Playground without Projects

When working on prototypes it is easy to create a new Playground to experiment on views. This makes the development really fast and easy. This method is much faster than creating a new XCode Project for prototyping.

Below is the code I use in Swift Playground


import UIKit
import PlaygroundSupport

let f = CGRect(x: 0.0, y: 0.0, width: 50.0, height: 50.0)
let v = UIView(frame: f)
v.backgroundColor = UIColor.yellow

PlaygroundPage.current.liveView = v

 

Then, to see the live view, select ViewAssistant EditorShow Assistant Editor

Show Assistant Editor in XCode

 

And then select Timeline option to see the live view.

Show Playground Timeline in Assistant Editor (XCode)

 

Later, we will add UIView with red background color as sub-view to test live updates.


// will be adding red view as sub view
let redView = UIView(frame: CGRect.zero)
redView.translatesAutoresizingMaskIntoConstraints = false
redView.backgroundColor = UIColor.red

v.addSubview(redView)

redView.centerXAnchor.constraint(equalTo: v.centerXAnchor, constant: 0.0).isActive = true
redView.centerYAnchor.constraint(equalTo: v.centerYAnchor, constant: 0.0).isActive = true
redView.widthAnchor.constraint(equalToConstant: 15.0).isActive = true
redView.heightAnchor.constraint(equalToConstant: 15.0).isActive = true

 

And finally, see the updates.

See live updates in Playground (XCode)

Generate Random Colors in Swift

Randomize UIColor

We have all come across scenarios where we need to use random colors. For example, showing UIViewControllers with random background color in a UIPageViewController, or just show UITableViewCells with random background colors in a UITableView.

I have created an extension on UIColor to help me in the applications.

Here is the code in Swift 3:

extension UIColor {
    static var random: UIColor {
        // Seed (only once)
        srand48(Int(arc4random()))
        return UIColor(red: CGFloat(drand48()), green: CGFloat(drand48()), blue: CGFloat(drand48()), alpha: 1.0)
    }
}

 

Usage:

view.backgroundColor = UIColor.random

 

Here is the github repo for the sample project:

https://github.com/hashaam/Sample-RandomColor