Tuesday, 12 September 2017

Add event to device calendar in iOS using Swift

Add event to device calendar in iOS using Swift

Introduction to Event-Kit framework:

 EventKit framework comes with Xcode and the EventKit framework provides classes for accessing and manipulating calendar events and reminders. Thus, we can add event to device calendar using classes provide din EventKit framework. In this tutorial we will learn how to create event in device calendar of iPhone or device calendar in iOS using swift language.

Looking for objective C version then please visit Add event to device calendar - Objective C

Steps for creating event to device calendar

Step 1: Add EventKit framework under Linked Frameworks and libraries

Step 2: Create Event store object

Step 3: In info.plist file add privacy policy key for calendar usage, as shown in below image other wise your app will crash as soon as you want to access device calendar

Add privacy policy key for accessing device calendar in info.plist

Step 4:  Create a function having three parameter's

1) Event Title - This is the title of the event we want to show in device calendar.
2) Event Start date - This is the event start date.
3) Event end date - This is the event end date.

 Inside the function we, will first request for access to device calendar. On successful access to device calendar, we will use EKEvent class to create and event on device calendar and EKAlarm class to set an alarm or reminder for the event.

Where to go from here:

In this tutorial we learned about how to create event to device calendar in iOS using EventKit framework.  For more tutorial on swift, please stay tune. If any problem arises then please ask your questions via commenting, will try to respond to them as soon as possibly. Happy coding :)

Wednesday, 6 September 2017

Get thumbnail from video url in swift

Get thumbnail from video url in swift  - Tutorial

Get thumbnail from video url in swift :

                                           Generating thumbnail or preview image from video URl in your iOS app seems to be a tough task. In this tutorial we will look at the code snippet to create or generate thumbnail from video url in swift. If you are looking for objective C  code then please go to this link

Swift code to generate thumbnail from video URL:

Below is the code that will generate thumbnail from video URl in swift language. 

Where to go from here:

In this tutorial you learned how to create or generate thumbnail from video url in swift. The code is fully testified at the time of tutorial being written. If any one found issue is code then please post it as comment. We will try to fix it as soon as we can. Thanks for reading. Happy coding

Sunday, 6 August 2017

Update UItableViewCell subviews tag after deleting a row from UITableView in swift3 iOS tutorial

Update UItableViewCell subviews tag after deleting a row from UITableView in swift3 iOS tutorial

Update tags of UItableViewCell without reloading whole UITableView:

In iOS app development, UITableView is most common control used in all apps. While creating list in iOS apps, we use UITableView, and if we want to delete a row from that UITableView when user tap on a button e.g. a delete button on the row than at this time we have below options to full-fill our objective.

1) remove the index based on our button tag from the data-source array
2) After removing the record, reload UITableView
3) use UItableView function deleteRows, reloadRows to load only that particular row

Now, first option is the necessary step, we always need to perform if we want to change or delete an item or row from UITableView. The main limitation of reloading whole UITableView, here i ma talking about the 2nd option in order to update button tags is not a good approach as if we have 100's of records in our UITableView than they all will get reloaded. With 3rd option we will remove the limitation we encountered in option  2nd, but it will only update the tag of row that gets reloaded again, thus left us with all old tags for button intact in other rows of UITableView.

Solution for update tag after deleting row form UITableView:

The only solution is that to get the tag when we called our button action,  rather than giving the tag to button at cellForRowatIndexPath method. One can get the tag by using the below code.

In above code we get the point of our sender, and from that point we get the row index path.

Where to go from here:

In this tutorial, we learned how to update button tag displayed on UITableView cell while deleting or adding a row in the UITableView rather than reloading whole UITableView,

Sunday, 16 July 2017

Create UIProgressView in swift

UIProgressView Swift Tutorial

Introduction to UIProgressView:

                 UIProgressView is the control used in iOS apps to show the progress of a particular task of the app and how much it's remain in order to complete the task. One can think of an example, like we when we install a particular software on desktop. During installation process we see a progress bar that tells how much the software is pending. In the same when we have to show progress of a particular task in iOS app we can use or we will use UIProgressView to full-fill our need. In this tutorial, you will learn how to create UIProgressView in swift.

In this tutorial we will, only covered basic functionality of UIProgressView in swift. Some of the important properties of UIProgressView are 
  1. trackTintColor  = Change color of UIProgressView track i.e. unfilled part of UIprogressView
  2. progressTintColor = Change color of UIProgressView progress o.e. filled part of UIProgressView
  3. trackImage = An image to use for the portion of the track that is not filled.
  4. progressImage =  An image to use for the portion of the progress bar that is filled.

Steps to create UIProgressView in swift:

Step 1: Create a new Xcode project, select template "Single View Application". Give product name "UIProgressViewTutorialSwift", set language to "Swift". Same is shown in the picture below

Creating new project - UIProgressview tutorial in swift

Step2: Open "Main.storyboard", drag "UIProgressView" control from object library to ViewController's view.

Selecting UIProgressView control from object library in Main.storyboard

Step3: Add constraints to the UIProgressView as shown in below picture.

Adding constraints to UIProgressView that we dragged to View of UIViewController

 After adding constraints, check for them that they were added correctly as shown in the image below

