Wednesday 5 April 2017

Audio : AVAudioPlayer



STEP-1 : Create project and add a mp3 file(song.mp3) to project.



STEP - 2 : Add five button and give name according to below image



STEP-3 :

ViewController.swift


//
//  ViewController.swift
//  AudioPlayer1
//
//  Created by Divakar Singh on 4/5/17.
//  Copyright © 2017 Divakar Singh. All rights reserved.
//

import UIKit
import AVFoundation

class ViewController: UIViewController {
    var player : AVAudioPlayer = AVAudioPlayer()
    
    @IBAction func play(_ sender: Any) {
        player.play()
    }
    @IBAction func pause(_ sender: Any) {
        player.pause()
    }
    @IBAction func replay(_ sender: Any) {
        player.currentTime = 0
    }
    @IBAction func stop(_ sender: Any) {
        player.stop()
    }
    @IBAction func playtime(_ sender: Any) {
        player.play(atTime: player.deviceCurrentTime+3.0)
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        do{
            
            let audioPath = Bundle.main.path(forResource: "song", ofType: "mp3")
            try player = AVAudioPlayer(contentsOf: NSURL(fileURLWithPath: audioPath!) as URL)
            
        }catch
        {
            // Error
        }
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}








22

Json : Getting Specific Json Object



JSON Data

{
    "contacts": [
        {
                "id": "c200",
                "name": "Ravi Tamada",
                "email": "ravi@gmail.com",
                "address": "xx-xx-xxxx,x - street, x - country",
                "gender" : "male",
                "phone": {
                    "mobile": "+91 0000000000",
                    "home": "00 000000",
                    "office": "00 000000"
                }
        },
        {
                "id": "c201",
                "name": "Johnny Depp",
                "email": "johnny_depp@gmail.com",
                "address": "xx-xx-xxxx,x - street, x - country",
                "gender" : "male",
                "phone": {
                    "mobile": "+91 0000000000",
                    "home": "00 000000",
                    "office": "00 000000"
                }
        },
        {
                "id": "c202",
                "name": "Leonardo Dicaprio",
                "email": "leonardo_dicaprio@gmail.com",
                "address": "xx-xx-xxxx,x - street, x - country",
                "gender" : "male",
                "phone": {
                    "mobile": "+91 0000000000",
                    "home": "00 000000",
                    "office": "00 000000"
                }
        },
        {
                "id": "c203",
                "name": "John Wayne",
                "email": "john_wayne@gmail.com",
                "address": "xx-xx-xxxx,x - street, x - country",
                "gender" : "male",
                "phone": {
                    "mobile": "+91 0000000000",
                    "home": "00 000000",
                    "office": "00 000000"
                }
        },
        {
                "id": "c204",
                "name": "Angelina Jolie",
                "email": "angelina_jolie@gmail.com",
                "address": "xx-xx-xxxx,x - street, x - country",
                "gender" : "female",
                "phone": {
                    "mobile": "+91 0000000000",
                    "home": "00 000000",
                    "office": "00 000000"
                }
        },
        {
                "id": "c205",
                "name": "Dido",
                "email": "dido@gmail.com",
                "address": "xx-xx-xxxx,x - street, x - country",
                "gender" : "female",
                "phone": {
                    "mobile": "+91 0000000000",
                    "home": "00 000000",
                    "office": "00 000000"
                }
        },
        {
                "id": "c206",
                "name": "Adele",
                "email": "adele@gmail.com",
                "address": "xx-xx-xxxx,x - street, x - country",
                "gender" : "female",
                "phone": {
                    "mobile": "+91 0000000000",
                    "home": "00 000000",
                    "office": "00 000000"
                }
        },
        {
                "id": "c207",
                "name": "Hugh Jackman",
                "email": "hugh_jackman@gmail.com",
                "address": "xx-xx-xxxx,x - street, x - country",
                "gender" : "male",
                "phone": {
                    "mobile": "+91 0000000000",
                    "home": "00 000000",
                    "office": "00 000000"
                }
        },
        {
                "id": "c208",
                "name": "Will Smith",
                "email": "will_smith@gmail.com",
                "address": "xx-xx-xxxx,x - street, x - country",
                "gender" : "male",
                "phone": {
                    "mobile": "+91 0000000000",
                    "home": "00 000000",
                    "office": "00 000000"
                }
        },
        {
                "id": "c209",
                "name": "Clint Eastwood",
                "email": "clint_eastwood@gmail.com",
                "address": "xx-xx-xxxx,x - street, x - country",
                "gender" : "male",
                "phone": {
                    "mobile": "+91 0000000000",
                    "home": "00 000000",
                    "office": "00 000000"
                }
        },
        {
                "id": "c2010",
                "name": "Barack Obama",
                "email": "barack_obama@gmail.com",
                "address": "xx-xx-xxxx,x - street, x - country",
                "gender" : "male",
                "phone": {
                    "mobile": "+91 0000000000",
                    "home": "00 000000",
                    "office": "00 000000"
                }
        },
        {
                "id": "c2011",
                "name": "Kate Winslet",
                "email": "kate_winslet@gmail.com",
                "address": "xx-xx-xxxx,x - street, x - country",
                "gender" : "female",
                "phone": {
                    "mobile": "+91 0000000000",
                    "home": "00 000000",
                    "office": "00 000000"
                }
        },
        {
                "id": "c2012",
                "name": "Eminem",
                "email": "eminem@gmail.com",
                "address": "xx-xx-xxxx,x - street, x - country",
                "gender" : "male",
                "phone": {
                    "mobile": "+91 0000000000",
                    "home": "00 000000",
                    "office": "00 000000"
                }
        }
    ]
}


ViewController.swift


//
//  ViewController.swift
//  JsonApp1
//
//  Created by Divakar Singh on 4/4/17.
//  Copyright © 2017 Divakar Singh. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
    let url = URL(string: "http://api.androidhive.info/contacts/")
        let task = URLSession.shared.dataTask(with: url!){
            (data, response, error) in
            
            if error != nil{
                print("Error")
            }else{
                if let content = data
                {
                    do{
                        let myJson = try JSONSerialization.jsonObject(with: content, options: JSONSerialization.ReadingOptions.mutableContainers) as AnyObject
                        
                        // let print all contact data
                        if let contact = myJson["contacts"] as? NSArray
                        {
                            // To print single data
                            /*let info = contact[0] as? NSDictionary
                            print(info?["id"] as! String)
                            print(info?["name"] as! String)
                            print(info?["email"] as! String)
                            print(info?["address"] as! String)
                            print(info?["phone"])*/
                            
                            // To print array data
                            for i in 0..<contact.count
                            {
                                let info = contact[i] as? NSDictionary
                                print(info?["id"] as! String)
                                print(info?["name"] as! String)
                                print(info?["email"] as! String)
                                print(info?["address"] as! String)
                                print(info?["phone"])
                                print("\n**********************NEXT************************\n")
                            }
                            
                        }
                        
                        
                    }catch
                    {
                        
                    }
                }
            }
            
        }
        task.resume()
        
    }
    
    

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

