Well guys as you know whenever any novice developer starts coding/learning iOS app development after spending some time in coding they starts developing a very famous app Media Player. And try to integrate web downloading feature in their app. But in every tutorial && book on web source they all they find is downloading a dedicated single file of any type like any image file or video file or any mp3 file. But today you will learn how to download mp3 files generally whichever is search by user by putting song name in the search text field. I will be using a site (www.mp3mad.com) website, by which I will be able to download Hindi, punjabi, or any ghazal from it. Here is the code for this feature of downloading any song by entering its name in the search field.
The general idea behind this is that all you have to do is just fetch the code of that particular website && extract the url of the entered string && the create a session in which you can download the data of that particular song && save it as a mp3 file in your drive.
@IBAction func loadLocation(AnyObject) {
var urlText = textField.text
var startindex = urlText.startIndex
var endindex = urlText.endIndex
var range = urlText.startIndex..<urlText.endIndex
var urlText1: String?
var urltext1 = urlText.stringByReplacingOccurrencesOfString(" ", withString: "+", options: NSStringCompareOptions.CaseInsensitiveSearch, range: range)
if !urlText.hasPrefix("http:") && !urlText.hasPrefix("https:") {
urlText1 = "http://cdn1.mp3mad.info/search.php?ty=\(urltext1)&sh=Song&catz=All+Music"
let url = NSURL(string: urlText1!)
let task = NSURLSession.sharedSession().dataTaskWithURL(url!) {(data, response, error) in
var urlContent = NSString(data: data, encoding: NSUTF8StringEncoding)
var contentArray = (urlContent as String).componentsSeparatedByString("<img src=/mp3.gif> <a href=\"")
for var i = 1; i < contentArray.count; i++ {
var newcontentArray = contentArray[i].componentsSeparatedByString(".html\">")
//getting all the urls of the next page or download page
//getting all the urls of the next page or download page
var newString = newcontentArray[0] + ".html"
var songName = newString.componentsSeparatedByString("/")
var realSongName = songName[4].componentsSeparatedByString(".") songNames.append(realSongName[0].stringByReplacingOccurrencesOfString("-", withString: " "))
urlOfNextPage.append(newString)
}
println("names are \(songNames)")
for i in urlOfNextPage {
println("urls are \(i)")
}
}
task.resume()
}
}
Here in this code all we are doing is getting the song name that is entered by user in the text field in textField variable && the encoding it into a "URL" by embedding it into the search url of this particular website. && then in the session we are downloading the data of the website so that we can extract the real url of the song by breaking the complete string into parts.All this code does is searching the user entered song on the "www.mp3mad.com" website. Now all we need to do again to extract the real download url && download the song so here is the code for doing so....
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let escapedSearchTermCompleteUrl = finalUrl[indexPath.row].stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)
let url = NSURL(string: escapedSearchTermCompleteUrl!)
println(escapedSearchTermCompleteUrl!)
let urlRequest = NSURLRequest(URL: url!)
println(urlRequest)
NSURLConnection.sendAsynchronousRequest(urlRequest, queue: NSOperationQueue.mainQueue(), completionHandler: {
response, data, error in
if error != nil {
println("There was an error...")
} else {
var documentDirectory : String?
var paths : [AnyObject] = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
if paths.count > 0 {
documentDirectory = paths[0] as? String
println("\(finalNames[indexPath.row])")
var savePath = documentDirectory! + "/\(finalNames[indexPath.row]).mp3"
println(savePath)
NSFileManager.defaultManager().createFileAtPath(savePath, contents: data, attributes: nil)
}
}
})
}
Well guys this is the idea of how you download a user entered song from web.
I hope you like this idea at least......
No comments:
Post a Comment