Added constraints to UIProgressView
Step4: Open "ViewController.swift" and add given below code to as shown

In above code, we created IBOutlet for UIProgressView, so that we can connect it in "Main.storyboard" and created a variable "progressValue"(this variable will get increased after 0.2 seconds). Then we created a function "updateProgress" that will increase the progress of UIProgressView  as per "progressValue". The function "updateProgress"will continuously gets called until our "progressValue" reach at 1.0. UIProgressView has minimum value = 0.0 and maximum value = 1.0

Step5: Open Main.storyboard and connect IBOultlet to UIProgressView as shown below

Showing all IBOutlet we created in ViewController.swift

Connecting IBOutlet to UIProgressView

Step6: Run the app and you will see the UIProgressView will gets updated and blue bar fills up grey one.

Where to go from here:

 In this tutorial, we learn how to create and use UIProgressView in swift. We only covered basic functionality of UIProgressView. Hope you enjoyed the tutorial. Stay tuned for more tutorial on swift.
Get source code from UIProgressViewTutorialSwift.zip

Monday, 3 July 2017

Programmatically get height of keyboard in swift 3 language in iOS app - Tutorial

Get keyboard height in swift3 tutorial with example

Introduction to the tutorial programmatically get height of keyboard in swift 3 language in iOS app 

With the introduction of different size of iPhone devices,  getting keyboard height is a pain for iOS developers. With the addition of suggestion toolbar above keyboard in iOS, it's necessary for iOS developers to get exact height of the keyboard so that they can meet the app requirements where they have to show button above keyboard when user is typing on text field or other input field. 

                               iOS sends notifications each time keyboard comes up or goes off from screen. We, iOs developers can take advantage of this notification. Below are the codes that gives keyboard height in swift 3 

Adding notification observers for  UIKeyboardWillShowUIKeyboardWillHide notifications

First we have to add notification observers to our class where we want to get keyboard height as soon as user start editing an input field. You can add them in viewWillAppear()

Adding selectors/methods definition for the notification observers added 

Remove observers 

You can remove observers in viewWillDisappear(). 

Monday, 19 June 2017

Convert Date to time ago in swift 3 - Tutorial

Convert Date to time ago  in swift3 - Tutorial

Introduction to tutorial on Convert Date to time ago in swift 3

I in this tutorial I am going to share a short written extension of Date class that will convert given date and time with respect to current date time into seconds ago, minutes ago, days ago, and so on. If we are creating a feed based app or app showing listing of posts, then we have requirement to show time elapsed from the time of feed/post posting to the current time of the device.

Extension for Date that convert given date to time ago

Below is the code for the extension

Step 2: In  ViewController.swift file, viewDidLoad function, we will use it as shown below. We are passing -180 to create date 3 minutes behind current date.

Monday, 5 June 2017

Create dynamic height UITableViewCell in Swift- tutorial

Create dynamic height UITableViewCell in Swift- tutorial

Introduction to create dynamic height UITableViewCell in Swift

In this tutorial, we will learn create dynamic height UITableViewCell in Swift or how to create multi line UILabel in UITableView using swift language.
                                     We will use the new UITableViewAutomaticDimension property. UITableViewAutomaticDimension property is a handy thing for iOS developers as it sets the cell height automatically, only condition is that you set up your cell constraints correctly. So let us jump to the tutorial and learn how can we create create dynamic height UITableViewCell or multi line UILabel in UITableView using swift.

Steps to create the multi line UILable in UITableViewCell of the UITableView

Step 1: Create a signle view application and name it "Multi-Line-UILable". After creating the project you will see following files contained by the project (i am mentioning only 3 files here) 
  1. AppDelegate.swift
  2. ViewController.swift
  3. Main.Storyboard

Step 2:  Open ViewController.swift and create an IBOutLet for UITableView. 

Step 3: Create a new file which is a base class of UITableViewCell class.  

Creating MultilineLabel UItableViewCell class

Step 4: Open MultilineLabelCell.xib and follow the steps as shown in below image sequence. Here constraints will play important role in determining  UITableViewCell height, because Autolayout always consider the constraint during rendering of the content. In our case the cell will always  have 11.5 pixel space from bottom of the cell. You can change constant value of bottom constraint to see the different scenarios and also leading, trailing, top constraint value's, so that layout will look more attractive.

Step 5: Open MultilineLabelCell.swift and create IBOulet for the UILabel, we added to the UITableViewCell class.

Step 6: Open MultilineLabelCell.xib and connect IBOutlet lblTitle to UILabel.

Step 7: Open ViewController.swift and add below code.

In the above code we created IBOutlet for UITableView, named it as tblList and set delegate and datasource for our tblList UITbaleView. Here we need to tell the default estimated row height so that it improved performance as explained by APPLE. Also telling our ViewController.swif class that we are going to implement delegate and datasource for UITableView.
                                                             As we are using custom class so it's also good place to register custom cell xib name here. By now you are seeing error in your xcode, this is because we haven't yet implemented the delagets and datasource for uITableView.

Step 8: Below is the complete code that will produce dynamic cell height for UITableView row, as per the content displayed by UILabel.

Run your code and you will see, cell of dynamic height based on content length.