new

Tuesday 4 April 2017

Swift : Json to get all data



Step - 1 : Add security to app in info.plist file
Open info.plist --> Bundle Version --> click on (+) --> Select 'App Transport Security Setting' --> click on (+) --> Select 'Allow Arbitrary Load' --> change value to 'YES'.

Step - 2 : Open ViewController.swift and place below code

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
    let url = URL(string: "http://api.androidhive.info/contacts/")
        let task = URLSession.shared.dataTask(with: url!){
            (data, response, error) in
            
            if error != nil{
                print("Error")
            }else{
                if let content = data
                {
                    do{
                        let myJson = try JSONSerialization.jsonObject(with: content, options: JSONSerialization.ReadingOptions.mutableContainers) as AnyObject
                        print(myJson)
                        
                    }catch
                    {
                        
                    }
                }
            }
            
        }
        task.resume()
        
    }
    
    

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

Step - 3 : Now run the app

Note :  To see the result,  Click on hide or Show Debug area --> Show Console
For help, See Image


Choose


Now Click Show Console to get Json Result





Navigation Controller to switch between two View




Swift 3.0 : View Controller With Navigation Controller

1. Create New Project
2. SingleView without core data
3. Click on MainStoryBoard. You will see View Controller. 

4. Now click on Object Library ( Right Bottom corner )




5. Click on ViewController and drag it to MainStoryBoard

6. Place One Label(A) and a Button(ViewB) on first ViewController
7. Place One Label(B)on second ViewController




8. Now, Select first ViewController and Goto Editor --> Embed In --> NavigationController




9. Now Click on ViewB button and hold CTRL key and drag it to Second ViewController and leave
10. Action Segue popup showing -- Choose "Show"




11. Now run the app, when you click on ViewB button it showing Second ViewController with Label(B)
12. You also see one BACK button on top NavigationController. Click to back on first ViewController





If you want to add title on ViewController1 and ViewController2

1. Double Click on NavigationCintroller on ViewController1 then add title
2. But when you going to add ViewController2 then not showing show
then go to Widget Library --> Choose Nav Bar and place to ViewController2
3. Now double click to add title.

Monday 3 April 2017

ViewController : Passing Data



Swift 3.2


1. Create New Project
2. Single View without core data
3. Click on MainStoryBoard



4. On main ViewController - Add 1 button and 1 textfield
5. Now, Add new View Controller file - ViewTwo 
(Right click on main project --> New File --> Choose Swift --> and type name "ViewTwo" --> create ).  SEE IMAGEs







6. Inside the ViewTwo class file write this code. 

ViewTwo.swift

//
//  ViewTwo.swift
//  ShowView1
//
//  Created by Divakar Singh on 4/3/17.
//  Copyright © 2017 Divakar Singh. All rights reserved.
//

import Foundation
import UIKit

class ViewTwo : UIViewController {
    
    @IBOutlet var Label: UILabel!
    var LabelText = String()
    
    
    override func viewDidLoad() {
        Label.text = LabelText
    
    }

}


 7. Inside the ViewController class file write this code

ViewController.swift

//
//  ViewController.swift
//  ShowView1
//
//  Created by Divakar Singh on 4/3/17.
//  Copyright © 2017 Divakar Singh. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    @IBOutlet var TextField: UITextField!
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        var DestViewController : ViewTwo = segue.destination as! ViewTwo
        DestViewController.LabelText = TextField.text!
        
  
    }

}