Luiz Luiz - 3 months ago 7
iOS Question

Play a sound before opening an URL

Explanation



I'm having a problem when calling an action before opening App Store to rate my game. The sound won't be played. I've seen a game in which it worked. Am I doing anything wrong?

Code



func rateGame(){

let appIDString = "375380948"
let reviewsURLString = "itms-apps://itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?pageNumber=0&sortOrdering=1&type=Purple+Software&mt=8&id=\(appIDString)"
let reviewsURL = NSURL(string: reviewsURLString)

clickSoundFX()
UIApplication.sharedApplication().openURL(reviewsURL!)
}

func clickSoundFX(){
self.runAction(SKAction.playSoundFileNamed("buttonClick.mp3", waitForCompletion: false))
}




Thanks in advance,

Luiz.

Answer

When you launch clickSoundFX() and after you call:

UIApplication.sharedApplication().openURL(reviewsURL!)

your game start to make an action to play a simple sound but right after all the app going in applicationDidEnterBackground due to open an external url: in this exact moment all your game going in paused mode so you can't do any action.

A safe way to do your code is to make a pause sequence after your sound and check the completion like this:

let wait = SKAction.SKAction.waitForDuration(1.0)
let sound = SKAction.playSoundFileNamed("buttonClick.mp3")
let seq = SKAction.sequence([sound,wait])
self.runAction(seq,completion: {
     UIApplication.sharedApplication().openURL(reviewsURL!)
})