如何使用swift在UIWebView上加载HTML字符串?

时间:2017-09-04 05:31:03

标签: html ios swift uiwebview

以下是我用于在Web视图上加载HTML字符串的代码: -

let urlAsString = "https://here is my url"            
var encryptedStr: String = "merchant_id=\(merchantId)&order_id=\(orderId)&redirect_url=\(redirectUrl)&cancel_url=\(cancelUrl)&language=EN&billing_name=\(billingName)&billing_address=\(billingAddress)&billing_city=\(billingCity)&billing_state=\(billingState)&billing_zip=\(billingZipCode)&billing_country=\(billingCountry)&billing_tel=\(billingTel)&billing_email=\(billingEmail)&delivery_name=\(deliveryName)&delivery_address=\(deliveryAddress)&delivery_city=\(deliveryCity)&delivery_state=\(deliveryState)&delivery_zip=\(deliveryZipCode)&delivery_country=\(deliveryCountry)&delivery_tel=\(deliveryTel)&merchant_param1=additional Info.&merchant_param2=additional Info.&merchant_param3=additional Info.&merchant_param4=additional Info.&payment_option=\(payOptId)&card_type=\(cardType)&card_name=\(cardName)&data_accept=\(dataAcceptedAt)&enc_val=\(encVal!)&issuing_bank=\(issuingBank)&access_code=\(accessCode)&mobile_no=\(mobileNo)&emi_plan_id=\(emiPlanId)&emi_tenure_id=\(emiTenureId)"

print("encryptedStr :: ",encryptedStr)

if isSaveCard!
{
    encryptedStr = encryptedStr + ("&saveCard=Y")
}

let myRequestData = NSData(bytes: encryptedStr, length: encryptedStr.lengthOfBytes(using: .utf8))

print("\n\n\n myRequestData :: ",myRequestData.description)

let request: NSMutableURLRequest = NSMutableURLRequest(url: URL(string: urlAsString)!)

request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "content-type")
request.setValue(urlAsString, forHTTPHeaderField: "Referer")
request.httpMethod = "POST"
request.httpBody = myRequestData as Data
print("\n\n\nwebview :: ",request)

let transResData: Data? = try? NSURLConnection.sendSynchronousRequest(request as URLRequest, returning: nil) // here i am not getting any data

print("transResData ::: \(String(describing: transResData))") 

let transResString = String(data: transResData!, encoding: String.Encoding.ascii)
print("\n\n\n*******************************Payload Response Start*******************************\n")
print("\(String(describing: transResString))") // it gives blank space no any HTML Code
print("\n**********************************Payload Response End********************************\n\n\n")
viewWeb.loadHTMLString(transResString!, baseURL: nil)

但是没有在名为 transResData 的变量中获取任何数据。它提供0字节作为输出。请提供任何可以帮助我获取输出的代码。 TIA。

0 个答案:

没有答案