Tuesday, 9 January 2018

How to create csv file in swift programmatically - Tutorial



Introduction to how to create CSV file in swift programmatically - Tutorial

As the heading of this tutorial says, we are going to learn how to create CSV(comma separated values) file in swift programmatically. CSV(comma separated values) or excel files as they are commonly known are not a difficult task in iOS app development using swift. In this post, we will learn the code to create CSV(comma separated values) file in swift programmatically and save it to document directory. If you are looking for objective c version then please follow the given below link 


Jump to code

Step 1: Create an array, named as "employeeArray" which will store all our records for the employees as key value objects. Also we will add dummy data to the newly created array


Step 2: Now we have data with us, and its time to create CSV(comma separated values) file using swift programmatically. For this we will loop through our records in "employeeArray" and append them in a string. Then we will write this string to our document directory of the app. All the stuff goes in different function named as "createCSV", below is the code for the same


Step 3: Finally we will call our function from "viewDidLoad". Below is the complete code


Note: Please remove "comma" for your data in order to create accurate CSV file.

Where to go from here:

In this post, we learned how to create CSV file in swift programmatically. If you have any query then please free to ask using our comment section and we will respond back to you as soon as we can. Thank you :)

Tuesday, 26 December 2017

Create UITabBarController Programmatically in swift (swift 4 + xcode 9.1)



 Tutorial how to create UITabBarController Programatically in swift


Introduction to the tutorial how to create UITabBarController Programatically in swift


UITabBarController is the main controller used in ios app development. You can take the example of shopping apps or workouts app which contains the bottom tab, those app uses UITabBarController. In this tutorial, we will learn how to create UITabBarController programmatically in swift language (swift 4). Follow below steps to create UITabBarController Programmatically in swift.

Step 1: Create a new project using "SingleView App" template. We are not going to use "Tabbed App" template for our tutorial. We will name our project as "UITabBarController-Tutorial". Move to step 2 after creating your project.

Create UITabBarController Programatically in swift (swift 4 + xcode 9.1)


Step 2: Open "ViewController.swift" file. We will create function that will create TabBarController for us. We will named it "createTabBarController". Inside our function we will create an instance of UITabBarController and add it to our view.


Step 3: Call the function created in step 2, inside our viewDidLoad function.


Step 4: At this moment if you run your code, then you will see something like this

UITabBarController- programattically tutorial in swift

It's not showing any tabs, as we haven't added any viewcontrollers to it yet.

Step 5: Let us now add Tabs and ViewControllers to our UITabBarController. We will create two viewcontroller named as, "FirstVc" and "SecondVc". Below is the code of updated "createTabBarController" function.


Step 6: If you, run your code now. You will see two tabs with title "First"  and "Second". These are the titles we gave to  our two viewcontrollers.

UITabBarController- programattically tutorial in swift

Step 7: Here we are done with creating UITabBarController programmatically in swift (swift 4). But still we need to add images to our tabs. For this we can use UITabBarItem. Below is the  updated code of "createTabBarController" function. Here we set tabbaritem to our ViewControllers and also added images to them.


Step 8: Below is the app, how it looks after step 7 after running it successfully.

UItabBarController programmatically in swift - tutorial


Step 9: In order to add navigation to your view-controllers we need to add UINavigationController too. For this we will change the line where we are assigning array of ViewControllers to UITabBarController with the below code.


Step 10: Now if you run your app, you will find your tab's view background color are changed. But wait here is one issue as tabs are changing but same background color is appearing. For this error, and to resolve it please initialize  UITabBarController as global instance for the class. You can check it here in below code for whole class


Where to go from here:

In this post we learned, how to create UITabBarController programmatically in swift (swift 4). Also we add images to our UITabBarController using UITabBarItem and used UINavigationController within each tab.

Source code: UITabBarController-Tutorial.zip

Sunday, 12 November 2017

Multiple component UIPickerView in swift 4


Multiple component UIPickerView in swift 4 - tutorial with sample code

Multiple component UIPickerView in swift

