我使用的 API 有 3 個引數年月和薪水型別。
當我在我的 URL 中輸入這些引數時
if let url = URL(string: "https://rip.rtuk.gov.tr/workplacebackend/api/Employee/GetPayroll?yil=\(year)&ay=\(month)&maasturu=\(salaryType)")
網址變成
"https://rip.rtuk.gov.tr/workplacebackend/api/Employee/GetPayroll?yil=2020&ay=2&maasturu=1"
當我使用 Auth 和 Token 輸入此 URL 時,它會回傳一個 pdf 檔案。我應該如何修改我的 getPayroll 以便我可以在請求后下載 pdf?
func getPayroll(year: Int, month: Int, salaryType: Int, completion: @escaping (String) -> ()) {
if let url = URL(string: "https://rip.rtuk.gov.tr/workplacebackend/api/Employee/GetPayroll?yil=\(year)&ay=\(month)&maasturu=\(salaryType)") {
var request = URLRequest(url: url)
request.httpMethod = "GET"
request.addValue("Basic \(authToken)", forHTTPHeaderField: "Authorization")
request.addValue(workingToken, forHTTPHeaderField: "token")
let task = URLSession.shared.downloadTask(with: request) { (localURL, urlResponse, error) in
if let localURL = localURL {
if let string = try? String(contentsOf: localURL) {
completion(string)
}
}
}
task.resume()
}
}
所以當我點擊一個單元格時,我想顯示一個警報,下載然后打開 pdf
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
switch cellType(rawValue: selected.title) {
case .salary:
selectedPayment = (payment?[indexPath.row])!
RtukNetworkManager.shared.getPayroll(year: selectedPayment.year, month: selectedPayment.month, salaryType: selectedPayment.paymentType) { filePath in
DispatchQueue.main.async {
self.displayDownloadAlert(filePath)
}
}
func downloadFile(path: String) {
let donwloadManager = FileDownloadManager()
donwloadManager.downloadFile(path: path, viewController: self)
}
當我點擊一個單元格時,收到一個錯誤作為警報
錯誤 無法顯示檔案
uj5u.com熱心網友回復:
這會將pdf檔案保存到url并檢查是否已經下載
func getPayroll(year: Int, month: Int, salaryType: Int, completion: @escaping ((URL?) -> ())) {
let documentsUrl = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
let parameters: [String: Int] = ["y?l": year, "ay": month, "maasturru": salaryType]
if let url = URL(string: "https://rip.rtuk.gov.tr/workplacebackend/api/Employee/GetPayroll?yil=\(year)&ay=\(month)&maasturu=\(salaryType)") {
let destinationUrl = documentsUrl.appendingPathComponent(url.lastPathComponent)
if FileManager().fileExists(atPath: destinationUrl.path) {
print("File already exists [\(destinationUrl.path)]")
completion(destinationUrl)
}
var request = URLRequest(url: url)
request.httpMethod = "GET"
request.addValue("Basic \(authToken)", forHTTPHeaderField: "Authorization")
request.addValue(workingToken, forHTTPHeaderField: "token")
let session = URLSession(configuration: .default)
let task = session.dataTask(with: request) { (data, response, error) in
if error == nil {
if let response = response as? HTTPURLResponse {
if response.statusCode == 200 {
if let data = data {
if let _ = try? data.write(to: destinationUrl, options: Data.WritingOptions.atomic)
{
completion(destinationUrl)
}
else
{
completion(destinationUrl)
}
}
}
}
} else {
print(error?.localizedDescription)
completion(nil)
}
}
task.resume()
}
}
之后打電話給你下載
RtukNetworkManager.shared.getPayroll(year: selectedPayment.year, month: selectedPayment.month, salaryType: selectedPayment.paymentType) { filePath in
if let url = filePath {
self.displayDownloadAlert(url.lastPathComponent)
showPdf(url:url)
}
}
顯示pdf
func showPdf(url:URL) {
let pdfView = PDFView(frame: CGRect(x:100,y:100,width:200,height:200))
let pdfDocument = PDFDocument(url: url))
pdfView.autoresizesSubviews = true
pdfView.autoresizingMask = [.flexibleWidth, .flexibleHeight, .flexibleTopMargin, .flexibleLeftMargin]
pdfView.displayDirection = .vertical
pdfView.autoScales = true
pdfView.displayMode = .singlePage
pdfView.displaysPageBreaks = true
pdfView.document = pdfDocument
pdfView.maxScaleFactor = 4.0
pdfView.minScaleFactor = pdfView.scaleFactorForSizeToFit
pdfView.usePageViewController(true, withViewOptions: [:])
view.addSubview(pdfView)
}
還匯入 PDFKit PDFView 用于顯示 pdf
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/490028.html
