I have not been too fond of Objective-C, which was the primary reason for me to stay away from making iOS apps till now. So what changed? Well Apple has done something very interesting recently and that is the introduction of a new programming language i.e. Swift. Swift is awesome, it almost feels like Python, C++ and Objective-C had a baby with some of their good parts in them. So I have been getting to know Swift and it is an awesome language to program in. What I am going to share with this and a series of blog posts are solutions to some problems that i have encounter while i am trying to finish my first iOS app. The one hurdle that I have encountered while getting started on developing an iOS app is that a majority of the solutions for iOS specific problems provide solutions to them using Objective-C. Which is fair, because Swift has not been around for that long. Anyway let us get started with a few basics,

Getting started with Swift

I would highly recommend having a read of this book i.e.  the free book titled Swift Programming Language by Apple.

String and Numbers

var num:String = "123"; //create a string
var val  = num.toInt()!; //convert it to an Int
var value:String = "Concat:(val)"; ///concat a string with an Integer

Working with WebViews

Say we want to show some HTML content in our app, well one way to do that in an iOS app is by using webViews i.e. UIWebView.  How do we do that? ok lets look at how we can achieve this. When you start a create a new project called HelloWorld, in your project navigator you will see a tree-like structure that shows the linked libraries (if you have any?) followed by 3 folders which will be something like HelloWorld, HelloWorldTests and Products. Now if you have an HTML file called index.html, that you want to display in your app, follow the following steps to do so.
  1. Add a WebView object to your storyboard.
  2. Add a reference outlet for that WebView to your ViewController and give it a name,  say webView. If you do not know how to add a reference outlet to your ViewController, you will find this video very helpful.
  3. Add your index.html file to your project by right-clicking on the HelloWorld folder in your project navigator and selecting Add Files to HelloWorld. 
Next up is displaying the contents of index.html into your WebView, that can be achieved with the following code.
//get the path to the index.html file
var path = NSBundle.mainBundle().pathForResource("index", ofType: "html");
let url = NSURL(string: path!); //initialise an NSURL with the path to the index.html file
let request = NSURLRequest(URL: url); //add the url in a request
webView.loadRequest(request); //have the UIWebView load the request
This post sheds some more light into dealing with some additional features in iOS 8 for working with HTML content.

How to take in a string and convert it to NSDate

Say we receive a date in a String format from the webView and we want to convert that to a date in Swift. How do we do that? Use the NSDateFormatter and set the NSDateFormatter.dateFormat to a particular type of date format.
e.g.
let start = "1996.07.10 16:08:56"
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat="yyyy.MM.dd HH:mm:ss";
let startDate:NSDate = dateFormatter.dateFromString(start)!

Other useful tutorials

In addition to this, I have written several iOS tutorials with useful code samples.

Building a native iOS Currency converter app

In this post, I will talk about my approach when building a new iOS Currency Converter. I focus on how I start by defining a simple user story, understand what the user story is followed by how I can accomplish all that technically. This post focuses on the requirements specification to build an iOS mobile app. The concepts discussed here apply to both all mobile apps, this document is focused on iOS Currency Converter. The rest of this document is structured as follows
  • iOS Currency Converter app requirements as mentioned in the assessment document
  • Define the user story for the app
  • A description of what the app will do
  • Technical requirements i.e. what libraries or frameworks can be used to achieve this
  • UI design samples for the minimal UI
  • Some backend architecture design
    • Decisions to adopt this style
  • Lastly, Questions i.e. if any aspect of building this is not clear
You can read more in the link below 

Building a native iOS Currency Converter app - My Day To-Do - Bhuman (mydaytodo.com)

Tutorial on animating rows in UITableView

This post is a tutorial on how to add, delete and reload rows in UITableView with animation. Shown below is some code that I wrote for Xcode playground. This code programmatically constructs a UIButton, UITableView, adds them to a view and styles them using auto layout constraints. Then, it randomly generates a number between 1 and 3 and shows how to either,

  1. Animate inserting a new row (UITableViewCell) into a UITableView
  2. How to delete and animate removal of a row (UITableViewCell) from a UITableView
  3. How to animate reloading a section of a UITableViewRow

The code also has the necessary comments that explains some of the things. You can read the full post below.

Xcode Playground to add, delete & reload UITableView rows with animation (How-To) – My Day To-Do (mydaytodo.com)

Build your first iOS app: A Tip Calculator

Ignoring the COVID-19 world we live in right now, going out to eat is something we all love and while we don’t mind tipping the wait staff, we often don’t understand the tip calculation. Hence, I thought that helping you make a tip calculator would be the best way to get you started on making your first iOS app. Don’t worry if you haven’t built anything in iOS prior to this, with the right instructions, it’s not hard. If I can do it, so can you. The rest of this article is structured as follows,
  1. We will talk a little bit about what the app is
  2. Talk about the starter project and Xcode environment
  3. Discuss some storyboard concepts and write some code
  4. Finally, conclude with what you can do next after building this app
You can read the rest of the post in the comment below. 

Building your first iOS app - Tip Calculator - My Day To-Do (mydaytodo.com) 

Get updates?

If you find any of my posts useful and want to support me, you can buy me a coffee :) https://www.buymeacoffee.com/bhumansoni Or you can  buying or even try one of my apps on the App Store.  https://mydaytodo.com/apps/

Conclusion

Anyway, hope you found this post useful and if you like you can look at my other iOS posts on my blog. The explore other aspects of iOS such as UI programming, Xcode playgrounds and all.

Get updates?

As usual, if you find any of my posts useful support me by  buying or even trying one of my apps on the App Store.  https://mydaytodo.com/apps/
Categories: iOSSwift

0 Comments

Leave a Reply

Avatar placeholder
Verified by MonsterInsights