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)
    }