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?

Jeesson_7
  • 761
  • 1
  • 11
  • 38

3 Answers3

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().

sschunara
  • 2,285
  • 22
  • 31
  • 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. – Jeesson_7 Feb 27 '17 at 07:27
  • Main reason from 2 request is calling datataskrequest and loading request in web view. If you comment 'task.resume' your issue will be solved – sschunara Feb 27 '17 at 07:35
3

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

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

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()
Craig Grummitt
  • 2,945
  • 1
  • 22
  • 34