How to pass raw JSON Body with POST request in Moya request - ios

i'm using Moya library to calling api in my project.
now one of apis required to pass raw json object(multiples object as single object) with POST request in body. it is working fine in postman.
check below screenshot,
also check raw body json,
{
"geometry": {
"location": {
"lat": "22.7195687",
"lng": "75.8577258"
}
},
"icon": "https://maps.gstatic.com/mapfiles/place_api/icons/v1/png_71/geocode-71.png",
"name": "Indore",
"photos": [
{
"photo_reference": "Aap_uECCOLowEnJ2yBUzF0nwRjV5jBx2_JWsofVosuLVvlr-ClIMHNR5-QGIe4phK-3_Bj_laHD_XH_LvlmGDzm33KvxuO1XzaZocxTLOVUdSGI3_otXvpx_FbuzmwiibZiylQEMkekTLKbLdXjK8H3w10nOcoJE-InDVvf5P7Cvyum_kk9k"
}
],
"place_id": "ChIJ2w1BG638YjkR9EBiNdrEbgk",
"reference": "ChIJ2w1BG638YjkR9EBiNdrEbgk",
"types": [
"locality",
"political"
],
"vicinity": "Indore"
},
{
"geometry": {
"location": {
"lat": "22.7429365",
"lng": "75.8867267"
}
},
"icon": "https://maps.gstatic.com/mapfiles/place_api/icons/v1/png_71/generic_business-71.png",
"name": "Visual Maker",
"photos": [
{
"photo_reference": "Aap_uED84yCmvAirxt-dSdPPSE3O_eBSunEiSOM1Uzr0kNNMiJBVvPtBuCuwck2Ek0CDg7S8JP09Iva3Rjhq63O1Tyql_CTeMRF_GWC19QfZUFwwvadcRbfLWo6Wqn4ndCTCh5A6RV212PJcB0HZqe6YV7FphiV_XjkP9pCvk5JLDKNrvOXz"
}
],
"place_id": "ChIJGwLEIlr9YjkRnr8uTQiQ8KU",
"reference": "ChIJGwLEIlr9YjkRnr8uTQiQ8KU",
"types": [
"university",
"general_contractor",
"point_of_interest",
"establishment"
],
"vicinity": "behind Anop Cinema, K/112, LIG Colony, Indore"
},
}
this is two object but it can be multiples objects
below is my Moya class for api calling.
import Moya
import Result
import Alamofire
import UIKit
private func JSONResponseDataFormatter(_ data: Data) -> Data {
do {
let dataAsJSON = try JSONSerialization.jsonObject(with: data)
let prettyData = try JSONSerialization.data(withJSONObject: dataAsJSON, options: .prettyPrinted)
return prettyData
} catch {
return data // fallback to original data if it can't be serialized.
}
}
private extension String {
var urlEscaped: String {
return self.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)!
}
}
let MyAPIProvider = MoyaProvider<EBabuAPI>( plugins: [VerbosePlugin(verbose: true)])
enum MyAPI {
case topPlaceVibeAvg(geometry : [Any], icon:[String], name:[String], photos:[Any], placeIds:[String], reference:[String], types:[Any], vicinity:[Any])
}
extension EBabuAPI: TargetType {
var headers: [String : String]? {
switch self {
default:
return ["Authorization": SharedPreference.authToken()]
}
}
var baseURL : URL {
return URL(string: Constants.API.baseURL)! //["Authorization": "Bearer \(Helpers.userToken())"]
}
var path: String {
switch self {
case .topPlaceVibeAvg:
return "places/topPlaceVibeAvg"
}
}
public var method: Moya.Method {
switch self {
case .topPlaceVibeAvg :
return .post
}
}
var sampleData: Data {
return "".data(using: .utf8)!
}
var task: Task {
switch self {
case .topPlaceVibeAvg(let geometry,let icon, let name, let photos, let placeIds, let reference, let types, let vicinity):
return .requestParameters(parameters: ["geometry":geometry, "icon":icon, "name":name, "photos":photos, "place_id":placeIds, "reference":reference, "types":types, "vicinity":vicinity], encoding: JSONEncoding.default)
}
}
func dicToStrig(data : AnyObject) -> String {
do {
let jsonData = try JSONSerialization.data(withJSONObject: data, options: [])
let jsonString = String(data: jsonData, encoding: String.Encoding.ascii)!
return jsonString
} catch {
//handle error
print("error",error)
}
return ""
}
var parameterEncoding: ParameterEncoding {
switch self {
default:
return JSONEncoding.default
}
}
}
struct JsonArrayEncoding: Moya.ParameterEncoding {
public static var `default`: JsonArrayEncoding { return JsonArrayEncoding() }
public func encode(_ urlRequest: URLRequestConvertible, with parameters: Parameters?) throws -> URLRequest {
var req = try urlRequest.asURLRequest()
let json = try JSONSerialization.data(withJSONObject: parameters!["jsonArray"]!, options: JSONSerialization.WritingOptions.prettyPrinted)
req.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
req.httpBody = json
return req
}
}
as everyone can see above code i'm trying to call topPlaceVibeAvg api with their keys & values api calling successfully but it does not provide proper response same as postman screenshot.
here is api Response:
{
data = (
);
message = "average fetched successfully!";
statusCode = 200;
success = 1;
}
i'm using below ViewModel(placesViewModel) to call api.
var icon = [String]()
var name = [String]()
var name = [Any]()
var placeIds = [String]()
var reference = [String]()
var testTypes = [Any]()
var vicinity = [Any]()
var geometry = [Any]()
var photos = [Any]()
func topPlaceVibeAvg(_ completion: #escaping ((JSONDictionary) -> ())) {
if !isPullToRefresh { Loader.show("Looking around....") }
APIController.makeRequest(.topPlaceVibeAvg(geometry: geometry, icon: icon, name: name, photos: photos, placeIds: placeIds, reference: reference, types: types, vicinity: vicinity)) { (data, error) in
Loader.hide()
if error == nil {
completion(data!)
} else {
ToastAndAlertManager.showToastWithTitle((error?.desc)!, colorCode: ToastColor.error.rawValue)
}
}
}
here is my ViewController class for api
placesViewModel.geometry = self.nearByPlacesArray.map{$0.geometry}
placesViewModel.icon = self.nearByPlacesArray.map{$0.icon}
placesViewModel.name = self.nearByPlacesArray.map{$0.name}
placesViewModel.photos = self.nearByPlacesArray.map{$0.photos}
placesViewModel.placeIds = self.nearByPlacesArray.map{$0.placeId}
placesViewModel.reference = self.nearByPlacesArray.map{$0.reference}
placesViewModel.testTypes = self.nearByPlacesArray.map{$0.types}
placesViewModel.vicinity = self.nearByPlacesArray.map{$0.vicinity}
placesViewModel.topPlaceVibeAvg { (data) in
print(data)
}
Note : **nearByPlacesArray** is main array & i'm to sorting my required array object from here
I'm really tried with problem, i've already search regarding this thing but got no answers.
AnyOne have idea about this?

