Showing posts with label hardware. Show all posts
Showing posts with label hardware. Show all posts

Monday, March 7, 2016

Make the iPhone vibrate with kSystemSoundID_Vibrate

The following code was built with Xcode 7.2.1 (Swift 2.1.1).

Create a new Single View Application project in Xcode and modify ViewController.swift as:


import UIKit
import AudioToolbox

class ViewController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //Buttons
        let button1 : UIButton!
        button1 = UIButton(frame: CGRectMake(0, 0, 300, 30))
        button1.center = CGPointMake(view.center.x, 150)
        button1.setTitle("Vibrate Alert", forState: UIControlState.Normal)
        button1.setTitleColor(UIColor.blueColor(), forState: UIControlState.Normal)
        button1.setTitleColor(UIColor.cyanColor(), forState: UIControlState.Highlighted)
        button1.addTarget(self, action: "btn1Pressed:", forControlEvents: UIControlEvents.TouchUpInside)
        view.addSubview(button1)
        
        let button2 : UIButton!
        button2 = UIButton(frame: CGRectMake(0, 0, 300, 30))
        button2.center = CGPointMake(view.center.x, 300)
        button2.setTitle("Vibrate System", forState: UIControlState.Normal)
        button2.setTitleColor(UIColor.blueColor(), forState: UIControlState.Normal)
        button2.setTitleColor(UIColor.cyanColor(), forState: UIControlState.Highlighted)
        button2.addTarget(self, action: "btn2Pressed:", forControlEvents: UIControlEvents.TouchUpInside)
        view.addSubview(button2)
    }
    
    func btn1Pressed(sender: UIButton) {
        AudioServicesPlayAlertSoundWithCompletion(SystemSoundID(kSystemSoundID_Vibrate), nil)
        //AudioServicesPlayAlertSound(SystemSoundID(kSystemSoundID_Vibrate))
    }
    
    func btn2Pressed(sender: UIButton) {
        AudioServicesPlaySystemSoundWithCompletion(SystemSoundID(kSystemSoundID_Vibrate), nil)
        //AudioServicesPlaySystemSound(SystemSoundID(kSystemSoundID_Vibrate))
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

}

Result with an iPhone 5:

The iPhone vibrates when pressing either of the buttons.

Result with an iPad 3rd generation:

The iPad neither vibrates nor beeps when pressing either of the buttons.

References:

How to make iPhone vibrate using Swift?
Making the iPhone vibrate

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.

    }

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