0

This is my request funtion:

func receiptValidation(completion: @escaping(_ isPurchaseSchemeActive: Bool, _ error: Error?) -> ()) {
    let receiptFileURL = Bundle.main.appStoreReceiptURL
    guard let receiptData = try? Data(contentsOf: receiptFileURL!) else {
        //This is the First launch app VC pointer call
        completion(false, nil)
        return
    }
    let recieptString = receiptData.base64EncodedString(options: NSData.Base64EncodingOptions(rawValue: 0))
    let jsonDict: [String: AnyObject] = ["receipt-data" : recieptString as AnyObject, "password" : "7bb160f1c8ec4d929fbc751c507d24fd" as AnyObject]
    
    do {
        let requestData = try JSONSerialization.data(withJSONObject: jsonDict, options: JSONSerialization.WritingOptions.prettyPrinted)
        let storeURL = URL(string: self.verifyReceiptURL)!
        var storeRequest = URLRequest(url: storeURL)
        storeRequest.httpMethod = "POST"
        storeRequest.httpBody = requestData
        let session = URLSession(configuration: URLSessionConfiguration.default)
        let task = session.dataTask(with: storeRequest, completionHandler: { [weak self] (data, response, error) in
            do {
                if let jsonResponse = try JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.mutableContainers) as? NSDictionary {
                    print("json response \(jsonResponse)")
                    if let expiresDate = self?.getPurchaseAndExpirationDateFromResponse(jsonResponse, keyString: "expires_date") {
                        //print("expiresDate \(expiresDate)")
                        let purchaseStatus = self?.isSubscriptionActive(expireDate: expiresDate)
                        if let purchaseStatus = purchaseStatus {
                            completion(purchaseStatus, nil)
                        }
                    }
                }
            } catch let parseError {
                completion(false, parseError)
            }
        })
        task.resume()
    } catch let parseError {
        completion(false, parseError)
    }
}

This is how I am calling it:

func callForVal() {
    receiptValidation() { isPurchaseSchemeActive, error in
        if let err = error {
            self.onBuyProductHandler?(.failure(err))
        } else {
            self.onBuyProductHandler?(.success(isPurchaseSchemeActive))
        }
    }
}

But sometimes It takes a long time to give a response back. Now I want to call it with a 60 seconds timer If do not get any response within these 60 seconds. How can I do it?

Tulon
  • 4,011
  • 6
  • 36
  • 56
  • You can create your custom URLSessionConfiguration instead of URLSessionConfiguration.default and set timeout for it. Refer: https://developer.apple.com/documentation/foundation/nsurlsessionconfiguration/1408259-timeoutintervalforrequest – Hoang Anh Tuan Feb 27 '22 at 17:04
  • Thanks for your comment. But I am not asking for additional data. I am simply trying to call the same request from the beginning. @HoangAnhTuan – Tulon Feb 27 '22 at 17:15
  • You _can_ just initiate another URL session task if the first one timed out (e.g. look for [`URLError(.timedOut)`](https://developer.apple.com/documentation/foundation/urlerror/2293002-timedout)), but why would you do that rather than just increasing your timeout as Hoang Anh Tuan suggested? – Rob Feb 28 '22 at 04:54
  • Unrelated: Do not create a new `URLSession` instance for each request. You are going to leak memory if you don’t invalidate old sessions. Easier, instantiate a single `URLSession`, rather than one for every request. – Rob Feb 28 '22 at 04:55

0 Answers0