To send complex parameters in the request body you can use the Moya function:
requestCompositeData(bodyData: Data, urlParameters: [String: Any])
Moya have this comment for this function:
A requests body set with data, combined with url parameters
So this function allow us send anything in the request body.
For example something like this is a good aproach:
import Moya
import SwiftyJSON
// Data task in MoyaProvider
var task: Task {
switch self {
case .categoriesAndSubCategories:
let graphQLJson = JSON([
[
"query": "query categories { categories { id, title, description, slug, adsStats, subcategories { id, title, slug } } }"
]
])
return .requestCompositeData(bodyData: try! graphQLJson.rawData(), urlParameters: [:])
}
}

Related

if i want to receive data from the server the app does not go to the URLSession if debug hem then skip the URLSession and i don't know why

This is the Json object which I should receive
[
{
"id": "18",
"profileName": "testProfiel24"
},
{
"id": "19",
"profileName": "testProfiel25"
},
{
"id": "1021",
"profileName": "testProfiel26"
},
{
"id": "1022",
"profileName": "testProfiel27"
},
{
"id": "1023",
"profileName": "testProfiel28"
}
]
This the function that I send, I used my api url to send an api call
func getData(){
let getToken : String? = KeychainWrapper.standard.string(forKey: "accessToken")
I add a value of the token to the url and with the print line I get the link
let url = URL(string: "http://....../profile/load")!
let queryItems = [URLQueryItem(name: "token", value: getToken!)]
let newUrl = url.appending(queryItems)!
print(newUrl)
The I make a URLSession with the new URL
but when hi goed to make dataTask. hi skips every things and goes to
resume()
URLSession.shared.dataTask(with: newUrl, completionHandler: { data, response, error in
guard let data = data,error == nil else{
print(error?.localizedDescription as Any)
return
}
var result : Profile?
do{
result = try JSONDecoder().decode(Profile.self, from: data)
}catch{
print("failed to convert\(error.localizedDescription)")
}
guard let json = result else{
return
}
print(json.id)
print(json.profileName)
}).resume()
with the debug mode I get this value of dataTask =
LocalDataTask <1F6380AD-5D01-490D-BA4A-94A7FD893531>.<2>
But I don't receive any data for the server because hi skips it
This the code to add a key and a value to the link and it works good
extension URL {
func appending(_ queryItems: [URLQueryItem]) -> URL? {
guard var urlComponents = URLComponents(url: self, resolvingAgainstBaseURL: true) else {
return nil
}
urlComponents.queryItems = (urlComponents.queryItems ?? []) + queryItems
return urlComponents.url
}
}
This the data that should receive for the server
struct Profile : Decodable {
let id, profileName : String
}

Codable for API request

How would I make this same API request through codables?
In my app, this function is repeated in every view that makes API calls.
func getOrders() {
DispatchQueue.main.async {
let spinningHUD = MBProgressHUD.showAdded(to: self.view, animated: true)
spinningHUD.isUserInteractionEnabled = false
let returnAccessToken: String? = UserDefaults.standard.object(forKey: "accessToken") as? String
let access = returnAccessToken!
let headers = [
"postman-token": "dded3e97-77a5-5632-93b7-dec77d26ba99",
"Authorization": "JWT \(access)"
]
let request = NSMutableURLRequest(url: NSURL(string: "https://somelink.com")! as URL,
cachePolicy: .useProtocolCachePolicy,
timeoutInterval: 10.0)
request.httpMethod = "GET"
request.allHTTPHeaderFields = headers
let session = URLSession.shared
let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
if (error != nil) {
print(error!)
} else {
if let dataNew = data, let responseString = String(data: dataNew, encoding: .utf8) {
print("----- Orders -----")
print(responseString)
print("----------")
let dict = self.convertToDictionary(text: responseString)
print(dict?["results"] as Any)
guard let results = dict?["results"] as? NSArray else { return }
self.responseArray = (results) as! [HomeVCDataSource.JSONDictionary]
DispatchQueue.main.async {
spinningHUD.hide(animated: true)
self.tableView.reloadData()
}
}
}
})
dataTask.resume()
}
}
I would suggest to do the following
Create Base Service as below
import UIKit
import Foundation
enum MethodType: String {
case get = "GET"
case post = "POST"
case put = "PUT"
case patch = "PATCH"
case delete = "DELETE"
}
class BaseService {
var session: URLSession!
// MARK: Rebuilt Methods
func FireGenericRequest<ResponseModel: Codable>(url: String, methodType: MethodType, headers: [String: String]?, completion: #escaping ((ResponseModel?) -> Void)) {
UIApplication.shared.isNetworkActivityIndicatorVisible = true
// Request Preparation
guard let serviceUrl = URL(string: url) else {
print("Error Building URL Object")
return
}
var request = URLRequest(url: serviceUrl)
request.httpMethod = methodType.rawValue
// Header Preparation
if let header = headers {
for (key, value) in header {
request.setValue(value, forHTTPHeaderField: key)
}
}
// Firing the request
session = URLSession(configuration: URLSessionConfiguration.default)
session.dataTask(with: request) { (data, response, error) in
DispatchQueue.main.async {
UIApplication.shared.isNetworkActivityIndicatorVisible = false
}
if let data = data {
do {
guard let object = try? JSONDecoder().decode(ResponseModel.self , from: data) else {
print("Error Decoding Response Model Object")
return
}
DispatchQueue.main.async {
completion(object)
}
}
}
}.resume()
}
private func buildGenericParameterFrom<RequestModel: Codable>(model: RequestModel?) -> [String : AnyObject]? {
var object: [String : AnyObject] = [String : AnyObject]()
do {
if let dataFromObject = try? JSONEncoder().encode(model) {
object = try JSONSerialization.jsonObject(with: dataFromObject, options: []) as! [String : AnyObject]
}
} catch (let error) {
print("\nError Encoding Parameter Model Object \n \(error.localizedDescription)\n")
}
return object
}
}
the above class you may reuse it in different scenarios adding request object to it and passing any class you would like as long as you are conforming to Coddle protocol
Create Model Conforming to Coddle protocol
class ExampleModel: Codable {
var commentId : String?
var content : String?
//if your JSON keys are different than your property name
enum CodingKeys: String, CodingKey {
case commentId = "CommentId"
case content = "Content"
}
}
Create Service to the specific model with the endpoint constants subclassing to BaseService as below
class ExampleModelService: BaseService<ExampleModel/* or [ExampleModel]*/> {
func GetExampleModelList(completion: ((ExampleModel?)/* or [ExampleModel]*/ -> Void)?) {
super.FireRequestWithURLSession(url: /* url here */, methodType: /* method type here */, headers: /* headers here */) { (responseModel) in
completion?(responseModel)
}
}
}
Usage
class MyLocationsController: UIViewController {
// MARK: Properties
// better to have in base class for the controller
var exampleModelService: ExampleModelService = ExampleModelService()
// MARK: Life Cycle Methods
override func viewDidLoad() {
super.viewDidLoad()
exampleModelService.GetExampleModelList(completion: { [weak self] (response) in
// model available here
})
}
}
Basically, you need to conform Codable protocol in your model classes, for this you need to implement 2 methods, one for code your model and another for decode your model from JSON
func encode(to encoder: Encoder) throws
required convenience init(from decoder: Decoder) throws
After that you will be able to use JSONDecoder class provided by apple to decode your JSON, and return an array (if were the case) or an object of your model class.
class ExampleModel: Codable {
var commentId : String?
var content : String?
//if your JSON keys are different than your property name
enum CodingKeys: String, CodingKey {
case commentId = "CommentId"
case content = "Content"
}
}
Then using JSONDecoder you can get your model array like this
do {
var arrayOfOrders : [ExampleModel] = try JSONDecoder().decode([ExampleModel].self, from: dataNew)
}
catch {
}
First of all, I can recommend you to use this application -quicktype- for turning json file to class or struct (codable) whatever you want. enter link description here.
After that you can create a generic function to get any kind of codable class and return that as a response.
func taskHandler<T:Codable>(type: T.Type, useCache: Bool, urlRequest: URLRequest, completion: #escaping (Result<T, Error>) -> Void) {
let task = URLSession.shared.dataTask(with: urlRequest) { (data, response, error) in
if let error = error {
print("error : \(error)")
}
if let data = data {
do {
let dataDecoded = try JSONDecoder().decode(T.self, from: data)
completion(.success(dataDecoded))
// if says use cache, let's store response data to cache
if useCache {
if let response = response as? HTTPURLResponse {
self.storeDataToCache(urlResponse: response, urlRequest: urlRequest, data: data)
}
}
} catch let error {
completion(.failure(error))
}
} else {
completion(.failure(SomeError))
}
}
task.resume()
}

