I am pretty much a beginner in Swift and I am trying to pass a value to make a call to an API with the value being from an input field.
The API is given below. How do I write code to pass value for ZIP_CODE from a text field?
https://iaspub.epa.gov/enviro/efservice/getEnvirofactsUVDAILY/ZIP/ZIP_CODE/JSON
#IBOutlet var zipLabel: UILabel!
#IBOutlet var zipInput: UITextField!
#IBOutlet var jsondataLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
getJSON()
}
#IBAction func btnAction() {
zipLabel.text = zipInput.text
}
func getJSON(){
let url = NSURL(string :"https://iaspub.epa.gov/enviro/efservice/getEnvirofactsUVDAILY/ZIP/10001/JSON")
let request = URLRequest(url: url as! URL )
let config = URLSessionConfiguration.default
let session = URLSession(configuration: config)
let task = session.dataTask(with: request){(data, response, error ) -> Void in
if error == nil{
DispatchQueue.main.async(execute: {
let jsondata = JSON(data : data!)
print(jsondata)
print("-----")
print(jsondata[0]["UV_INDEX"])
let result = jsondata[0]["UV_INDEX"].stringValue
self.jsondataLabel.text = result
})
}else{
print("There was an error")
}
}
task.resume()
}
func getJSON(_ zipcode : Int){
let url = NSURL(string :"https://iaspub.epa.gov/enviro/efservice/getEnvirofactsUVDAILY/ZIP/\(zipcode)/JSON")
let request = URLRequest(url: url as! URL )
let config = URLSessionConfiguration.default
let session = URLSession(configuration: config)
let task = session.dataTask(with: request){(data, response, error ) -> Void in
if error == nil{
DispatchQueue.main.async(execute: {
let jsondata = JSON(data : data!)
print(jsondata)
print("-----")
print(jsondata[0]["UV_INDEX"])
let result = jsondata[0]["UV_INDEX"].stringValue
self.jsondataLabel.text = result
})
}else{
print("There was an error")
}
}
task.resume()
}
If your API gives you the response on GET Request you can use the following code -
#IBOutlet var zipLabel: UILabel!
#IBOutlet var zipInput: UITextField!
#IBOutlet var jsondataLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func btnAction() {
zipLabel.text = zipInput.text
getJSON(zipcode: zipInput.text)
}
func getJSON(zipcode: String){
let url = NSURL(string :"https://iaspub.epa.gov/enviro/efservice/getEnvirofactsUVDAILY/ZIP/10001/JSON&zipcode="+zipcode)
let request = URLRequest(url: url as! URL )
let config = URLSessionConfiguration.default
let session = URLSession(configuration: config)
let task = session.dataTask(with: request){(data, response, error ) -> Void in
if error == nil{
DispatchQueue.main.async(execute: {
let jsondata = JSON(data : data!)
print(jsondata)
print("-----")
print(jsondata[0]["UV_INDEX"])
let result = jsondata[0]["UV_INDEX"].stringValue
self.jsondataLabel.text = result
})
}else{
print("There was an error")
}
}
task.resume()
}
But Please note that this code won't work if your API gives you the response on POST request.
Related
I an application that suppose to get weather data in certain cities and I have a dropdown and based on the dropdown selection, an api should be called (based on city name), when I tap on a city for the first time the app runs it returns nil and if I tap on another city it returns data based on the first selection, I have debugged and traced the code line by line however the URL is correct and the everything seems to be in order, any help? here is my code:
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var tempraturevalue: UILabel!
#IBOutlet weak var cityname: UILabel!
#IBOutlet weak var temprature: UIImageView!
#IBOutlet var cityButtons: [UIButton]!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
#IBAction func handleSelection(_ sender: UIButton) {
cityButtons.forEach{ (button) in
UIView.animate(withDuration: 0.3) {
button.isHidden = !button.isHidden
self.view.layoutIfNeeded()
}
}
}
enum Cities:String {
case amman = "Amman"
case azzerqa = "Az zerqa"
case irbid = "Irbid"
case aqaba = "Aqaba"
}
var tempraturevalueData:Double = 0.0
var cityNameData:String = ""
#IBAction func cityTapped(_ sender: UIButton) {
guard let title = sender.currentTitle, let City = Cities(rawValue: title)
else {
return
}
var city:String
switch City {
case .amman:
city = "Amman"
case .azzerqa:
city = "zerqa"
case .irbid:
city = "Irbid"
case .aqaba:
city = "Aqaba"
}
let url = URL(string: "https://api.weatherapi.com/v1/current.json?key={key}&q=\(city)")
guard url != nil else {
print("error creating URL Object")
return
}
var request = URLRequest(url: url!, cachePolicy: .useProtocolCachePolicy , timeoutInterval: 10)
let headers = ["Content-Type" : "application/json"]
request.allHTTPHeaderFields = headers
request.httpMethod = "GET"
let session = URLSession.shared
let dataTask = session.dataTask(with: request, completionHandler: {(data, response, error) in
if error == nil && data != nil {
do {
let decoder = JSONDecoder()
do {
let weatherdatadecoded = try decoder.decode(WeatherData.self, from: data!)
self.tempraturevalueData = weatherdatadecoded.current?.temp_c ?? 0.1
self.cityNameData = weatherdatadecoded.location?.name ?? "no city found"
}
catch {
print(error)
}
}
catch {
print(error.localizedDescription)
}
}
})
dataTask.resume()
print(self.cityNameData)
print(self.tempraturevalueData)
}
}
Im New to Swift and I'm Integrating PayUmoney IOS SDK in swift, Im getting trouble When I'm doing in live its showing hash mismatch (Hash mismatch1) If I'm doing in test its showing invalid merchant key (Invalid key) I struck here from 2 weeks Did so many things and didn't get any solution for this can any one help it would be awesome. Below is my code, Thank you in Advance.
var params : PUMRequestParams = PUMRequestParams.shared()
var utils : Utils = Utils()
params.environment = PUMEnvironment.test;
params.firstname = txtFldName.text;
params.key = "bZf4AOjj";
params.merchantid = "5745303";
params.logo_url = "";
params.productinfo = "Product Info";
params.email = txtFldEmail.text;
params.phone = "";
params.surl = "https://www.payumoney.com/mobileapp/payumoney/success.php";
params.furl = "https://www.payumoney.com/mobileapp/payumoney/failure.php";
if(params.environment == PUMEnvironment.test){
generateHashForProdAndNavigateToSDK()
}
else{
calculateHashFromServer()
}
// assign delegate for payment callback.
params.delegate = self;
}
func generateHashForProdAndNavigateToSDK() -> Void {
let txnid = params.txnid!
let hashSequence : NSString = "\(params.key)|\(txnid)|\(params.amount)|\(params.productinfo)|\(params.firstname)|\(params.email)|||||||||||2uIsGhXWVw" as NSString
let data :NSString = utils.createSHA512(hashSequence as String!) as NSString
params.hashValue = data as String!;
startPaymentFlow();
}
// MARK:HASH CALCULATION
func prepareHashBody()->NSString{
return "SHA-512key=\(params.key!)&amount=\(params.amount!)&txnid=\(params.txnid!)&productinfo=\(params.productinfo!)&email=\(params.email!)&firstname=\(params.firstname!)" as NSString;
}
func calculateHashFromServer(){
let config = URLSessionConfiguration.default // Session Configuration
let session = URLSession(configuration: config) // Load configuration into Session
let url = URL(string: "https://test.payumoney.com/payment/op/v1/calculateHashForTest")!
var request = URLRequest(url: url)
request.httpBody = prepareHashBody().data(using: String.Encoding.utf8.rawValue)
request.httpMethod = "POST"
let task = session.dataTask(with: request, completionHandler: {
(data, response, error) in
if error != nil {
print(error!.localizedDescription)
} else {
do {
if let json = try JSONSerialization.jsonObject(with: data!, options: []) as? [String: Any]{
print(json)
let status : NSNumber = json["status"] as! NSNumber
if(status.intValue == 0)
{
self.params.hashValue = json["result"] as! String!
OperationQueue.main.addOperation {
self.startPaymentFlow()
}
}
else{
OperationQueue.main.addOperation {
self.showAlertViewWithTitle(title: "Message", message: json["message"] as! String)
}
}
}
} catch {
print("error in JSONSerialization")
}
}
})
task.resume()
}
Hello Vinny do with webview its working for me. Before I also used this PayUmoney IOS SDK but faced so many problems so based on objective-c I did this so I think its useful to you. create a weak var webview and create class UIwebviewdelegate
class PayumoneyViewController: UIViewController, UIWebViewDelegate, UIAlertViewDelegate {
#IBOutlet weak var Webview: UIWebView!
and for test use below credentials
//test
var merchantKey = "40747T"
var salt = "ur salt"
var PayUBaseUrl = "https://test.payu.in"
For live
//Production
var merchantKey = “xxxxxx”
var salt = “xxxxx”
var PayUBaseUrl = "https://secure.payu.in"
let productInfo = “Myapp” //It can be Project name or anything else
let firstName = “Santoshi” //Details of user whose is purchasing order
let email = “santoshi#app.com" //Details of user whose is purchasing order
let phone = "xxxxxxxxx" //Details of user whose is purchasing order
let sUrl = "www.google.com" //By this URL we match whether payment got success or failure
let fUrl = "www.google.com" //By this URL we match whether payment got success or failure
let service_provider = "payu_paisa"
var txnid1: String! = "" //Its an unique id which can give order a specific order number.
let totalPriceAmount = "1.0"
Above viewdidload do like this
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(true)
initPayment()
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(true)
}
In viewdidload do like this
override func viewDidLoad() {
super.viewDidLoad()
Webview.delegate = self
// Do any additional setup after loading the view.
}
Create payment and Generate Hash key
func initPayment() {
txnid1 = “Myapp\(String(Int(NSDate().timeIntervalSince1970)))"
//Generating Hash Key
let hashValue = String.localizedStringWithFormat("%#|%#|%#|%#|%#|%#|||||||||||%#",merchantKey,txnid1,totalPriceAmount,productInfo,firstName,email,salt)
let hash = self.sha1(string: hashValue)
let postStr = "txnid="+txnid1+"&key="+merchantKey+"&amount="+totalPriceAmount+"&productinfo="+productInfo+"&firstname="+firstName+"&email="+email+"&phone="+phone+"&surl="+sUrl+"&furl="+fUrl+"&hash="+hash+"&service_provider="+service_provider
let url = NSURL(string: String.localizedStringWithFormat("%#/_payment", PayUBaseUrl))
let request = NSMutableURLRequest(url: url! as URL)
do {
let postLength = String.localizedStringWithFormat("%lu",postStr.characters.count)
request.httpMethod = "POST"
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Current-Type")
request.setValue(postLength, forHTTPHeaderField: "Content-Length")
request.httpBody = postStr.data(using: String.Encoding.utf8)
Webview.loadRequest(request as URLRequest)
}
catch let error as NSError
{
print(error)
}
}
Finally Do this
func sha1(string:String) -> String {
let cstr = string.cString(using: String.Encoding.utf8)
let data = NSData(bytes: cstr, length: string.characters.count)
var digest = [UInt8](repeating: 0, count:Int(CC_SHA512_DIGEST_LENGTH))
CC_SHA512(data.bytes, CC_LONG(data.length), &digest)
let hexBytes = digest.map { String(format: "%02x", $0) }
return hexBytes.joined(separator: "")
}
func webViewDidFinishLoad(_ webView: UIWebView) {
let requestURL = self.Webview.request?.url
let requestString:String = (requestURL?.absoluteString)!
if requestString.contains("https://www.payumoney.com/mobileapp/payumoney/success.php") {
print("success payment done")
}else if requestString.contains("https://www.payumoney.com/mobileapp/payumoney/failure.php") {
print("payment failure")
}
}
func webView(_ webView: UIWebView, didFailLoadWithError error: Error) {
let requestURL = self.Webview.request?.url
print("WebView failed loading with requestURL: \(requestURL) with error: \(error.localizedDescription) & error code: \(error)")
if error._code == -1009 || error._code == -1003 {
showAlertView(userMessage: "Please check your internet connection!")
}else if error._code == -1001 {
showAlertView(userMessage: "The request timed out.")
}
}
func showAlertView(userMessage:String){
}
I have Faced Same problem and i have got solution of this problem.
In my code this line generate optional values --> let hashSequence : NSString = "(params.key!)|(txnid)|(params.amount!)|(params.productinfo!)|(params.firstname!)|(params.email!)|||||||||||(params.merchantid!)" as NSString
remove optional from the values.
I'm trying to login and I'm doing tests right now and I'm trying when the log is correct I changed the title of the button but I get this error: value of type '(Any) -> ()' has no member ' SetTitle '. Then I leave you the code and a screenshot.
I need help because I'm blocked and I can not continue. All help will be appreciated. Thank you.
And this is my code
import UIKit
class LogViewController: UIViewController {
#IBOutlet weak var _username: UITextField!
#IBOutlet weak var _password: UITextField!
#IBAction func _login_button(_ sender: Any) {
let username = _username.text
let password = _password.text
if(username == "" || password == "")
{
return
}
DoLogin(username!, password!)
}
func DoLogin(_ user:String, _ psw:String)
{
let url = URL(string: "http://localhost/bill/userLogin.php")
let session = URLSession.shared
let request = NSMutableURLRequest(url: url!)
request.httpMethod = "POST"
let paramToSend = "username=" + user + "&password=" + psw
request.httpBody = paramToSend.data(using: String.Encoding.utf8)
let task = session.dataTask(with: request as URLRequest, completionHandler: {
(data, response, error) in
guard let _:Data = data else
{
return
}
let json:Any?
do
{
json = try JSONSerialization.jsonObject(with: data!, options: [])
}
catch
{
return
}
guard let server_response = json as? NSDictionary else
{
return
}
if let data_block = server_response["data"] as? NSDictionary
{
if let session_data = data_block["session"] as? String
{
let preferences = UserDefaults.standard
preferences.set(session_data, forKey: "session")
DispatchQueue.main.async {
execute:self.LoginDone()
}
}
}
})
task.resume()
}
func LoginDone()
{
_username.isEnabled = false
_password.isEnabled = false
_login_button.setTitle("Logout", for: .normal)
}
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
You have issue that is connected with _login_button because it is #IBAction not #IBOutlet.
For solving of this issue drag outlet from storyboard and connect it with proper file.
And than set property of the button.
Example.
#IBOutlet weak var _login_button_outlet: UIButton!
func LoginDone()
{
_username.isEnabled = false
_password.isEnabled = false
_login_button_outlet.setTitle("Logout", for: .normal)
}
hi you trying to set the title to the _login_button which not the button its the #IBAction method so for setting the title you have to create #IBOutlet weak var _login_button: UIButton!.
i tried build login authentication without oauth is work and then i build other project only oauth code for request roken is work too, and now i want to make project authentication login with oauth swift. im really stuck what must i do.
this is my code im trying :
import UIKit
import OAuthSwift
class ViewController: UIViewController {
#IBOutlet weak var passwordTxt: UITextField!
#IBOutlet weak var phonenumberTxt: UITextField!
var oauthswift: OAuth1Swift?
let dataShared = UserDefaults.standard
let fileUrl = NSURL(string: "8villages://app/oauth-callback")
override func viewDidLoad() {
super.viewDidLoad()
requestToken()
}
#IBAction func doLogin(_ sender: Any) {
let phonenumber = phonenumberTxt.text
let password = passwordTxt.text
if(phonenumber == "" || password == "")
{
return
}
login(phonenumber!, password!)
}
func login(_ number:String, _ pswrd:String) {
let url = URL(string: "https://accounts.8villages.com/authentication")
let session = URLSession.shared
let request = NSMutableURLRequest(url: url!)
request.httpMethod = "POST"
let paramToSend = "identifier=" + number + "&password=" + pswrd
request.httpBody = paramToSend.data(using: String.Encoding.utf8)
let task = session.dataTask(with: request as URLRequest, completionHandler: {
(data, response, error) in
guard let _:Data = data else
{
return
}
let json:Any?
do {
json = try JSONSerialization.jsonObject(with: data!, options: [])
}
catch {
return
}
guard let server_response = json as? NSDictionary else {
return
}
if let data_block = server_response["data"] as? NSDictionary {
if let session_data = data_block["session"] as? String {
let preference = UserDefaults.standard
preference.set(session_data, forKey: "session")
DispatchQueue.main.async(
execute:self.LoginDone
)//
}
}
})
task.resume()
}
func LoginDone() {
self.performSegue(withIdentifier: "loginView", sender: self)
}
func requestToken() {
print ("hei")
var is8VillagesInstalled: Bool = {
let appUrl = URL(string: "8villages://app")
return UIApplication.shared.canOpenURL(appUrl!)
}()
self.oauthswift = OAuth1Swift.init(consumerKey: "**********",
consumerSecret: "**********",
requestTokenUrl: "https://oauth.8villages.com/tokens/request-token",
authorizeUrl: "8villages://app/?oauth_token_secret=",
accessTokenUrl: "https://accounts.8villages.com/tokens/access-token")
self.oauthswift?.authorizeURLHandler = OAuthSwiftOpenURLExternally.sharedInstance
self.oauthswift?.allowMissingOAuthVerifier = true
self.oauthswift?.postOAuthRequestToken(
callbackURL: fileUrl as! URL,
success: { credentials, response, parameters in
print("credentials", credentials)
print("response =", response!)
print("parameters =", parameters)
// request ke api login
}, failure: { error in
print("the error is", error.description, error.errorCode)
});
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
so, i dont know how to authentication login with oauth. how to make them work.
I am trying to serialize a GET request then make a movie object, then appending that movie object to a movies array which I will use to show info on the UI.
I am new and have struggled with this problem for some time now :(
If you look at the self.movies?.append(movie) shouldnt that work? I dont see any reasons as to when i try to get the first item i get fatal error index out of bounds which means I the Array is not filled yet.... Dont know what i am doing wrong :(
import UIKit
class ViewController: UIViewController {
var movies:[Movie]? = []
#IBOutlet weak var uiMovieTitle: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
getMovieData()
print(self.movies?.count)
setUI()
}
#IBAction func yesBtn(_ sender: UIButton) {
print(movies?[5].title ?? String())
}
#IBAction func seenBtn(_ sender: UIButton) {
}
#IBAction func noBtn(_ sender: UIButton) {
}
#IBOutlet weak var moviePoster: UIImageView!
let urlString = "https://api.themoviedb.org/3/discover/movie?api_key=935f539acbfed4b9e5534ddeed3fb57e&language=en-US&sort_by=popularity.desc&include_adult=false&include_video=false&page=1&with_genres=12"
func getMovieData(){
//Set up URL
let todoEndPoint: String = "https://api.themoviedb.org/3/discover/movie?api_key=935f539acbfed4b9e5534ddeed3fb57e&language=en-US&sort_by=popularity.desc&include_adult=false&include_video=false&page=1&with_genres=12"
guard let url = URL(string: todoEndPoint) else {
print("Cant get URL")
return
}
let urlRequest = URLRequest(url: url)
//Setting up session
let config = URLSessionConfiguration.default
let session = URLSession.shared
//Task setup
let task = session.dataTask(with: urlRequest) { (data, URLResponse, error) in
//Checking for errors
guard error == nil else{
print("Error calling GET")
print(error)
return
}
//Checking if we got data
guard let responseData = data else{
print("Error: No data")
return
}
self.movies = [Movie]()
do{//If we got data, if not print error
guard let todo = try JSONSerialization.jsonObject(with: responseData, options:.mutableContainers) as? [String:AnyObject] else{
print("Error trying to convert data to JSON")
return
}//if data is Serializable, do this
if let movieResults = todo["results"] as? [[String: AnyObject]]{
//For each movieobject inside of movieresult try to make a movie object
for moviesFromJson in movieResults{
let movie = Movie()
//If all this works, set variables
if let title = moviesFromJson["title"] as? String, let movieRelease = moviesFromJson["release_date"] as? String, let posterPath = moviesFromJson["poster_path"] as? String, let movieId = moviesFromJson["id"] as? Int{
movie.title = title
movie.movieRelease = movieRelease
movie.posterPath = posterPath
movie.movieId = movieId
}
self.movies?.append(movie)
}
}
}//do end
catch{
print(error)
}
}
////Do Stuff
task.resume()
}
func setUI(){
//uiMovieTitle.text = self.movies![0].title
//print(self.movies?[0].title)
}
}
my Movie class:
import UIKit
class Movie: NSObject {
var title:String?
var movieRelease: String?
var posterPath:String?
var movieId:Int?
var movieGenre:[Int] = []
//public init(title:String, movieRelease:String, posterPath:String,movieId:Int) {
// self.movieId = movieId
//self.title = title
//self.movieRelease = movieRelease
//self.posterPath = posterPath
//self.movieGenre = [movieGenre]
//}
}
getMovieData calls the network asynchronously. Your viewDidLoad invokes this, then calls setUI() - but the networking is still ongoing when setUI is called.
Instead, call setUI when the networking is complete - after the self.movies?.append(movie) line. The UI code will need to happen on the main thread. So...
for moviesFromJson... // your existing code
...
self.movies?.append(movie)
}
// Refresh UI now movies have loaded.
DispatchQueue.main.async {
setUI()
}
import UIKit
class ViewController: UIViewController {
var movies:[Movie]? = []
#IBOutlet weak var uiMovieTitle: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
getMovieDataCall(completionHandler: {data, error in self. getMovieDataCallBack(data: data, error: error)})
}
func getMovieDataCallBack(data: Data?, error: Error?) {
if error == nil {
let dictionary = try! JSONSerialization.jsonObject(with: data!, options: .allowFragments) as! Dictionary<String, AnyObject>
//do your appending here and then call setUI()
print("dictionaryMovie \(dictionary)")
} else {
showAlertView("", error?.localizedDescription)
}
}
func getMovieDataCall(completionHandler: #escaping (Data?, Error?) -> Void)){
//Set up URL
let todoEndPoint: String = "https://api.themoviedb.org/3/discover/movie?api_key=935f539acbfed4b9e5534ddeed3fb57e&language=en-US&sort_by=popularity.desc&include_adult=false&include_video=false&page=1&with_genres=12"
guard let url = URL(string: todoEndPoint) else {
print("Cant get URL")
return
}
let urlRequest = URLRequest(url: url)
//Setting up session
let config = URLSessionConfiguration.default
let session = URLSession.shared
//Task setup
let task = session.dataTask(with: urlRequest) { (data, URLResponse, error) in
if error != nil {
NSLog("GET-ERROR", "=\(error)");
completionHandler(nil, error)
} else {
let dataString = String(data: data!, encoding: String.Encoding(rawValue: String.Encoding.utf8.rawValue))
print(dataString!)
completionHandler(data, nil)
}
task.resume()
}
func setUI(){
}