0

I have a very basic/simple swift app, with a WebView embedded that links to a Youtube video via URL. See the below entire code. I want to add 10 other Youtube URL's and randomly populate them in the WebView via a button called "shuffleButton". How can I, very simply, use this button to load a video and then randomly shuffle through them. Can I locate the master list of video URL's in this same ViewController? Thanks!

// ViewController.swift

@IBOutlet var videoView: UIWebView!

@IBOutlet var shuffleButton: UIButton!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

let youtubeURL = "https://www.youtube.com/embed/Rg6GLVUnnpM"

    videoView.allowsInlineMediaPlayback = true

    videoView.loadHTMLString("<iframe width=\"\(videoView.frame.width)\" height=\"\(videoView.frame.height)\" src=\"\(youtubeURL)?&playsinline=1\" frameborder=\"0\" allowfullscreen></iframe>", baseURL: nil)

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
Andy Voelker
  • 81
  • 3
  • 10

2 Answers2

1

Not sure if this would be a very efficient solution but you could just create a basic lookup and call the arc4random_uniform(_:) function to generate a random number:

struct VideoLinks {
    static func getLink(forNumber number: Int) -> String {
        switch number {
        case 1: return "http://www.example.com/one"
        case 2: return "http://www.example.com/two"
        case 3: return "http://www.example.com/three"
        case 4: return "http://www.example.com/four"
        case 5: return "http://www.example.com/five"
        case 6: return "http://www.example.com/six"
        case 7: return "http://www.example.com/seven"
        case 8: return "http://www.example.com/eight"
        case 9: return "http://www.example.com/nine"
        case 10: return "http://www.example.com/ten"
        default: return "http://www.example.com/one"
        }
    }
}

func shuffle() -> String {
    let linkNumber = Int(arc4random_uniform(11))
    return VideoLinks.getLink(forNumber: linkNumber)
}

Or if you want an even simpler solution, just create an array of the links and call a random index:

let videoLinks = [
    "http://www.example.com/1",
    "http://www.example.com/2",
    "http://www.example.com/3",
    "http://www.example.com/4",
    "http://www.example.com/5",
    "http://www.example.com/6",
    "http://www.example.com/7",
    "http://www.example.com/8",
    "http://www.example.com/9",
    "http://www.example.com/10"
]

func shuffle() -> String {
    let randomNumber = Int(arc4random_uniform(10))
    return videoLinks[randomNumber]
}
Morgan
  • 1,280
  • 1
  • 14
  • 15
0

Can I locate the master list of video URL's in this same ViewController?

Sure!

So lets go ahead and include that list of URLs in our view controller:

let urls = ["URL1", "URL2", "URL3", "URL4"] // etc.

(That could be declared anywhere inside your view controller, such as directly above viewDidLoad.)

Now, we need a way to pick a random URL to open. The logical way to do that would be to just generate a random array index. This question discusses generating random numbers between ranges, and this answer is a great way of writing a Swift-friendly way to do it. (If you choose to follow the extension route, than the extension will need to be written at a global level, outside of any class or other object.)

So assuming we defined the random Int extension described in the answer link above, we could get a random index like so:

let randomI = Int.random(0..<urls.count)

So we could add an IBAction to your view controller that would trigger when the button is pressed.

@IBAction func shuffleURL() {
    let randomI = Int.random(0..<urls.count)
    videoView.loadHTMLString(urls[randomI], baseURL: nil)
}

Note: If you have a small number of urls, this may result in the same URL being loaded consecutively.

Community
  • 1
  • 1
Matthew Seaman
  • 7,952
  • 2
  • 37
  • 47