"dataTaskWithRequest" requests the NSMutableRequest twice when only one request is required
Asked Answered
O

3

5

I have a secure webView which shows the customer to Load his wallet . I pass secure information MPIN(like a one time password). There is problem with

@IBOutlet weak var loading: UIActivityIndicatorView!

@IBOutlet var lblLoading: UILabel!


@IBOutlet weak var mob_webview: UIWebView!

override func viewDidLoad()
{
    super.viewDidLoad()
    mob_webview.hidden = true
    mob_webview.delegate=self
    cmmn.createDatabase()
    linkgot = cmmn.geturl()

   link="http://*****************************************.jsp?"

    let request = NSMutableURLRequest(URL: NSURL(string: link)!)
    request.HTTPMethod = "POST"
    let postString = "recharge_type=\(_catcode)&amount=\(_amountfiled_got)&mobileNo=\(cmmn.getPhoneNumber())&prePostLan=\(prePostLan)&stdCode=\(_stdCode)&accNo=\(accNo)&deduct_frm=B&rcMobileNum=\(_numberfiled_got)&mobOperator=\(_merch_code)&operator=\(_operatr)&rcType=\(_rec_type)&mpin=\(_mpin)"
    request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding)
    let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
        data, response, error in

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

        print("response = \(response)")

        let responseString = NSString(data: data!, encoding: NSUTF8StringEncoding)
        print("responseString = \(responseString)")
    }
    task.resume()
    mob_webview.loadRequest(request)







    // Do any additional setup after loading the view.
}
func webViewDidFinishLoad(webView_Pages: UIWebView)
{

    mob_webview.hidden = false
    loading.hidden = true
    lblLoading.hidden=true
    print("OK")

}

Response in server log: enter image description here enter image description here

In the server ,If the user types the MPIN wrong three times, he gets blocked. This is done based on the number of wrong MPIN hits in the server. For some reason my web view makes the request twice (i.e. Calls the link which loads the request twice),even though its loaded just once.Suppose if customer enter wrong MPIN and load the web view, The link is called twice he looses 2 chances to enter correct MPIN. The android version of our APP works correctly with a similar kind of request.Any reason for it?

October answered 20/2, 2017 at 12:26 Comment(4)
Post your code - the request and the completion handler. One line isn't enough.Quantum
You have both a webview AND are making a NSURLSession request? how are the two related? Perhaps the webview is making one request and then you are making another request with the NSURLSession.Moderation
Can you please clean your code? It's really not good enough for us to see what's happening.Soelch
I have updated my codeOctober
L
4

I gone through apple reference document after reading your question. It says then webViewDidFinishLoad is called after loading each frames in webview. Here is document

webViewDidFinishLoad: Sent after a web view finishes loading a frame.

Please check with server, that how many request is made for one run. it's 2 or one. Also want to know how many time your print statment in your code execute print("response = \(response)"). As I don't console for this statement.

Found in your question you calls NSURLSession dataTaskWithRequest and also load request in web view. That might also problem for calling same thing twice. If you want to open request in webview don't use NSURLSession task request. Run it by commenting task.resume().

Lepidosiren answered 27/2, 2017 at 6:18 Comment(2)
I will check it asap. btb the print statement will be executed only once irrespective of my hits in server... I had already checked it before posting this question.October
Main reason from 2 request is calling datataskrequest and loading request in web view. If you comment 'task.resume' your issue will be solvedLepidosiren
T
3

task.resume() and mob_webview.loadRequest(request) will run request twice.

You'd better remove task.resume() before loadRequest.

Terra answered 1/3, 2017 at 6:11 Comment(0)
C
1

You are requesting your NSMutableURLRequest twice:

  1. You pass it to a URLSession's dataTaskWithRequest method. This method returns data from the URL in your request in a closure.
  2. You pass it to your UIWebView's loadRequest method. This method loads the content at the URL of your request in your UIWebView (your mob_webview IBOutlet).

To solve this, you need to either remove the URLSession code or the UIWebView code. Which you remove depends on what you want to do with the response from the server - do you want it as data or do you want to load it in the webview? If you want it as data you should use URLSession and remove the UIWebView loadRequest. If you want to load it in a webview, then you should use UIWebView and remove any UISession code.

If it is the URLSession code you need to remove, you should remove all of the following:

let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
    data, response, error in

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

    print("response = \(response)")

    let responseString = NSString(data: data!, encoding: NSUTF8StringEncoding)
    print("responseString = \(responseString)")
}
task.resume()
Concha answered 28/2, 2017 at 2:28 Comment(1)
Is there anything new in your answer compare to mine answer :)Lepidosiren

© 2022 - 2024 — McMap. All rights reserved.