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

Monday, 9 October 2017

Error while uploading app to Itunes Missing Marketing Icon - iOS Apps must include a 1024x1024px Marketing Icon in PNG format



Error while uploading app to Itunes Missing Marketing Icon - iOS Apps must include a 1024x1024px Marketing Icon in PNG format

Error while uploading app to Itunes, Missing Marketing Icon  - iOS Apps must include a 1024x1024px Marketing Icon in PNG format

As the tutorial heading reads out. In this tutorial we will learn how to remove this error and check whether the icon image  has alpha transparency enabled or it is without alpha transparency.
                                                          Apple Inc. recently changes there App developer Agreement and also the requirements that needs to be full-filled prior to uploading app to iTunes app store. If you are trying to upload iOS app from Xcode 9 then your app must include marketing app icon of size 1024*1024 pixels. You have to add this icon in your Assets.xcassets - App icon.

 IMPORTANT: Make sure to use the icon without Alpha/Transparency.

To add it follow the given below steps

Step 1: Select "Assets.xcassets".

Error while uploading app to Itunes Missing Marketing Icon - iOS Apps must include a 1024x1024px Marketing Icon in PNG format

Step 2: Select App Icon.

Step 3: Set your app icon  image of size 1024*1024 pixels to the box as shown in image below. Also make sure your app icon image does not have alpha.

Error while uploading app to Itunes Missing Marketing Icon - iOS Apps must include a 1024x1024px Marketing Icon in PNG format


How to check image alpha transparency 

If you are not sure how to check, whether your image has alpha or not, Then follow below steps

Step 1: Open your image with "Preview app."

How to check wether image has alpha transparency or not using preview app


Step 2: Make a duplicate copy of that image, as shown in image.

How to check wether image has alpha transparency or not using preview app


Step 3: Close the duplicate image window.

How to check wether image has alpha transparency or not using preview app

Step 4: If the pop up that came after closing duplicate window has alpha option then your image has alpha transparency. If no alpha option shown, then your image has no alpha transparency.

How to check wether image has alpha transparency or not using preview app

Where to go from here:

In this tutorial or blog post we learned, how to remove Missing Marketing Icon - iOS Apps must include a 1024x1024px Marketing Icon in PNG format, error while uploading ios app to iTunes store and how to check whether our icon image has transparency enabled or is without transparency as Apple clearly said that image should have without Alpha/Transparency.

Sunday, 24 September 2017

Integrate google map SDK for swift in iOS - Tutorial



How to create Google map for swift in iOS - Tutorial


Introduction:

In this tutorial we will learn how to integrate or add Google map SDK for swift in our iOS app project. Google map provides lots of options to the developer if the app want to show the location on map, navigation routes etc., because Google map is the most common map used by the people around the globe. So let start the tutorial.

Steps to integrate or add Google map SDK:

Step 1: Create a new single view application project and name it as "GoogleMap-Tutorial".

Note: If you are not logged in then please login to your Gmail or Google account.

Step 3: Click on "GET A KEY" button, a pop up will appear. Create a new project or use already existing project. After this click on "Enable API" button.

Get A key Google Map SDK iOS
Get A key Google Map SDK 

Create new project Google map SDK
Create new project to integrate Google map SDK
Step 4: You will get API key and copy it to safe location

Google map API key

Step 5: At this moment we have API key, now time to download SDK and install it into our project. Select "GUIDES" tab. 


Step 6:  There are two options to install the SDK, manually or using pods. We will prefer pods as it is more convenient way to upgrade to latest version of SDK. If you are not familiar with installation of pods, you can have a look at below link that explain how to install pods on mac.


Step 7: Open Terminal, and type command

Creating podfile for the project
Creating podfile for the project
The path above is the location of your project until "GoogleMap-Tutorial.xcodeproj".

Step 8: Create podfile using the command "touch podfile"
Coomand to create podfile xcode
Command to create podfile 
Step 9: Adding pod of Google map iOS SDK. In terminal open pofile using command "open -e podfile"

Opening podfile so taht we can write all the pods our application required to indatll
Opening podfile
Step 10: A podfile will be opened up in TextEdit and you can write pod to be installed in it and then save it. Then run the command "pod install". Shown in images below
Adding Google map SDK pod to our podfile
Editing podfile

Installing pods using command "pod install"
Installing pods using command "pod install"
 Step 11: After successful installation you will see message on terminal "Please close any current Xcode sessions and use `GoogleMap-Tutorial.xcworkspace` for this project from now on."

Step 12: Open ".xcworkspace" file. Don't use ".xcodeproj" file to open the project now onward.

Step 13: Open ViewController.swift and add following code to it.



Step 14: Check that you replaced the API key with your own API key, otherwise the map will not show on the screen. Run your app and you will see map is showing on the simulator/ device.

Google map showing on iPhone simulator

Where to go from here: 

In this tutorial you learned, how to configure Google map SDK in swift for iOS. In the future tutorials, we will cover how to add custom markers and draw poly lines / routes on Google map in swift. Hope the tutorial helped in setting up the Google map SDK for swift in iOS.
Download the source code from here