Answering the Top 10 Questions About iOS Development
Table of Contents

Answering the Top 10 Questions About iOS Development

Our iOS experts are here to answer the top 10 questions about iOS development on the internet. The post was contributed to us by Artur Rożek, Senior iOS Developer, with contributions from Stanisław Myszkowski, Senior iOS Developer, and additional help from Michał Skrzypek, Senior iOS Developer.

1. How Is Swift Better Than Objective-C?

Swift is a modern language created in 2014, while Objective-C is much more like C/C++ and was created in 1984. Objective-C lacks many features which are in Swift, such as optional types, modern structs and enums with associated values, tuples and even in the newest version (>=5.5) async features build right into the language syntax (see async/await docs here: https://docs.swift.org/swift-book/LanguageGuide/Concurrency.html)

2. What Is SwiftUI?

SwiftUI is a framework to use with Swift language to build user interfaces in the declarative manner and very fast as well. SwiftUI’s look and behavior depends on some state (or a set of states).

When using said framework, it is ideal to use the MVVM (Model-View-ViewModel) pattern where the ViewModel is an observable object, and the View just observes the ViewModel and changes its look depending on what is going on in the ViewModel. However, under the hood it is mostly the UIKit.

3. Is SwiftUI Production Ready?

If your project supports iOS 15 and newer versions, then the answer is yes. There are plenty of new features which helps develop rock solid applications. However, if your application must support iOS 14 or even iOS 13 then we would not recommend SwiftUI. Prior to iOS 15 there are plenty of SwiftUI features which are missing (modifiers such as .refreshable, .searchable, .badge, .interectiveDismissDisabled, .toolbar and so on, not to mention @FocusState, AsyncImage or SecureField).

There are 3rd party libraries that “hack” SwiftUI views by modifying the underlying UIKit components, such as https://github.com/siteline/SwiftUI-Introspect  

4. How Can I Check for an Active Internet Connection on iOS or macOS?

The most convenient way is to use an external library. One of our favorite ones is https://github.com/ashleymills/Reachability.swift which gives you the possibility to observe network status when it changes from online to offline and vice versa.

5. How to Download/Load an Image From URL?

You can load data from an URL using URLRequest/URLSession and then convert it to an image, but the better way is to use an external library which loads images asynchronously, such as KingFisher available under the link https://github.com/onevcat/Kingfisher or, if you support iOS 15 and above, you may use AsyncImage: https://developer.apple.com/documentation/SwiftUI/AsyncImage

6. How Can I Detect Any Text Changes in a UITextField?

The best way to do it is to assign a delegate to an UITextField instance (see delegate documentation here: https://developer.apple.com/documentation/uikit/uitextfielddelegate). Afterwards, you may use textField(_:shouldChangeCharactersIn:replacementString:) delegate method to detect text changes or listen to textDidChangeNotification notification (I would recommend the first method).

7. How Do I Programmatically Create Graphical Elements (like a UIButton) in Swift?

Like every other object in Swift. However, if you’d like such a view to be visible to the user you have to  

  • Add it to the parent view
  • Establish some constraints to that view within the parent view

Below is the code to create and make a button fill the parent view:

let button = UIButton() 
button.translatesAutoresizingMaskIntoConstraints = false

parentView.addSubview(button)  

NSLayoutConstraint.activate([   
  button.topAnchor.constraint(equalTo: parentView.topAnchor),
  button.bottomAnchor.constraint(equalTo: parentView.bottomAnchor),
  button.leadingAnchor.constraint(equalTo: parentView.leadingAnchor),
  button.trailingAnchor.constraint(equalTo: parentView.trailingAnchor) 
]) 

8. How to Convert an UIImage Into a Base64 String?

Assuming you have an image variable of type UIImage, then  

let base64EncodedString = image.pngData()?.base64EncodedString() 

Let value will be optional, so you may use guard to unwrap it. Please remember that the base64 string will contain PNG data. If you want to have a JPEG instead (with defined picture quality), then use:

let base64EncodedString = image.jpegData(compressionQuality: 80)?.base64EncodedString() 

9. How to Declare an Empty Dictionary in Swift?

For dictionary with keys and values of type String:

var emptyDictionary = [String:String]() 

Alternatively, you may use:

var emptyDictionary: [String:String] = [:] 

We would NOT recommend the syntax below:

var emptyDictionary: [String:String] = .init()  

10. How to Update to the Latest Version of CocoaPods

The simple way (although not the best)

Use an existing ruby installation shipped with your Mac. You will need to run the gem command with admin privileges, like so:

sudo gem update cocoapods # (when updating)

or

sudo gem install cocoapods # (when installing for the first time)

This approach will work if you’re working on a small project, and you don’t need a CI system.

The "not-so-simple" way

A better approach is to use a standalone ruby installation using either rbenv or rvm, so it doesn’t modify system ruby. Let’s focus on rbenv. For that, you will need to install Homebrew first (if you haven’t already), and then install rbenv:

brew install rbenv

When this step is complete, put the following in your ~/.zprofile (or ~/.bash_profile, if you’re using bash)

eval “$(rbenv init -)”

Then execute

rbenv install ruby-version 
# rbenv global   (alternatively, you can specify this using "rbenv shell"
# command or creating a .ruby-version file)

and

rbenv rehash 
source ~/.zprofile (or ~/.bash_profile)

You can then proceed with

gem install cocoapods # (or gem update cocoapods)

The Bundler way

This one comes in handy when you’re working on a larger project and want to make sure all ruby versions are consistent across dev machines and the CI system. It’s also a good idea if you have more ruby tools in your pipeline (such as fastlane, for instance)

Basic steps include setting up a separate ruby installation, like in the previous section. This time, we’re also going to create a `Gemfile` file that specifies all the gem versions.

This file could look like this:

source "https://rubygems.org" 

gem 'cocoapods', '1.11.3' 

gem 'fastlane', '>=2.205.0'

By running...

bundle install 

...the bundler will download all dependencies, just as you would be using plain gem install.

The advantage is that you only need to issue a single command.

In order to update a gem (for example, cocoapods), type

bundle update cocoapods

Then, if you want to run any of the bundler gems, you need to prefix the command with bundle exec, for example:

bundle exec pod install # (will install CocoaPods dependencies, just as running pod install would)

Just like installing it for the first time. Go to the terminal and just type:

sudo gem install cocoapods

That requires Ruby to be present on a Mac, though you don’t have to install Ruby separately. Ruby is already preinstalled on your Mac, though remember to use sudo.

Liked the article? subscribe to updates!
360° IT Check is a weekly publication where we bring you the latest and greatest in the world of tech. We cover topics like emerging technologies & frameworks, news about innovative startups, and other topics which affect the world of tech directly or indirectly.

Like what you’re reading? Make sure to subscribe to our weekly newsletter!
Relevant Expertise:
No items found.
Share

Join 17,850 tech enthusiasts for your weekly dose of tech news

By filling in the above fields and clicking “Subscribe”, you agree to the processing by ITMAGINATION of your personal data contained in the above form for the purposes of sending you messages in the form of newsletter subscription, in accordance with our Privacy Policy.
Thank you! Your submission has been received!
We will send you at most one email per week with our latest tech news and insights.

In the meantime, feel free to explore this page or our Resources page for eBooks, technical guides, GitHub Demos, and more!
Oops! Something went wrong while submitting the form.

Related articles

Our Partners & Certifications
Microsoft Gold Partner Certification 2021 for ITMAGINATION
ISO-IEC 27001:2013 ITMAGINATION ISO 9001 ITMAGINATION
© 2024 ITMAGINATION, A Virtusa Company. All Rights Reserved.