質問編集履歴
1
参考にした書籍を追加しました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -78,7 +78,7 @@
|
|
78
78
|
|
79
79
|
//検索キーワードをURLエンコードする
|
80
80
|
|
81
|
-
guard let keyword_encode = keyword.addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlQueryAllowed) else {
|
81
|
+
guard let keyword_encode = keyword.addingPercentEncoding(withAllowedCharacters: sNSCharacterSet.urlQueryAllowed) else {
|
82
82
|
|
83
83
|
return
|
84
84
|
|
@@ -277,3 +277,255 @@
|
|
277
277
|
| keyword | text | NO | | NULL | |
|
278
278
|
|
279
279
|
+-----------+------------------+------+-----+---------+----------------+
|
280
|
+
|
281
|
+
|
282
|
+
|
283
|
+
参考にしたものはこちらです。(たった2日でマスターできるiPhoneアプリ開発集中講座 Xcode 10 Swift 4.2対応 より)
|
284
|
+
|
285
|
+
```swift
|
286
|
+
|
287
|
+
//Jsonのitem内のデータ構造
|
288
|
+
|
289
|
+
struct ItemJson: Codable {
|
290
|
+
|
291
|
+
//お菓子の名前
|
292
|
+
|
293
|
+
let name: String?
|
294
|
+
|
295
|
+
//メーカー
|
296
|
+
|
297
|
+
let maker: String?
|
298
|
+
|
299
|
+
//掲載URL
|
300
|
+
|
301
|
+
let url: URL?
|
302
|
+
|
303
|
+
//画像のURL
|
304
|
+
|
305
|
+
let image: URL?
|
306
|
+
|
307
|
+
}
|
308
|
+
|
309
|
+
|
310
|
+
|
311
|
+
//JSONのデータ構造
|
312
|
+
|
313
|
+
struct ResultJson: Codable {
|
314
|
+
|
315
|
+
//複数要素
|
316
|
+
|
317
|
+
let item:[ItemJson]?
|
318
|
+
|
319
|
+
}
|
320
|
+
|
321
|
+
|
322
|
+
|
323
|
+
//searchokashiメソッド
|
324
|
+
|
325
|
+
func searchOkashi (keyword: String) {
|
326
|
+
|
327
|
+
|
328
|
+
|
329
|
+
//お菓子の検索キーワードをURLエンコードする
|
330
|
+
|
331
|
+
guard let keyword_encode = keyword.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) else {
|
332
|
+
|
333
|
+
return
|
334
|
+
|
335
|
+
}
|
336
|
+
|
337
|
+
|
338
|
+
|
339
|
+
//リクエストURLの組み立て
|
340
|
+
|
341
|
+
guard let req_url = URL(string:
|
342
|
+
|
343
|
+
"http://www.sysbird.jp/toriko/api/?apikey=guest&format=json&keyword=(keyword_encode)&max=10&prder=r") else {
|
344
|
+
|
345
|
+
return
|
346
|
+
|
347
|
+
}
|
348
|
+
|
349
|
+
print(req_url)
|
350
|
+
|
351
|
+
|
352
|
+
|
353
|
+
//リクエストに必要な情報を生成
|
354
|
+
|
355
|
+
let req = URLRequest(url: req_url)
|
356
|
+
|
357
|
+
//データ転送を管理するためのセッションを生成
|
358
|
+
|
359
|
+
let session = URLSession(configuration: .default, delegate: nil, delegateQueue: OperationQueue.main)
|
360
|
+
|
361
|
+
//リクエストをタスクとして登録
|
362
|
+
|
363
|
+
let task = session.dataTask(with: req, completionHandler: {
|
364
|
+
|
365
|
+
(data, response, error) in
|
366
|
+
|
367
|
+
//セッション終了
|
368
|
+
|
369
|
+
session.finishTasksAndInvalidate()
|
370
|
+
|
371
|
+
//do try catcyエラーハンドリング
|
372
|
+
|
373
|
+
do{
|
374
|
+
|
375
|
+
//JSONDecoderのインスタンス取得
|
376
|
+
|
377
|
+
let decoder = JSONDecoder()
|
378
|
+
|
379
|
+
//受け取ったJSONデータをパース(解析)して格納
|
380
|
+
|
381
|
+
let json = try decoder.decode(ResultJson.self, from:data!)
|
382
|
+
|
383
|
+
|
384
|
+
|
385
|
+
|
386
|
+
|
387
|
+
//お菓子の情報が取得できているか確認
|
388
|
+
|
389
|
+
if let items = json.item {
|
390
|
+
|
391
|
+
//お菓子のリストを初期化
|
392
|
+
|
393
|
+
self.okashiList.removeAll()
|
394
|
+
|
395
|
+
//取得したお菓子の数だけ処理
|
396
|
+
|
397
|
+
for item in items {
|
398
|
+
|
399
|
+
//お菓子の名前、メーカ名、掲載URL,画像URLをアンラップ
|
400
|
+
|
401
|
+
if let name = item.name, let maker = item.maker, let link = item.url, let image = item.image {
|
402
|
+
|
403
|
+
//1つのお菓子をタプルでまとめて管理
|
404
|
+
|
405
|
+
let okashi = (name, maker, link, image)
|
406
|
+
|
407
|
+
//お菓子の配列へ追加
|
408
|
+
|
409
|
+
self.okashiList.append(okashi)
|
410
|
+
|
411
|
+
}
|
412
|
+
|
413
|
+
}
|
414
|
+
|
415
|
+
//TableView を更新する
|
416
|
+
|
417
|
+
self.tableView.reloadData()
|
418
|
+
|
419
|
+
if let okashidbg = self.okashiList.first {
|
420
|
+
|
421
|
+
print ("-----------------")
|
422
|
+
|
423
|
+
print ("okashiList[0] = (okashidbg)")
|
424
|
+
|
425
|
+
}
|
426
|
+
|
427
|
+
}
|
428
|
+
|
429
|
+
|
430
|
+
|
431
|
+
} catch {
|
432
|
+
|
433
|
+
//エラー処理
|
434
|
+
|
435
|
+
print("エラーが出ました")
|
436
|
+
|
437
|
+
}
|
438
|
+
|
439
|
+
})
|
440
|
+
|
441
|
+
//ダウンロード開始
|
442
|
+
|
443
|
+
task.resume()
|
444
|
+
|
445
|
+
}
|
446
|
+
|
447
|
+
|
448
|
+
|
449
|
+
//Cellに値を設定するdatasourceメソッド。必ず記述。
|
450
|
+
|
451
|
+
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
452
|
+
|
453
|
+
//お菓子リストの総数
|
454
|
+
|
455
|
+
return okashiList.count
|
456
|
+
|
457
|
+
}
|
458
|
+
|
459
|
+
|
460
|
+
|
461
|
+
//Cellに値を設定するdatasourceメソッド。必ず記述。
|
462
|
+
|
463
|
+
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
464
|
+
|
465
|
+
//今回表示を行う、Cellオブジェクト(1行)を取得
|
466
|
+
|
467
|
+
let cell = tableView.dequeueReusableCell(withIdentifier: "okashiCell", for: indexPath)
|
468
|
+
|
469
|
+
//お菓子のタイトル設定
|
470
|
+
|
471
|
+
cell.textLabel?.text = okashiList[indexPath.row].name
|
472
|
+
|
473
|
+
//お菓子の画像を取得
|
474
|
+
|
475
|
+
if let imageData = try? Data(contentsOf: okashiList[indexPath.row].image) {
|
476
|
+
|
477
|
+
//正常に取得できた場合は、UIImageで画像オブジェクトを生成してCellにお菓子画像を設定
|
478
|
+
|
479
|
+
cell.imageView?.image = UIImage(data: imageData)
|
480
|
+
|
481
|
+
}
|
482
|
+
|
483
|
+
//設定済みのCellオブジェクトを画像に反映
|
484
|
+
|
485
|
+
return cell
|
486
|
+
|
487
|
+
}
|
488
|
+
|
489
|
+
|
490
|
+
|
491
|
+
//Cellが洗濯された際に呼び出されるdelegateメソッド
|
492
|
+
|
493
|
+
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
|
494
|
+
|
495
|
+
// ハイライト解除
|
496
|
+
|
497
|
+
tableView.deselectRow(at: indexPath, animated: true)
|
498
|
+
|
499
|
+
|
500
|
+
|
501
|
+
//SFSafariViewを開く
|
502
|
+
|
503
|
+
let safariViewController = SFSafariViewController(url: okashiList[indexPath.row].link)
|
504
|
+
|
505
|
+
|
506
|
+
|
507
|
+
//dekegate の通知先を自分自身
|
508
|
+
|
509
|
+
safariViewController.delegate = self
|
510
|
+
|
511
|
+
|
512
|
+
|
513
|
+
//SafariViewが開かれる
|
514
|
+
|
515
|
+
present(safariViewController, animated: true, completion: nil)
|
516
|
+
|
517
|
+
}
|
518
|
+
|
519
|
+
|
520
|
+
|
521
|
+
// SafariViewが閉じられた時によばれるdelegateメソッド
|
522
|
+
|
523
|
+
func safariViewControllerDidFinish(_ controller: SFSafariViewController) {
|
524
|
+
|
525
|
+
// SafariViewを閉じる
|
526
|
+
|
527
|
+
dismiss(animated: true, completion: nil)
|
528
|
+
|
529
|
+
}
|
530
|
+
|
531
|
+
```
|