简体   繁体   中英

Swift SystemSoundIDButton Vibrate

I have this code.

import AudioToolbox.AudioServices

if restoreData.boolForKey("VibrationSwitchButton") == true {
  vibra()
}

func vibra() {
  if UIDevice.currentDevice().model == "iPhone" {
  let systemSoundIDButton : SystemSoundID = 4095
  AudioServicesPlayAlertSound(SystemSoundID(systemSoundIDButton))
  // print("VIBRA")}
}

I have a UISwitch that active and deactivate this path of code. The switch works correctly but if I activate the vibrate in the general option (sound), I can't deactivate from my setting and contrary too.

How can I generalize my configuration? if I have to vibrate on in general setting I can deactivate on my app.

Swift 2.0 and Xcode 7.1

Use AudioServicesPlaySystemSound instead of AudioServicesPlayAlertSound .

sample code:

if restoreData.boolForKey("VibrationSwitchButton") == true {
   vibra()
} else {
   soundOnly()
}

func soundOnly() {
 if UIDevice.currentDevice().model == "iPhone" {
    let systemSoundIDButton : SystemSoundID = 4095
    AudioServicesPlaySystemSound(SystemSoundID(systemSoundIDButton))
     // print("Sound Only")
 }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM