我在git集線器上發現了一個條形碼掃描器項目,我將其合併到我的應用程序link中。我正在使用Google圖書API來獲取有關我掃描的圖書的信息。發送信息到另一個視圖控制器
func getBookInfo(isbn: String) {
guard let url = URL(string: "https://www.googleapis.com/books/v1/volumes?q=isbn13:\(isbn)") else {
print("the url is not valid")
return
}
URLSession.shared.dataTask(with: url, completionHandler: {data, response, error -> Void in
guard error == nil else {
print(response)
print(error!.localizedDescription)
return
}
guard let data = data else {
print("no error but no data")
print(response)
return
}
guard let jsonResult = try? JSONSerialization.jsonObject(with: data, options: []) else {
print("the JSON is not valid")
return
}
if let arrayOfTitles = (jsonResult as AnyObject).value(forKeyPath: "items.volumeInfo.title") as? [String] {
self.BookName.text = "\(arrayOfTitles[0])"
print(self.BookName.text!)
}
if let arrayOfAuthors = (jsonResult as AnyObject).value(forKeyPath: "items.volumeInfo.authors") as? [[String]] {
self.Author.text = "\((arrayOfAuthors[0])[0])"
print(self.Author.text!)
}
if let arrayOfCategories = (jsonResult as AnyObject).value(forKeyPath: "items.volumeInfo.categories") as? [[String]] {
self.Category.text = "\((arrayOfCategories[0])[0])"
print(self.Category.text!)
}
if let arrayOfISBN13 = (jsonResult as AnyObject).value(forKeyPath: "items.volumeInfo.industryIdentifiers.identifier") as? [[String]] {
self.ISBN13.text = "\((arrayOfISBN13[0])[0])"
print(self.ISBN13.text!)
}
if let arrayOfISBN10 = (jsonResult as AnyObject).value(forKeyPath: "items.volumeInfo.industryIdentifiers.identifier") as? [[String]] {
self.ISBN10.text = "\((arrayOfISBN10[0])[1])"
print(self.ISBN10.text!)
}
if let arrayOfFormat = (jsonResult as AnyObject).value(forKeyPath: "items.volumeInfo.printType") as? [String] {
self.CoverType.text = "\(arrayOfFormat[0])"
print(self.CoverType.text!)
}
}).resume()
}
後,我掃描書籍,並已收到的信息,我想辭退具有條形碼掃描儀,並在顯示視圖控制器視圖控制器,我想顯示我的書的信息只是掃描。
extension MultipleImageViewController: BarcodeScannerCodeDelegate {
func barcodeScanner(_ controller: BarcodeScannerController, didCaptureCode code: String, type: String) {
if code.isEmpty {
let delayTime = DispatchTime.now() + Double(Int64(6 * Double(NSEC_PER_SEC)))/Double(NSEC_PER_SEC)
DispatchQueue.main.asyncAfter(deadline: delayTime) {
controller.resetWithError()
}
}
else{
getBookInfo(isbn: code)
let delayTime = DispatchTime.now() + Double(Int64(6 * Double(NSEC_PER_SEC)))/Double(NSEC_PER_SEC)
DispatchQueue.main.asyncAfter(deadline: delayTime) {
controller.dismiss(animated: true, completion: nil)
}
}
}
}
然而,當條形碼掃描器視圖控制器被駁回,我不得不退出程序,然後再回來到應用程序,以便爲我的信息,在這我想它的視圖控制器來顯示。不用離開應用程序並返回來自條形碼掃描器的信息不會顯示在所需的視圖控制器中。
這是什麼意思?「我必須退出應用程序,然後返回到應用程序,以便我的信息在視圖控制器中顯示出我想要的。」 – Shubham
請您詳細說明代碼段沒有以預期的方式工作? – Shubham
當我掃描書本代碼並找到該書時,我想關閉條形碼控制器,然後讓我的已發現信息顯示在現有的視圖控制器上,但它不會顯示,除非我按Home按鈕退出該應用程序,然後再次按下應用程序。然後顯示信息 – juelizabeth