I have a textview in which a user is supposed to enter a url. First I am checking the validity of the URL. Once the validity is confirmed, I need to check if It's a video url or photo url. I have two arrays of all possible video (MP4, MOV etc) and photo (JPG, PNG) file formats and I get an appropriate message if the url contains any element from those two arrays.
Here is a sample instagram video link.. For example: https://www.instagram.com/p/B-PilcbFy2w/
The following Link is a valid video link but it does not contain any video file formats like (MOV, MP4 etc). How can I validate such links?
I know the concept of regular expressions to validate such things but for that I'll have to implement a bunch of IF statements which is not an efficient way. The sample video Link is just an example. I need to validate all kind of video links whether they are from social media platforms or whatever. Help regrading this will really be appreciated.
Here is my sample code..
@IBOutlet weak var textVW: UITextView!
@IBAction func validateButton(_ sender: UIButton) {
let userInputURL = URL(string: textVW.text)
if userInputURL != nil && userInputURL?.scheme != nil && userInputURL?.host != nil {
// - a scheme (like http://)
// - a host (like stackoverflow.com)
print("Valid URL..")
// Now checking url type, if its a video or image url..
checkURLType(inputURL: userInputURL!)
}
else {
print("Invalid URL..")
}
}
func checkURLType(inputURL : URL) {
// Most commom image types..
let imageExtensions = ["png", "jpg", "gif", "tif"]
// Most cmmon video types..
let videoExtensions = ["WEBM", "MPG", "MPEG", "MPE", "MP4", "M4P", "M4V", "AVI", "WMV", "MOV"]
let url: URL? = NSURL(fileURLWithPath: inputURL.path) as URL
let pathExtention = url?.pathExtension
if imageExtensions.contains(pathExtention!)
{
print("Image URL: \(String(describing: url))")
}
else if videoExtensions.contains(pathExtention!)
{
print("Video URL: \(String(describing: url))")
}
else
{
print("Does Not Exist: \(String(describing: url))")
}
}
Instagram does not include the file format as you are looking for. Instead, you can use a simple trick to confirm if the link is representing a video or a photo. Thats the Instagram link you provided: https://www.instagram.com/p/B-PilcbFy2w/
When we add ?__a=1 at the end of the link of an Instagram post we will get something like this:
you can easily search for the element "is_video", if it is false then the post is a picture, otherwise it is a video. please mark answered if helped.