How do I decode JSON(JSON Web Token) in Swift 4?

How do I decode this json in Swift 4, with decoder?
I want to be able to get the "token" by itself, so I can store it in the Keychain.
{
"success":true,
"token":"***"
,
"user": {
"id": "59f0ec6d5479390345980cc8",
"username": "john",
"email": "john#gmail.com"
}
}
I have tried this, but it doesn't print anything.
let session = URLSession.shared
let task = session.dataTask(with: request) { (data, _, _) in
guard let data = data else { return }
do {
let jsonwt = try JSONDecoder().decode(JWT.self, from: data)
print(jsonwt.token)
} catch {}
}
task.resume()
}
I can put this after the catch, but that gets the whole json, and I don't want that.
print(String(data: data, encoding: .utf8)!)
Here are the structures. I think this is where the problem lies.
struct User: Decodable {
let id: String
let username: String
let email: String
}
struct JWT: Decodable {
let success: String
let token: String
let user: User
}
Works like this:
struct User : Codable
{ var id : String
}
struct JWT : Codable
{ var success : Bool
var token : String
var user :User
}
let json = """
{ \"success\" : true,
\"token\" : \"***\",
\"user\":
{ \"id\": \"59f0ec6d5479390345980cc8\",
\"username\": \"john\",
\"email\": \"john#gmail.com\"
}
}
"""
let decoder = JSONDecoder()
let jwt = try decoder.decode(JWT.self, from: json.data(using: .utf8)!)
print ("token: \(jwt.token)")
Here's some playground code that demonstrates the code for parsing JSON in Swift:
//: Playground - noun: a place where people can play
import UIKit
import XCTest
import PlaygroundSupport
let json = """
{
"success":true,
"token":"***"
,
"user": {
"id": "59f0ec6d5479390345980cc8",
"username": "john",
"email": "john#gmail.com"
}
}
""".data(using: .utf8)!
do {
if let data = try JSONSerialization.jsonObject(with: json, options: .allowFragments) as? [String:Any], let token = data["token"] {
print("token is \(token)")
}
} catch _ {
print("Failed to decode JSON")
}

