Youtube Video Id from URL - Swift3

后端 未结 8 2497
眼角桃花
眼角桃花 2021-02-15 12:52

Basically I have a Youtube URL as string, I want to extract the video Id from that URL. I found some code in objective c that is as below:

NSError *error = NULL;         


        
8条回答
  •  没有蜡笔的小新
    2021-02-15 13:30

    To get video Id from Youtube Url, use code # Swift4 :

    var videoId = ""
    
        if youtubeLink.lowercased().contains("youtu.be"){
                linkString = youtubeLink
                if let range = linkString.range(of: "be/"){
                    videoId = youtubeLink[range.upperBound...].trimmingCharacters(in: .whitespaces)
                }
            }
            else if youtubeLink.lowercased().contains("youtube.com"){
                linkString = youtubeLink
                if let range = linkString.range(of: "?v="){
                    videoId = youtubeLink[range.upperBound...].trimmingCharacters(in: .whitespaces)
                }
            }
    

    Hope will be helping! :)

提交回复
热议问题