{"id":3782,"date":"2021-08-17T14:15:45","date_gmt":"2021-08-17T06:15:45","guid":{"rendered":"http:\/\/123.57.164.21\/?p=3782"},"modified":"2021-08-17T14:15:45","modified_gmt":"2021-08-17T06:15:45","slug":"how-to-make-iphone-vibrate-using-swift","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=3782","title":{"rendered":"How To Make IPhone Vibrate Using Swift?"},"content":{"rendered":"\n<figure class=\"wp-block-embed is-type-wp-embed is-provider-izziswift wp-block-embed-izziswift\"><div class=\"wp-block-embed__wrapper\">\n<blockquote class=\"wp-embedded-content\" data-secret=\"uZ5CsgtOTn\"><a href=\"https:\/\/izziswift.com\/how-to-make-iphone-vibrate-using-swift\/\">How to make iPhone vibrate using Swift?<\/a><\/blockquote><iframe class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" title=\"&#8220;How to make iPhone vibrate using Swift?&#8221; &#8212; iZZiSwift\" src=\"https:\/\/izziswift.com\/how-to-make-iphone-vibrate-using-swift\/embed\/#?secret=uZ5CsgtOTn\" data-secret=\"uZ5CsgtOTn\" width=\"600\" height=\"338\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe>\n<\/div><\/figure>\n\n\n\n<h5 class=\"wp-block-heading\">Solution 1:<\/h5>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import UIKit\nimport AudioToolbox\n\nclass ViewController: UIViewController {\n\n    override func viewDidLoad() {\n        super.viewDidLoad()\n        \/\/ \u8fd9\u53e5\u8bdd\u4f1a\u4f7fiPhone\u4ea7\u751f\u9707\u52a8\u6548\u679c\uff0c\u53ef\u4ee5\u52a0\u5728\u6309\u94ae\u91cc\u9762\u3002\n        AudioServicesPlayAlertSound(SystemSoundID(kSystemSoundID_Vibrate))            \n    }\n}\n<\/pre>\n\n\n\n<p>load onto your phone and it will vibrate. You can put it in a function or IBAction as you wish.<\/p>\n\n\n\n<p>Code Update:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"> AudioServicesPlayAlertSoundWithCompletion(SystemSoundID(kSystemSoundID_Vibrate)) { }\n<\/pre>\n\n\n\n<p>As apple code docs written:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p><strong>This function will be deprecated in a future release. Use<br>AudioServicesPlaySystemSoundWithCompletion instead.<br><\/strong><em><\/em><\/p><\/blockquote>\n\n\n\n<p><strong>NOTE:<\/strong>\u00a0If vibrate doesn\u2019t work. check vibrate is enable in sounds and haptics settings<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<h5 class=\"wp-block-heading\">Solution 2:<\/h5>\n\n\n\n<p>In iOS 10 on iPhone 7 or 7 Plus, try:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">let generator = UIImpactFeedbackGenerator(style: .heavy)\ngenerator.impactOccurred()\n<\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p><\/p>\n\n\n\n<h5 class=\"wp-block-heading\">Solution 3:<\/h5>\n\n\n\n<p>Other vibration types:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import AudioToolbox\nAudioServicesPlaySystemSound(1519) \/\/ Actuate \"Peek\" feedback (weak boom)\nAudioServicesPlaySystemSound(1520) \/\/ Actuate \"Pop\" feedback (strong boom)\nAudioServicesPlaySystemSound(1521) \/\/ Actuate \"Nope\" feedback (series of three weak booms)\n<\/pre>\n\n\n\n<p><em>More About Vibration \u2013\u00a0<a href=\"http:\/\/www.mikitamanko.com\/blog\/2017\/01\/29\/haptic-feedback-with-uifeedbackgenerator\/\">http:\/\/www.mikitamanko.com\/blog\/2017\/01\/29\/haptic-feedback-with-uifeedbackgenerator\/<\/a><\/em><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<h5 class=\"wp-block-heading\">Solution 4:<\/h5>\n\n\n\n<p>Swift 4.2 Updated<\/p>\n\n\n\n<p>Just insert code below into your project.<\/p>\n\n\n\n<p><strong>Usage<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Vibration.success.vibrate()\n<\/pre>\n\n\n\n<p><strong>Source Code<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"> enum Vibration {\n        case error\n        case success\n        case warning\n        case light\n        case medium\n        case heavy\n        @available(iOS 13.0, *)\n        case soft\n        @available(iOS 13.0, *)\n        case rigid\n        case selection\n        case oldSchool\n        public func vibrate() {\n            switch self {\n            case .error:\n                UINotificationFeedbackGenerator().notificationOccurred(.error)\n            case .success:\n                UINotificationFeedbackGenerator().notificationOccurred(.success)\n            case .warning:\n                UINotificationFeedbackGenerator().notificationOccurred(.warning)\n            case .light:\n                UIImpactFeedbackGenerator(style: .light).impactOccurred()\n            case .medium:\n                UIImpactFeedbackGenerator(style: .medium).impactOccurred()\n            case .heavy:\n                UIImpactFeedbackGenerator(style: .heavy).impactOccurred()\n            case .soft:\n                if #available(iOS 13.0, *) {\n                    UIImpactFeedbackGenerator(style: .soft).impactOccurred()\n                }\n            case .rigid:\n                if #available(iOS 13.0, *) {\n                    UIImpactFeedbackGenerator(style: .rigid).impactOccurred()\n                }\n            case .selection:\n                UISelectionFeedbackGenerator().selectionChanged()\n            case .oldSchool:\n                AudioServicesPlaySystemSound(SystemSoundID(kSystemSoundID_Vibrate))\n            }\n        }\n    }<\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<h5 class=\"wp-block-heading\">Solution 5:<\/h5>\n\n\n\n<p>For\u00a0<strong>iOS 10.0+<\/strong>\u00a0You can try\u00a0<a href=\"https:\/\/developer.apple.com\/documentation\/uikit\/uifeedbackgenerator\">UIFeedbackGenerator<\/a><\/p>\n\n\n\n<p>Simple viewController above, just replace your view controller in your test \u201csingle view app\u201d<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import UIKit\nclass ViewController: UIViewController {\n    var i = 0\n    override func viewDidLoad() {\n        super.viewDidLoad()\n        let btn = UIButton()\n        self.view.addSubview(btn)\n        btn.translatesAutoresizingMaskIntoConstraints = false\n        btn.widthAnchor.constraint(equalToConstant: 160).isActive = true\n        btn.heightAnchor.constraint(equalToConstant: 160).isActive = true\n        btn.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true\n        btn.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true\n        btn.setTitle(\"Tap me!\", for: .normal)\n        btn.setTitleColor(UIColor.red, for: .normal)\n        btn.addTarget(self, action: #selector(tapped), for: .touchUpInside)\n    }\n    @objc func tapped() {\n        i += 1\n        print(\"Running \\(i)\")\n        switch i {\n        case 1:\n            let generator = UINotificationFeedbackGenerator()\n            generator.notificationOccurred(.error)\n        case 2:\n            let generator = UINotificationFeedbackGenerator()\n            generator.notificationOccurred(.success)\n        case 3:\n            let generator = UINotificationFeedbackGenerator()\n            generator.notificationOccurred(.warning)\n        case 4:\n            let generator = UIImpactFeedbackGenerator(style: .light)\n            generator.impactOccurred()\n        case 5:\n            let generator = UIImpactFeedbackGenerator(style: .medium)\n            generator.impactOccurred()\n        case 6:\n            let generator = UIImpactFeedbackGenerator(style: .heavy)\n            generator.impactOccurred()\n        default:\n            let generator = UISelectionFeedbackGenerator()\n            generator.selectionChanged()\n            i = 0\n        }\n    }\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Solution 1: load onto your phone and it will vibrate. Y [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,8],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/3782"}],"collection":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3782"}],"version-history":[{"count":6,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/3782\/revisions"}],"predecessor-version":[{"id":3788,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/3782\/revisions\/3788"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3782"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3782"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3782"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}