POST Json to API with Alamofire?

I want to post a JSON object I create in my service class and pass to the networkService.
This is my network service, but i get an error of
Value of type '[String : Any]' has no member 'data'
on the line: let jsonData = json.data(using: .utf8, allowLossyConversion: false)!
func request(json: [String:Any]) {
let url = URL(string: urlString)!
let jsonData = json.data(using: .utf8, allowLossyConversion: false)!
var request = URLRequest(url: url)
request.httpMethod = HTTPMethod.post.rawValue
request.setValue("application/json; charset=UTF-8", forHTTPHeaderField: "Content-Type")
request.httpBody = jsonData
Alamofire.request(request).responseJSON {
(response) in
print(response)
}
}
The idea being I pass in my JSON when i call the func via the func parameter.
This is the JSON object passed in:
func loginUser(data: Array<String>, deviceToken: String) {
// create JSON
let json = [ "login-email" : data[0],
"login-password" : data[1],
"login-secret" : "8A145C555C43FBA5",
"devicetoken" : deviceToken
]
networkManager.request(json: json)
}
Then I convert and send it to the API (urlString)
Any idea if/why this isnt working?
THanks
Updated revision:
func request(json: [String:Any]) {
let url = URL(string: urlString)!
do {
let jsonData = try JSONSerialization.data(withJSONObject: json, options:[])
var request = URLRequest(url: url)
request.httpMethod = HTTPMethod.post.rawValue
request.setValue("application/json; charset=UTF-8", forHTTPHeaderField: "Content-Type")
request.httpBody = jsonData
Alamofire.request(request).responseJSON {
(response) in
print(response)
}
} catch {
print("Failed to serialise and send JSON")
}
}
update: added my code to make a call with completion question:
func sendLoginRequest() {
let userLogin = UserService.init(loginEmail: userEmail, loginPassword: userPassword, loginSecret: loginSecret, deviceToken: deviceToken)
networkService.logUserIn(request: userLogin) { (<#JSON?#>, <#NSError?#>) in
<#code#>
}
}
edit: Updated Payload Shot:
edit 2: mapping issue example:
init?(_ json: JSON) {
// Map API Key from top level
guard let apiKey = json["apikey"].string else { return nil }
// Map User at user level
guard let userDataArray = json["user"].array else {
fatalError("user data array NOT FOUND")
}
print("USER DATA IS \(userDataArray)")
// assign user
for child in userDataArray {
guard let userID = child["id"].int,
let userEmail = child["email"].string,
let lastName = child["lastname"].string,
let firstName = child["firstname"].string,
let company = child["company"].string,
let userImage = child["image"].string,
let jobTitle = child["jobtitle"].string
else { return nil
}
}
// Assign to model properties
self.apiKey = apiKey
self.userEmail = userEmail
self.lastName = lastName
self.firstName = firstName
self.company = company
self.userImage = userImage
self.jobTitle = jobTitle
self.userID = userID
}
I just show how I work with this.
You don't have to convert your parameters to JSON. It's code from Alamofire.
/// A dictionary of parameters to apply to a `URLRequest`.
public typealias Parameters = [String: Any]
Use this method instead of your:
Alamofire.request(url, method: method, parameters: parameters, encoding: encoding, headers: customHeaders)
Try this:
Instead of your request.httpBody = jsonData you can pass your json in parameters.
Your whole code will be:
func request(json: [String:Any]) {
Alamofire.request(urlString, method: .post, parameters: json, encoding: JSONEncoding.default).responseJSON {
(response) in
print(response)
}
}
If you are interested in my approach:
func makePick(request: MakePickRequest, completionHandler: #escaping APICompletionHandler) {
let parameters = request.converToParameters()
Alamofire.request(Endpoints.makePick, method: .post, parameters: parameters, encoding: JSONEncoding.default).responseJSON { response in
self.handleResponse(response: response, completionHandler: completionHandler)
}
}
Request:
struct MakePickRequest: GeneralRequest {
let eventId: Int64
let sportId: String
let pickType: PickType
let betType: BetType
let amount: Int
func converToParameters() -> [String : String] {
return ["event_id": String(eventId), "sport_id": sportId,
"pick_type": pickType.rawValue, "bet_type": betType.rawValue,
"amount": String(amount)]
}
}
Structure with endpoints:
struct Endpoints {
// Development baseURL
static let baseURL = "http://myurl/"
private static let apiVersion = "api/v1/"
static var fullPath: String {
return "\(baseURL)\(apiVersion)"
}
// MARK: - User endpoints (POST)
static var login: String {
return "\(fullPath)users/login"
}
static var signUp: String {
return "\(fullPath)users/signup"
}
...
}
Outside of any class (but import SwiftyJSON is obligatory):
typealias APICompletionHandler = (_ data: JSON?, _ error: NSError?) -> Void
Handle response:
private func handleResponse(response: DataResponse<Any>, completionHandler: APICompletionHandler) {
self.printDebugInfo(response)
switch response.result {
case .success(let value):
self.handleJSON(data: value, handler: completionHandler)
case .failure(let error):
print(error)
completionHandler(nil, error as NSError?)
}
}
private func handleJSON(data: Any, handler: APICompletionHandler) {
let json = JSON(data)
let serverResponse = GeneralServerResponse(json)
if (serverResponse?.status == .ok) {
handler(serverResponse?.data, nil)
} else {
handler(nil, self.parseJsonWithErrors(json))
}
}
GeneralServerResponse (depends on your server API):
import SwiftyJSON
final class GeneralServerResponse {
let data: JSON
let status: Status
init?(_ json: JSON) {
guard let status = json["status"].int else {
return nil
}
self.status = Status(status)
self.data = json["data"]
}
enum Status {
case ok
case error
case unauthorized
init(_ input: Int) {
if input >= 200 && input < 400 {
self = .ok
} else if input == 403 {
self = .unauthorized
} else {
self = .error
}
}
}
}
My actual example of usage.
This is outside:
func +=<K, V> ( left: inout [K : V], right: [K : V]) { for (k, v) in right { left[k] = v } }
Example of request:
func makePick(request: MakePickRequest, completionHandler: #escaping APICompletionHandler) {
var parameters = ["auth_token": Preferences.getAuthToken()]
parameters += request.converToParameters()
manager.apiRequest(url: Endpoints.makePick, method: .post, parameters: parameters, encoding: JSONEncoding.default).responseJSON { response in
self.handleResponse(response: response, completionHandler: completionHandler)
}
}
SessionManager extension to add headers for all requests:
extension SessionManager {
func apiRequest(url: URLConvertible, method: HTTPMethod, parameters: Parameters? = nil, encoding: ParameterEncoding, headers: HTTPHeaders? = nil) -> DataRequest {
var customHeaders: HTTPHeaders = ["api-key" : "1wFVerFztxzhgt"]
if let headers = headers {
customHeaders += headers
}
return request(url, method: method, parameters: parameters, encoding: encoding, headers: customHeaders)
}
}
In APIManager class:
private let manager: SessionManager
init() {
manager = Alamofire.SessionManager.default
}
Call example:
apiClient.makePick(request: request) { data, error in
if let error = error {
print(error.localizedDescription)
return
}
if let data = data {
// data is a JSON object, here you can parse it and create objects
}
}
Example of class:
import SwiftyJSON
final class MyClass {
let id: Int
let username: String
let parameter: Double
init?(_ json: JSON) {
guard let id = json["id"].int, let username = json["username"].string,
let parameter = json["parameter"].double else {
return nil
}
self.id = id
self.username = username
self.parameter = parameter
}
}

Implementing google translation api in swift 3 iOS

Hi i am new to iOS development and i am trying to implement google translation API within my app. I found some sample code online from GitHub https://github.com/prine/ROGoogleTranslate. I downloaded the sample code and followed the instructions provided by obtaining an api key from google cloud translate and placing it within the code however the code is not working, iv looked at the comments on the GitHub site and found that it has
worked for other developers. I really don't know what i am doing wrong in the code.
ROGoogleTranslateParams.swift
import Foundation
public struct ROGoogleTranslateParams {
public init() {
}
public init(source:String, target:String, text:String) {
self.source = source
self.target = target
self.text = text
}
public var source = "de"
public var target = "en"
public var text = "Hallo"
}
/// Offers easier access to the Google Translate API
open class ROGoogleTranslate {
/// Store here the Google Translate API Key
public var apiKey = "YOUR_API_KEY"
///
/// Initial constructor
///
public init() {
}
///
/// Translate a phrase from one language into another
///
/// - parameter params: ROGoogleTranslate Struct contains all the needed parameters to translate with the Google Translate API
/// - parameter callback: The translated string will be returned in the callback
///
open func translate(params:ROGoogleTranslateParams, callback:#escaping (_ translatedText:String) -> ()) {
guard apiKey != "" else {
print("Warning: You should set the api key before calling the translate method.")
return
}
if let urlEncodedText = params.text.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed) {
if let url = URL(string: "https://translation.googleapis.com/language/translate/v2?key=\(self.apiKey)&q=\(urlEncodedText)&source=\(params.source)&target=\(params.target)&format=text") {
let httprequest = URLSession.shared.dataTask(with: url, completionHandler: { (data, response, error) in
guard error == nil else {
print("Something went wrong: \(error?.localizedDescription)")
return
}
if let httpResponse = response as? HTTPURLResponse {
guard httpResponse.statusCode == 200 else {
if let data = data {
print("Response [\(httpResponse.statusCode)] - \(data)")
}
return
}
do {
// Pyramid of optional json retrieving. I know with SwiftyJSON it would be easier, but I didn't want to add an external library
if let data = data {
if let json = try JSONSerialization.jsonObject(with: data, options:JSONSerialization.ReadingOptions.mutableContainers) as? NSDictionary {
if let jsonData = json["data"] as? [String : Any] {
if let translations = jsonData["translations"] as? [NSDictionary] {
if let translation = translations.first as? [String : Any] {
if let translatedText = translation["translatedText"] as? String {
callback(translatedText)
}
}
}
}
}
}
} catch {
print("Serialization failed: \(error.localizedDescription)")
}
}
})
httprequest.resume()
}
}
}
}
ViewController.swift
import UIKit
class ViewController: UIViewController {
#IBOutlet var text:UITextField!
#IBOutlet var fromLanguage:UITextField!
#IBOutlet var toLanguage:UITextField!
#IBOutlet var translation:UILabel!
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.
}
#IBAction func translate(_ sender: UIButton) {
let translator = ROGoogleTranslate()
translator.apiKey = "YOUR_API_KEY" // Add your API Key here
var params = ROGoogleTranslateParams()
params.source = fromLanguage.text ?? "de"
params.target = toLanguage.text ?? "en"
params.text = text.text ?? "Hallo"
translator.translate(params: params) { (result) in
DispatchQueue.main.async {
self.translation.text = "\(result)"
}
}
}
}
These are classes are used.
The result i get when i press the 'translate' button is the following:
Response [403] - 355 bytes
your help is appreciated. The code is available to download from the url provided
Thank you
I'm the author of the library you mentioned above :). I guess you get the 403 because your Google Api Account is not yet activated correctly. Google has changed the policy of the Translation api and its not free anymore. So you problably didn't add the credit card informations in the Api account and therefor get the 403 error?
Try this "POST" method function not the 'Get' method as you implemented -
open func translateTest(params: GoogleAITranslateParams, targetLanguage: String, callback:#escaping (_ translatedText:String) -> ()) {
guard apiKey != "" else {
return
}
var request = URLRequest(url: URL(string: "https://translation.googleapis.com/language/translate/v2?key=\(self.apiKey)")!)
request.httpMethod = "POST"
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue(Bundle.main.bundleIdentifier ?? "", forHTTPHeaderField: "X-Ios-Bundle-Identifier")
let jsonRequest = [
"q": params.text,
"source": "en",
"target": targetLanguage,
"format": "text"
] as [String : Any]
if let jsonData = try? JSONSerialization.data(withJSONObject: jsonRequest, options: .prettyPrinted) {
request.httpBody = jsonData
let task: URLSessionDataTask = URLSession.shared.dataTask(with: request) { (data, response, error) in
guard error == nil else {
print("Something went wrong: \(String(describing: error?.localizedDescription))")
return
}
if let httpResponse = response as? HTTPURLResponse {
guard httpResponse.statusCode == 200 else {
if let data = data {
print("Response [\(httpResponse.statusCode)] - \(data)")
}
return
}
do {
if let data = data {
if let json = try JSONSerialization.jsonObject(with: data, options:JSONSerialization.ReadingOptions.mutableContainers) as? NSDictionary {
if let jsonData = json["data"] as? [String : Any] {
if let translations = jsonData["translations"] as? [NSDictionary] {
if let translation = translations.first as? [String : Any] {
if let translatedText = translation["translatedText"] as? String {
callback(translatedText)
}
}
}
}
}
}
} catch {
print("Serialization failed: \(error.localizedDescription)")
}
}
}
task.resume()
}
}

Resources