-1

i am trying to save data from my app to my database, but i can not find, why it is not working. I was using tutorial from youtube, but it does not work for me.

this is my php file:

<?php
$host="localhost";
$user="admin_aplikace";
$password="A9Qgez0zX3";

$connection = mysql_connect($host,$user,$password);

$name = $_POST["a"];
$age = $_POST["b"];


if(!$connection){
    die("Connection Failed");
}
else{
    $dbconnect = @mysql_select_db("admin_aplikace", $connection);

    if(!$dbconnect){
        die("Could not connect to Database");
    }
    else{
        $query = "INSERT INTO `zkouska2`(`bla`, `prvni`, `druha`) VALUES ([value-1],[$name],[$age])";
        mysql_query($query, $connection) or die(mysql_error());

        echo "Successfully added.";
        echo $query;
    }
}

?>

and this is my swift code:

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var age: UITextField!

@IBOutlet weak var name: UITextField!


@IBAction func submit(_ sender: Any) {
    //put the link of the php file here. The php file connects the mysql and swift
    let request = NSMutableURLRequest(url: NSURL(string: "http://myserver/zkouska2.php")! as URL)
    request.httpMethod = "POST"

    let postString = "a=\(name.text!)&b=\(age.text!)"


    request.httpBody = postString.data(using: String.Encoding.utf8)

    let task = URLSession.shared.dataTask(with: request as URLRequest) {
        data, response, error in

        if error != nil {
            print("error=\(String(describing: error))")
            return
        }

        print("response = \(String(describing: response))")

        let responseString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)
        print("responseString = \(String(describing: responseString))")
    }
    task.resume() 

}

and my errors:

NSLocalizedDescription=The resource could not be loaded because the App Transport Security policy requires the use of a secure connection.}) error=Optional(Error Domain=NSURLErrorDomain Code=-1022 "The resource could not be loaded because the App Transport Security policy requires the use of a secure connection." UserInfo={NSUnderlyingError=0x60000005faa0 {Error Domain=kCFErrorDomainCFNetwork Code=-1022 "(null)"}, NSErrorFailingURLStringKey=http://servis.dronysitmp.cz/zkouska2.php, NSErrorFailingURLKey=http://servis.dronysitmp.cz/zkouska2.php, NSLocalizedDescription=The resource could not be loaded because the App Transport Security policy requires the use of a secure connection.})

  • Errors? We love when we get the errors. – Anis Alibegić Aug 30 '17 at 08:11
  • @Spectarion errors are edited – Josef Navratil Aug 30 '17 at 08:14
  • Not PHP developer but might be issue in Confusion in VALUES ([value-1],[$name],[$age] – Govaadiyo Aug 30 '17 at 08:15
  • "The resource could not be loaded because the App Transport Security policy requires the use of a secure connection.": Look for "App Transport Security ". You should edit your Info.plist. – Larme Aug 30 '17 at 08:22
  • 1
    Possible duplicate of [How do I load an HTTP URL with App Transport Security enabled in iOS 9?](https://stackoverflow.com/questions/30731785/how-do-i-load-an-http-url-with-app-transport-security-enabled-in-ios-9) – Larme Aug 30 '17 at 08:23
  • Possible duplicate of [The resource could not be loaded because the App Transport Security policy requires the use of a secure connection](https://stackoverflow.com/questions/32631184/the-resource-could-not-be-loaded-because-the-app-transport-security-policy-requi) – Tamás Sengel Aug 30 '17 at 08:23
  • It other words, you need `https`. – Anis Alibegić Aug 30 '17 at 08:23
  • @Spectarion when i added https to my swift code, it wrote again error. But when i have http instead of https and i modified my info.plist, no errors i have, but again it doesnt work – Josef Navratil Aug 30 '17 at 08:33
  • Then you should have another error right? Which one now? – Larme Aug 30 '17 at 08:49
  • @Larme now i get only this response = Optional( { URL: http://servis.dronysitmp.cz/zkouska2.php } { status code: 500, headers { Connection = "keep-alive"; "Content-Length" = 0; "Content-Type" = "text/html; charset=UTF-8"; Date = "Wed, 30 Aug 2017 09:01:45 GMT"; "Keep-Alive" = "timeout=60"; Server = nginx; } }) responseString = Optional() – Josef Navratil Aug 30 '17 at 09:02
  • "status code: 500" That's a whole new error!. Your issue seems to be on your server. – Larme Aug 30 '17 at 09:07
  • @Larme You mean database or server, where my php file is? – Josef Navratil Aug 30 '17 at 09:12

1 Answers1

1

Please add following in your xcode project info.plist file:

<key>NSAppTransportSecurity</key>
    <dict>
        <key>NSAllowsArbitraryLoads</key>
        <true/>
    </dict>

This will remove error/crash on xcode for transport security error. This will allow you to run all non https servers to communicate.

refer Transport security has blocked a cleartext HTTP

https://developer.apple.com/library/content/documentation/General/Reference/InfoPlistKeyReference/Articles/CocoaKeys.html

Devanshu Saini
  • 765
  • 5
  • 24