As the title of this post says, Multiple component UIPickerView in swift. So in this tutorial, we are going to learn, how to create multiple component or multi component UIPickerView in swift. Generally a UIPickerView contains only single component, it is the default behavior of UIPickerView, but sometimes during app development we required two components or two lists to be displayed inside single UIPicker. In this tutorial we are going to covered the same thing. So let us start

Step 1: Create a new xcode project by selecting "Single View  App" template and name it "Multicomponent UIPickerView". Note: Please select development language as swift.

Select Singleview app template in xcode 9


Step 2: Open "Main.storyboard" file.

Select Main.storyboard file


Step 3: Drag an UIPickerView object  from object library and an UILabel, so that we can display our output of the picker selection.

Drag and drop UIPickerView and UIlabel on to ViewController from object library


Step 4: Add constraints to UIPickerView and UILabel as shown in the image.

Constraints given to UILabel

Constraints given to UIPickerView


Step 5: Open "ViewController.swift" file and first declare our IBOutlet's to both the controls, dragged in step 3.


Step 6: Open "Main.storyboard" and connect both  IBOutlet's with the respective objects.
Connect IBOutlet with the UIPickerView and UILabel


Step 7: In order to populate UIpickerView with our desired values or options we will take two arrays. With these arrays we will populate our UIPickerView.


Step 8: As we are done with are arrays, its time to write down UIPickerViewDataSource and UIPickerViewDelegate methods.

Step 9: If you run the code, you will see the following output.
If you run the code till now you will see output like this


On changing values, you will notice that label value will not be updated. So let us fix this problem.

Step 10: To detect a picker selection, we need to implement another delegate method named as "didSelectRow". Below is the code 


Step 11: Run the code and you will see whenever you change  UIPickerView values, label will get updated.

Complete code: 

Where to go from here:

In this tutorial, we learned how to create and use multiple component UIPickerView or multi component picker in swift language (swift3 / swift4).

Download source code from Multicomponent-UIPickerView.zip

If you any questions or doubts then please feel free to post your doubts in the comment section and will try to answer as soon as we can. Thank you

Tuesday, 31 October 2017

Convert Date in local timezone to UTC in Swift - Tutorial

Convert Date in local timezone to UTC in Swift - Tutorial

Convert date in local timezone to UTC:

In this tutorial, we will learn how to convert a date in local timezone to date in UTC timezone in swift. We will pass the date to the function (we will create it in this tutorial) and it will return the date converted to UTC timezone. So let us start the tutorial and learn how to convert a date to UTC date in swift.

Step1: Create a function named "convertToUTC", which will accept a string parameter and that is the date we want to convert into UTC timezone and returns the converted date in UTC timezone as a string.


Step2: We will create a "DateFormatter" that has a date format "dd-MM-yyyy hh:mm a". You can change this date format as per your requirements.

Step3: Finally, we convert the local timezone date (passed as string object), to Date object and then set our "DateFormatter", time zone to "UTC". Lastly, convert the Date object to string object and return from the function.

Step 4: Now call our function and printing the output in console. Here, we need to pass date in exact date format as set to our "DateFormatter" object otherwise the Date object will comes as nil.

Step 5: Run the code, and you will see the output in your console. My device timezone is IST i.e UTC + 5:30 , so i get the output as

Converted Date == 16-09-2017 05:30 AM

Final conclusion: In this tutorial, you learned how to convert date in device local timezone to UTC timezone in swift language. You can also download the source code from the link ConvertDateToUTC-Tutorial.zip

Thursday, 26 October 2017

Compare two dates using swift in ios



In so many of our app development, we need to compare two dates and then check whether both dates are equal or either small or greater then other date. In iOS, Apple provide compare function to compare to Date objects. In this tutorial i will share the code that shows how to compare two Dates using swift in iOS.

Here we will create two Date objects using DateFormatter and then use compare function of Date class.

Case 1: Both dates are equal




Case 2: First date is smaller then the second date



Case 3: First date is greater then the second date




Final notes:
In this tutorial, you learned how to compare two dates using swift in iOS. If you have any queries or suggestion, please feel free to comment.

Tuesday, 17 October 2017

How to solve pod update fails with RuntimeError - [Xcodeproj] Unknown object version (macOS Sierra and xcode 9.0)


