Best iOS Development Tips and Tricks - Part 3

1. Clean iOS app launch screen cache

You might have noticed when you tried to change something in the launch screen on the iOS application, you still see the old launch screen. Because the system caches launch images and it's not cleared even after deleting the application.

For cleaning the launch screen, all you have to do to completely clear your app’s launch screen cache is run this code inside your app:

import UIKit

public extension UIApplication {
    func clearLaunchScreenCache() {
        #if DEBUG
        do {
            let launchScreenPath = "\(NSHomeDirectory())/Library/SplashBoard"
            try FileManager.default.removeItem(atPath: launchScreenPath)
        } catch {
            print("Failed to delete launch screen cache - \(error)")
        }
        #endif
    }
}

To use this, add the following lines in the app's didFinishLaunchingWithOptions delegate method:

UIApplication.shared.clearLaunchScreenCache()

Checkout sample project on Github.

2. Add Function Documentation

You can use + + / (option + command + ) to add function documentation.

3. Project Build Settings Documentation

Do you know Xcode's project build setting has documentation? Just press (option) + Double Click for complete documentation or open Quick Help Inspector.

4. xed - Xcode Text Editor Invocation Tool

If you are in your Xcode project folder and your project has both a workspace(.xcworkspace) and a project(.xcodeproj) the file then you can use following command to open workspace in Xcode from your terminal -

xed .

If your project only has a project(.xcodeproj) the file then the same command will open your project.

You can also open/edit a single file from your Xcode project without launching the complete Xcode workspace or project. Like if you want to open your Podfile then we can open using -

xed Podfile

xed was introduced in Mac OS X 10.5 with Xcode 3.0.

5. Using #function for UserDefaults key

You can use #function literal to get the property name as UserDefault key. In the following example, #function will get replaced by accessToken. Be aware, changing the property names may break functionality for the existing users.

extension UserDefaults {
    var accessToken: String? {
        get {
            return string(forKey: #function)
        }
        set {
            set(newValue, forKey: #function)
            synchronize()
        }
    }
}

6. Strongly Typed Info.plist

You can easily access info.plist strongly typed by create a struct this way -

struct InfoPlist<T> {
    static func get(for key: String) -> T? {
        return  Bundle.main.infoDictionary?[key] as? T
    }
    
    static subscript(key: String) -> T? {
        return get(for: key)
    }
}

Now you can use above InfoPlist<T> struct like this -

let bundleIdentifier = InfoPlist<String>["CFBundleDisplayName"]

7. Generate Strongly-Typed Swift or Objective-C Data Models directly from JSON

Quicktype generate Strongly-Typed Swift/Objective-C Data Models from JSON, Schema, and GraphQL. You just need to paste your JSON string in left side text box. Generated models code has comments at the top with a code sample that shows how to convert a JSON string to instances of the generated types. You can also use any other way.

8. Create Empty Commits

Sometimes we need to make empty commits on some testing branch so that we can trigger various kinds of things like Jenkins build etc. So, here the way to make an empty commit which will trigger a push event on GitHub/GitLab, etc. This is more convenient than adding spaces to your README.md file.

git commit --allow-empty -m "Trigger Jenkins Build"

9. UIRefreshControl for any UIScrollView

Apple documentation shows us how we can use an UIRefreshControl in an UITableViewController, but do you know, we can add it to any UIScrollView ?

let refreshControl = UIRefreshControl()
refreshControl.addTarget(self, action: #selector(handleRefresh), for: .valueChanged)
scrollView.addSubview(refreshControl)

In a similar way, we can add UIRefreshControl to a WKWebView.

let refreshControl = UIRefreshControl()
refreshControl.addTarget(self, action: #selector(handleRefresh), for: .valueChanged)
webView.scrollView.addSubview(refreshControl)

10. Explore Private Framework with iPad Swift Playground App

Swift Playground app is the best way to explore iOS private APIs. You can browse iOS Private Headers for latest version from here..

This was the part 3 of Best iOS Development Tips and Tricks series. Here the part 1 and 2 of the same series.


Share Tweet Send
0 Comments
Loading...
You've successfully subscribed to Developer Insider
Great! Next, complete checkout for full access to Developer Insider
Welcome back! You've successfully signed in
Success! Your account is fully activated, you now have access to all content.