func getJSON(rs: String, tx: String) -> String {
var text = ""
let inputURL = self.inputURL.text
let link = inputURL?.components(separatedBy: "wall")
let baseURL = "https://api.vk.com/method/wall.getById?posts=\(link![1])&access_token=87c166c356c35f6d835048af60d84014fa57d4eae&v=5.64"
let url = NSURL(string: baseURL)
let request = NSURLRequest(url: url! as URL)
let session = URLSession(configuration: .default)
let task = session.dataTask(with: request as URLRequest) { (data, response, error) -> Void in
if error == nil {
let swiftyJSON = JSON(data: data!)
let response = swiftyJSON[rs].arrayValue
for tobject in response {
let text = tobject[tx].stringValue
}
}
}
task.resume()
return text
}
func parse(callback: @escaping (_ text: String, _ images: [String]) -> Void) {
let baseURL = "https://api.vk.com/method/wall.getById?posts=-34451036_490279)&access_token=5502c502caef0d55e1758dd&v=5.64"
let url = NSURL(string: baseURL)
let request = NSURLRequest(url: url! as URL)
let session = URLSession(configuration: .default)
let task = session.dataTask(with: request as URLRequest) { (data, response, error) -> Void in
if error == nil {
let swiftyJSON = JSON(data: data!)
let response = swiftyJSON["response"].arrayValue
var links = [String]()
for tobject in response {
let text = tobject["text"].stringValue
let attachments = tobject["attachments"].arrayValue
for photo in attachments {
let searchPhoto = photo["photo"].dictionary
if let val = searchPhoto?["photo_130"]?.stringValue {
links.append(val)
}
}
callback(text, links)
}
}
}
task.resume()
}
parse(callback: { (text, links) in
recipe.descriptionRecipe = text
(UIApplication.shared.delegate as! AppDelegate).saveContext()
self.goToHome()
})
func getJSON(rs: String, tx: String, completion: @escaping ((String) -> Void)) {
DispatchQueue.main.async { [weak self] in
guard let `self` = self else { return }
let inputURL = self.inputURL.text
let link = inputURL?.components(separatedBy: "wall")
let baseURL = "https://api.vk.com/method/wall.getById?posts=\(link![1])&access_token=87c166c356c35f6d835048af60d84014fa57d4eae&v=5.64"
let url = URL(string: baseURL)
let request = NSURLRequest(url: url! as URL)
let session = URLSession(configuration: .default)
session.dataTask(with: request as URLRequest) { (data, response, error) -> Void in
if error == nil {
let swiftyJSON = JSON(data: data!)
let response = swiftyJSON[rs].arrayValue
for tobject in response {
completion(tobject[tx].stringValue)
}
}
}
}
}