页面不会从iOS重定向到URLRequest

页面不会从iOS重定向到URLRequest

问题描述:

I'm trying to make a URLRequest from iOS with Swift and I send some data by the url to a kind of PHP server.

let url = URL(string: "http://www.mybadservice.com/redirectToAction.php?id=\(id)")


    print(stringUrl)

    var mutableRequest = URLRequest(url: url!)

    URLSession.shared.dataTask(with: mutableRequest) { (data, urlResponse, error) in
        if error != nil {
            print("Error running this ...")
            return
        }

        let statusCode = urlResponse as? HTTPURLResponse
        print("HTTP RESPONSE : \(statusCode?.statusCode)")
        let dataString = String(data: data!, encoding: .utf8)
        print("DATA : \(dataString)")
    }.resume()

When I send the data to redirectToAction.php it should gets the data using GET, makes a url and redirect to another page using javascript and this page does some work.

<html>
<head>  
    <title></title>
</head>
<body>


    <script type="text/javascript">    
        location.href ="./DoSomeWork.php?id=passedId"; /*Where pasedId is the paramether I send by url, I don't now how exactly passedId is linked together from php to javascript because I have not access to the code */
    </script>
</body>

Now it does not works just when I make a urlResponse. I tried to copy the url generated by the app and paste it in the browser and it works perfectly. Also the request in the app returns a 200 http code and prints the whole html page.

This will not work, because php is server side and the server can not redirect to a page in a unrelated browser.

Trust me, in one year you will cry tears if you read your question again :)