Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so dont bother with any of their useless mail servers here and just use oauth login instead. Thank the nice Russians for causing that. :)
Paste
Pasted as Swift by registered user mesutde ( 9 years ago )
//
// ViewController.swift
// gDictionary
//
// Created by YUNUS YILMAZ on 19.01.2017.
// Copyright © 2017 YUNUS YILMAZ. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var result: UITextView!
@IBOutlet weak var search: UITextField!
@IBAction func request(_ sender: Any) {
let requestURL: NSURL = NSURL(string: "https://glosbe.com/gapi/translate?from=eng&dest=tr&format=json&phrase=cat&pretty=true")!
let urlRequest: NSMutableURLRequest = NSMutableURLRequest(url: requestURL as URL)
let session = URLSession.shared
/*
Request Esnasında bilgi gönderilecek ise POST medthodu ile request işlemi örneği
let reqJson = ["User":"IOSClient"]
let reqData : NSData = NSKeyedArchiver.archivedData(withRootObject: reqJson) as NSData
JSONSerialization.isValidJSONObject(reqJson)
urlRequest.httpMethod = "POST"
urlRequest.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
urlRequest.httpBody = reqData as Data
*/
let task = session.dataTask(with: urlRequest as URLRequest) {
(data, response, error) -> Void in
let httpResponse = response as! HTTPURLResponse
let statusCode = httpResponse.statusCode
//print("jsonParse")
if (statusCode == 200) {
print("Everyone is fine, successfully.")
do{
let json = try JSONSerialization.jsonObject(with: data!, options:.allowFragments) as! [String:AnyObject]
let isSuccess : String = json["result"] as! String
if isSuccess == "ok"
{
let tuc = json["tuc"] as? NSArray
for element in tuc as! [[String:AnyObject]]
{
for (key, value) in element
{
if(key == "phrase")
{
if let text = value["text"] {
print("\(text!).")
}
}
}
}
}
else
{
self.result.text = "Bir hata oluştu."
}
}catch {
self.result.text = "Bir hata oluştu."
}
}
}
task.resume()
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Revise this Paste
Parent: 82718