Solve pod update fails with RuntimeError - [Xcodeproj] Unknown object version (macOS Sierra and xcode 9.0)

Solve pod update fails with RuntimeError - [Xcodeproj] Unknown object version (macOS Sierra and xcode 9.0)

As per the heading, in this tutorial we will learn how to solve pod install or pod update error that it fails with run-time error - [Xcodeproj] unknown object version. Also we will, solve another error 
operation not permitted - /usr/bin/pod cocoa-pods. So basically we will solve the below two errors

1) pod install or pod update fails with run-time error - [Xcodeproj] unknown object version

2) operation not permitted - /usr/bin/pod cocoa-pods

Below are the steps to solve the above listed two errors

Step 1: Open terminal and type command   
sudo gem install cocoapods

How to solve pod update fails with RuntimeError - [Xcodeproj] Unknown object version (macOS Sierra and xcode 9.0)
Step 2: It will ask for password, enter your system password.
Step 3: If you get error as shown in the picture below then see step 4.
How to solve pod update fails with RuntimeError - [Xcodeproj] Unknown object version (macOS Sierra and xcode 9.0)
Step 4:  Run the command 

sudo gem install -n /usr/local/bin cocoapods

operation not permitted - /usr/bin/pod cocoapods

Step 5:  Your pods were successfully installed and now you can install pods for your xcode project.

How to solve pod update fails with RuntimeError - [Xcodeproj] Unknown object version (macOS Sierra and xcode 9.0)

Where to go from here

In this tutorial we  learned how to solve pod install or pod update error that it fails with run-time error - [Xcodeproj] unknown object version and second error operation not permitted - /usr/bin/pod cocoa-pods 
Note:
Video tutorial  is also available. You can watch the video tutorial Youtube video link

Sunday, 15 October 2017

Create tag clouds view in swift - Tutorial



Create tag clouds view in iOS


Introduction to tutorial, how to create tag clouds view in swift:

                                       In this tutorial, we will learn how to create tag clouds view in swift. Tag clouds view, we  create will look like as show in the image view.

Cloud tags view in iOS - programmatically using swift
When user taps on cross icon we, will remove the tag and all other tags will be re-adjusted according to the width. 

Steps to create tag clouds view in swift:

Step 1: Create a new single view application and name it "TagsCloud". 

Step 2: Open "ViewController.swift", and create an IBOutlet for UITextField so that we can add a new cloud tag. Secondly, we will declare an array named "tagsArray", who will hold all our tags.


Step 3: Open Main.storyboard file and create user interface that contains one UITextField and a UIButton, as shown in the given below images, you can create your own layout too.

Intial Layout of the screen to add tags
Initial layout of our screen

Constraints used for UIButton
Constraints used for UIButton



Constraints used for UITextField
Constraints used for UITextField


 Step 4: Connect IBOutlet "txtInput" to UITextField.

Step 5: Open ViewController.swift and creates an IBAction for our UIButton.


Step 6: Open "Main.Storyboard", and connect IBAction "addTag" to UITouchUpInside event of the
UIButton.

Step 7: Next we will write definition  for recently created IBAction "addTag", here if our UITextField "txtInput" is not empty then we will add that entry to our array and call our method that we create tags cloud.


Step 8: At this time if runs your app, and press "Add Tag", button the app will gets crashed as we haven't implemented out function that will create clod tags. Copy the below code and add it to your file.



In the above function we will first remove all the sub-views whose tag is not equals to zero. Then we will get width for tags text, the add the spacing and button width as per the designs with respect to tag view. IF our tag is going outside the screen bounds then we will change x position and y position so that it will adjust to new line.
                                            Then we draw, all our elements required to create tag with cross icon and add selector to cross button so that we can remove it.

Step 9 : Last step, we are using String extension that will return string width based upon the font  that our tag is using.


Where to go from here:

     In this tutorial, you learned how to create cloud tags view in ios using swift. If you type anything in the UITextField and press "Add Tag" button. It will create a new tag. When you tap on cross icon the tags will be automatically adjusted in the cloud. If you feel any issue then please feel free to comment. You can download the source code from the given link