Monday, April 20, 2015

AVCaptureTorchMode - Flashlight / Torch function

1. Drag a button to the storyboard. Make the text as "OFF".

2. Control-drag the button to ViewController.swift and make it an IBAction:

    @IBAction func myButton(sender: AnyObject) {
    }

3. Complete the code as below:

import UIKit
import AVFoundation

class ViewController: UIViewController {

    @IBAction func myButton(sender: AnyObject) {
        
        let myDevice = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)
        
        if (myDevice.hasTorch) {
            myDevice.lockForConfiguration(nil)
            if (sender.titleLabel!?.text == "ON")
            {
                myDevice.torchMode = AVCaptureTorchMode.Off
                sender.setTitle("OFF", forState:  UIControlState.Normal);
            }
            else
            {
                myDevice.torchMode = AVCaptureTorchMode.On
                sender.setTitle("ON", forState:  UIControlState.Normal);
            }
            myDevice.unlockForConfiguration()
        } else {
            let alertView = UIAlertView()
            alertView.message = "No Flash Detected!"
            alertView.addButtonWithTitle("OK")
            alertView.show()
        }
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

    }

========================

Saturday, April 18, 2015

Toggle Button using setTitle()

1. Drag a button to the storyboard. Make the text as "OFF".

2. Control-drag the button to ViewController.swift and make it an IBAction:

    @IBAction func myButton(sender: AnyObject) {
    }

3. Complete the code as below:

import UIKit

class ViewController: UIViewController {

    @IBAction func myButton(sender: AnyObject) {
        
        if (sender.titleLabel!?.text == "ON")
        {
            sender.setTitle("OFF", forState:  UIControlState.Normal);
        }
        else
        {
            sender.setTitle("ON", forState:  UIControlState.Normal);
        }
    }

========================

Thursday, April 16, 2015

iAd for iPhones and iPads

1. Choose "Swift" for language and "Universal" for devices. Set the project name as iad_swift_universal. (Note: If "iphone" is used for the devices option, iAd works with iphones only and iPads will not display iAd.)

2. In "Targets," select iad_swift_universal. At top select the "General" tab. Select "Linked Frameworks and Libraries" and add the iAd framework.

3. Select the storyboard and unselect "Use Auto Layout."

4. Drag an iAd BannerView to the storyboard.

5. Control-drag the ADBannerView to the ViewController. Select "delegate" as the outlet.

6. Complete the code as below:


import UIKit
import iAd

class ViewController: UIViewController, ADBannerViewDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    func bannerViewDidLoadAd(banner: ADBannerView!) {
    }
    func bannerViewActionShouldBegin(banner: ADBannerView!, willLeaveApplication willLeave: Bool) -> Bool {
        return true
    }
    func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) {


    }

Friday, April 3, 2015

Play an embedded Youtube video

1. Uncheck the 'Use Auto Layout' option in the file inspector.

2. Drag the web view to the storyboard.  

3. Control-drag the web view to ViewController.swift. Name it myWebView and obtain the code as below:


@IBOutlet weak var myWebView: UIWebView!

4. Complete the code as below:

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.
        
        let myURL : NSURL = NSURL(string: "
https://www.youtube.com/embed/vmMAI86Lub0")!
       //Note: use the "embed" address instead of the "watch" address.
        let myURLRequest : NSURLRequest = NSURLRequest(URL: myURL)
        self.myWebView.loadRequest(myURLRequest)
    }

Monday, March 23, 2015

presentViewController - [kUTTypeMovie] Video Capture and Play

1. Drag a button to the storyboard. Name the button as 'Capture Video.' Uncheck the 'Use Auto Layout' option in the file inspector.

2. Control-drag the button to ViewController.swift and selection 'Action' as the connection type. Name it captureVideo and obtain the code as below:

    @IBAction func captureVideo(sender: AnyObject) {

    }

3. Write the code as below:

import UIKit
import MobileCoreServices

class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    
    @IBAction func captureVideo(sender: AnyObject) {
        if (UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera)) {
            var myPickerController = UIImagePickerController()
            myPickerController.delegate = self
            myPickerController.allowsEditing = true
            myPickerController.sourceType = UIImagePickerControllerSourceType.Camera
            myPickerController.mediaTypes = [kUTTypeMovie]
            self.presentViewController(myPickerController, animated: true, completion: nil)
        }
        
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }


Saturday, January 17, 2015

UIPickerView - One selection among multiple options


1. Drag a UIPickerView to the storyboard

2. Control-Drag the UIPickerView to the ViewController for datasource and delegate

3. Complete the code as below:

import UIKit

class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {

    let languageList = ["United States", "United Kingdom", "Australia", "China", "Hong Kong", "Taiwan"]
    let languageCodeList = ["en-us", "en-gb", "en-au", "zh-cn", "zh-hk", "zh-tw"]
    
    func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
        return 1
    }
    
    func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return languageList.count
    }
    
    func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
        return languageList[row]
    }
    
    func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        println("language code is \(languageCodeList[row])")

    }

Show/Hide the keyboard while using the UITextView/UITextField

To show the keyboard:


self.inputTextView.becomeFirstResponder()


To hide the keyboard:

self.inputTextView.resignFirstResponder()



To hide the keyboard while tapping outside the UITextField/UITextField:

@IBOutlet weak var inputTextView: UITextView!

    override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
        self.inputTextView.resignFirstResponder()
    }


To hide the keyboard while tapping the return key in the UITextField:

class ViewController: UIViewController, UITextFieldDelegate {

    @IBOutlet weak var inputTextField: UITextField!
    
    func textFieldShouldReturn(textField: UITextField) -> Bool {
        textField.resignFirstResponder()
        return true
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.inputTextField.delegate = self