Work with Core Data in Swift Language

Lets take a brief idea about how to use core data with swift language.

Create a new project by check on Use Core Data.

Core Data Project Setting
Core Data Project Setting

It will create the basic methods of core data in to the AppDelegate.swift

There is core data model called {YOUR-PROJECT.xcdatamodeld} will be there with the project.

It will be look like as follows :

Core Data Model
Core Data Model

Read more

Use of constant (#define) in Swift Language – iOS

Swift Constant Test
Swift Constant Test

In Objective-C we are using a header file to create constant variables like as
[code lang=”obj-c”]
// Objective-C
#define APP_ALERT_TITLE "Objective-C Constant"
[/code]
Swift has new syntax to define the constant (#define)
[code lang=”obj-c”]
// Swift
let APP_ALERT_TITLE = "Swift Constants"
[/code]

Lets try

Create a swift file with the constants

[code lang=”obj-c”]
import Foundation
class Constants {
// MARK: List of Constants
static let APP_ALERT_TITLE = "Swift Constants"
static let SAMPLE_MESSAGE = "The alert is working !!"
}
[/code]
Note : Here the MARK statement is also changed.
[code lang=”obj-c”]
// Objective-C
#pragma mark –
#pragma mark List of Constants
[/code]
[code lang=”obj-c”]
// Swift
// MARK: List of Constants
[/code]

Read more

How to use Alamofire and SwiftyJSON with Swift? – Swift 3 – iOS 10 – Xcode 8

Swift JSON
Swift JSON

Alamofire is an HTTP networking library written in Swift.
SwiftyJSON makes it easy to deal with JSON data in Swift.

Read more

Create your own Slider menu (Drawer) in Swift

This article is updated with Swift 4 – Xcode 9 – iOS 11

Why to use a library everytime?

Slider Menu (Drawer)
Slider Menu (Drawer)

Let’s create our own Slide Menu (Drawer) in Swift 4.
1. Create New Project in Xcode 9 with Swift Language
2. Design the Menu in UIViewController
Menu UIViewController
Menu UIViewController

Declaration of Variables and Protocols (Delegate) :

Following method is for updating the Items in the Menu :

Following method is for click event and animation :


3. Now we will create a Base UIViewController to use anywhere in the project which control the delegate of menu.

First we will create this 3 lines Drawer Icon via Code
Preview_Slider_Drawer_ICon

Delegate (Protocol) method call :

To open a view controller by identifier :
Set the Restoration Identifier and Storyboard Identifier. If current view is open then we will not open it once again for that we have to check via Restoration Identifier.

4. Now We will assign this drawer to any of the UIViewController
We have to use only one method to add drawer (slide menu) self.addSlideMenuButton()


Source Code is available at the Github AKSwiftSlideMenu

GitHub AKSwiftSlideMenu Releases
GitHub AKSwiftSlideMenu Releases

You can download for versions of Swift 2, Swift 3 or Swift 4. From Releases Tab at GitHub AKSwiftSlideMenu.

I have uploaded a video for easy way to integrate AKSwiftSlideMenu in your project :


Check other blog posts about Swift

Happy Coding 🙂

How to work with IBDesignable and IBInspectable in Swift Language?

IBDesignable with Storyboard
IBDesignable with Storyboard

How to use the @IBDesignable and @IBInspectable?

By giving you a simple example that we can not change the corner radius from the design view. We have to change it run time. If you want to make possible it with design time then you can use the @IBDesignable and @IBInspectable.

So, We are taking an example as IBButtonExtender for this the functionality

  • Border Color
  • Border Width
  • Corner Radius

Create an Custom Class for UIButton with @IBDesignable

Create an @IBInspectable for Inspect the element. the Border Color property will be added to the list in the right panel when you create button with this class.

Set Initialization functions and other @IBInspectable as above.
You can check the code for the ButtonExtender.swift

IBButtonExtender

Presenting the ButtonExtender for Border Color, Border Width and Corner Radius function with the @IBDesignable and @IBInspectable in the Swift Language.

By making some corner radius and colors, You will get neat and clean output.

Output
Output

Github

Find IBButtonExtender on github 🙂
Check other articles on Swift Language.
All suggestions are acceptable. Put it in the comments!
Happy Coding 🙂

Realm – mobile database with Swift

What is Realm?
Realm is a mobile database
Realm is a replacement for SQLite & Core Data.
It can save you thousands of lines of code & weeks of work,
and lets you craft amazing new user experiences.

Download the Realm framework for swift from https://realm.io/
[code language=”obj-c”]
import UIKit
import RealmSwift
class Person: Object {
dynamic var name = ""
dynamic var age = 0
}
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Initializing the object
let personObj1 = Person()
personObj1.name = "Ashish"
personObj1.age = 25
println("name of person : \(personObj1.name)")
// Writing it to realm
let realm = Realm()
realm.write {
realm.add(personObj1)
}
let personObj2 = Person()
personObj2.name = "Darshak"
personObj2.age = 27
realm.write {
realm.add(personObj2)
}
// Quering
let r = Realm().objects(Person).filter("age > 24")
println(r)
// Queries are chainable
let r2 = r.filter("name contains ‘Ashish’")
println(r2)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
[/code]
Output :
[code language=”obj-c”]
name of person : Ashish
Results<Person> (
[0] Person {
name = Ashish;
age = 25;
},
[1] Person {
name = Darshak;
age = 27;
}
)
Results<Person> (
[0] Person {
name = Ashish;
age = 25;
}
)
[/code]
Happy Coding 🙂

Facebook Login – Swift Language – iOS 10 – Swift 3

Facebook Login using custom button in Swift Language
Xcode Version 8
Article is Updated with the Facebook 4.16 SDK [27-September-2016].

facebook-logo
Install Pods

Appdelegate.swift

ViewController.swift
Create touch up inside event for custom button like as follows

In iOS 10 don’t forget to set capabilities (Keychain Sharing) :
Go to your project targets -> Capabilities -> Keychain Sharing -> Toggle Switch ON

Capabilities - KeyChain Sharing
Capabilities – KeyChain Sharing

Detailed Output Log :

Happy Coding 😀

How to work with WatchKit in Swift Language – iOS 8 #Tutorial #CounterWithWatch

Apple Watch represents a new chapter in the relationship people have with technology. Now you can deliver innovative new experiences to your customers on their wrist. And by adding WatchKit, you can take your apps even further by extending and enhancing their functionality on Apple Watch.

Lets learn how to work with WatchKit by an Counter Example.

Counter Watch Example
Counter Watch

Create New Project in XCode with Swift Language
New Project
New Project

Add Apple Watch Target in your Application
Add Target
Add Target

Select Apple Watch Target
Add Target Apple Watch
Add Target Apple Watch

Select Options for Target
Target Options
Target Options

Storyborad of watch
By adding target you will find the InterfaceController.swift and Interface.storyboard
Storyboard And Swift Files
Storyboard And Swift Files

Design watch with the controls
Design and reposition the controls with the properties of controls.
Design Controls in Watch
Design Controls in Watch

Now Time to do some code and connect the outlets
Connect Outlets
Connect Outlets

Code:
[code language=”obj-c”]
// InterfaceController.swift
import WatchKit
import Foundation
class InterfaceController: WKInterfaceController {
@IBOutlet weak var lblCounter: WKInterfaceLabel!
var counter:Int = 0
override func awakeWithContext(context: AnyObject?) {
super.awakeWithContext(context)
// Configure interface objects here.
}
override func willActivate() {
// This method is called when watch view controller is about to be visible to user
super.willActivate()
}
override func didDeactivate() {
// This method is called when watch view controller is no longer visible
super.didDeactivate()
}
@IBAction func upButtonPressed() {
counter = counter + 1
lblCounter.setText("\(counter)")
}
@IBAction func downButtonPressed() {
counter = counter – 1
lblCounter.setText("\(counter)")
}
}
[/code]
Download project with this stuff AppleWatchCounterDemo.zip
Thanks!
Happy Coding 😀

Passing data with Unwind Segue in Swift Language – iOS 8

Unwind Segue Data
Unwind Segue Data

We have learn about Work with Unwind Segue in Swift Language – iOS 8 in first tutorial. Now we will learn how to pass the data with unwind segue.

It just simple.
Continue with the same example project.
Take data variables which you want to get in the parent view.
For example,
[code language=”obj-c”]
class ViewController2: UIViewController {
var data:String = "view 2 data"
….
}
[/code]

To get data from first view controller we have to get data from source view controller like follows :

[code language=”obj-c”]
@IBAction func unwindToVC(segue:UIStoryboardSegue) {
if(segue.sourceViewController .isKindOfClass(ViewController2))
{
var view2:ViewController2 = segue.sourceViewController as ViewController2
let alert = UIAlertView()
alert.title = "UnwindSegue Data"
alert.message = view2.data
alert.addButtonWithTitle("Ok")
alert.show()
}
if(segue.sourceViewController .isKindOfClass(ViewController3))
{
var view3:ViewController3 = segue.sourceViewController as ViewController3
let alert = UIAlertView()
alert.title = "UnwindSegue Data"
alert.message = view3.data
alert.addButtonWithTitle("Ok")
alert.show()
}
}
[/code]
Download project with this stuff UnwindSegueData.zip
Thanks!
Happy Coding 😀

Work with Unwind Segue in Swift Language – iOS 8

Unwind - Exit
Unwind – Exit

An unwind segue (sometimes called exit segue) can be used to navigate back through push, modal or popover segues (as if you popped the navigation item from the navigation bar, closed the popover or dismissed the modally presented view controller). On top of that you can actually unwind through not only one but a series of push/modal/popover segues, e.g. “go back” multiple steps in your navigation hierarchy with a single unwind action.

To enable the Unwind Segue you need to add some code first.
[code language=”obj-c”]
@IBAction func unwindToVC(segue: UIStoryboardSegue) {
}
[/code]
You have to add this code in the view controller where you want to unwind (came back).

Unwind To View
Unwind To View

Suppose you want to came back from second or third view to first view then you have to add the code in first view.
Then just connect any control(Which contains action) with the unwind segue.
Unwind Segue
Unwind Segue

For check the particular view controller by unwind segue, use following code :
[code language=”obj-c”]
@IBAction func unwindToVC(segue:UIStoryboardSegue) {
if(segue.sourceViewController .isKindOfClass(ViewController2))
{
let alert = UIAlertView()
alert.title = "UnwindSegue"
alert.message = "Unwind from view 2"
alert.addButtonWithTitle("Ok")
alert.show()
}
if(segue.sourceViewController .isKindOfClass(ViewController3))
{
let alert = UIAlertView()
alert.title = "UnwindSegue"
alert.message = "Unwind from view 3"
alert.addButtonWithTitle("Ok")
alert.show()
}
}
[/code]
Download project with this stuff UnwindSegue.zip
In next tutorial you will find the topic Passing data with Unwind Segue in Swift Language – iOS 8
Thanks!
Happy Coding 😀