Saturday, 24 June 2017

Simple Web Browser

STEP - 1 : Main.Storyboard

A. Add a Textfield, Button, WebView to Main.Storyboard.

B. Add @IBOutlet to ViewController.swift  and Button action (@IBAction) ( Press 'Ctrl + Mouse Left Key' and drag to ViewController )

STEP - 2 : Add 'App Transport Security Setting' to info.plist file.

STEP - 3 : Inside 'App Transport Security Setting' -> Add 'Allow Arbitrary Load' and set value 'Yes'.

STEP - 4 : ViewController.swift

//
//  ViewController.swift
//  WebBrowser
//
//  Created by Admin on 03/04/1939 Saka.
//  Copyright © 1939 Saka Admin. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    
    @IBOutlet weak var addressBar: UITextField!
    
    @IBOutlet weak var myWebview: UIWebView!
    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.
    }


    @IBAction func surfBrowser(_ sender: Any) {
        
       // var urName : String
        
        let url = URL(string: "http://\(addressBar.text!)")
        myWebview.loadRequest(URLRequest(url: url!))
    }
}



Youtube Player (Swift)


STEP - 1 : Main.Storyboard

A. Add a WebView to Main.Storyboard.

B. Add @IBOutlet to ViewController.swift ( Press 'Ctrl + Mouse Left Key' and drag to ViewController )

STEP - 2 : Add 'App Transport Security Setting' to info.plist file.

STEP - 3 : Inside 'App Transport Security Setting' -> Add 'Allow Arbitrary Load' and set value 'Yes'.

STEP - 4 : ViewController.swift

*******************------------------*******************
//
//  ViewController.swift
//  YoutubeEmbed
//
//  Created by Admin on 03/04/1939 Saka.
//  Copyright © 1939 Saka Admin. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var myWebview: UIWebView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        getVideo(videocode: "Kd57YHWqrsI")
    }

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

    func getVideo(videocode : String)
    {
        let url1 = URL(string: "http://www.youtube.com/embed/\(videocode)")
        myWebview.loadRequest(URLRequest(url: url1!))
    }

}


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