1
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

swiftで楽天レシピをAPI通信で実装してみた。

Posted at

今回はオリジナルアプリケーションを制作する上でAPI通信で実装してみたかったので今回備忘録的に記述していきます。

今回使用するAPIは楽天です。
その中の料理レシピのAPIがあったのでそれを使って通信してみました。

とりあえず途中までのコードです。

今回はMVCモデルで楽天APIから最初はレシピ画像とレシピタイトルを持ってきます。

json形式でデータを持ってきます。

APIのURL
https://webservice.rakuten.co.jp/explorer/api/Recipe/CategoryRanking/

ViewController.swift
import UIKit
import Alamofire
import SwiftyJSON
import SDWebImage

class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
    
    
    @IBOutlet weak var tableView: UITableView!
    
    var userName = String()
    
    

    override func viewDidLoad() {
        super.viewDidLoad()
        
        tableView.delegate = self
        tableView.dataSource = self
       
        jsonAnaly()
        
    }
    
    var contentArray = [Contents]()
    
    
    func jsonAnaly()  {
   
        let url = "https://app.rakuten.co.jp/services/api/Recipe/CategoryRanking/20170426?format=json&applicationId=1014272479943576132"
    
        AF.request(url, method: .get, parameters: nil, encoding: JSONEncoding.default).responseJSON { (response) in
            
            switch response.result{
            
            case .success:
                let json:JSON = JSON(response.data as Any)
                
                for i in 0...json.count{
                        let foodImageUrl = json["result"][i]["foodImageUrl"].string
                        
                        let recipeTitle = json["result"][i]["recipeTitle"].string
                        
            
                        var contentModel = Contents(foodImageUrl:foodImageUrl , recipeTitle:recipeTitle)
                    
                        self.contentArray.append(contentModel)
                }
                
                self.tableView.reloadData()
                
            case .failure(let error):
                
                print(error)
            
                
            }
        }
    
    }

構造体を使って引数などを持ってきています。

ImageModel.swift
import Foundation

struct Contents:Codable {
    var foodImageUrl:String?
    var recipeTitle:String?
}

まだ、勉強を初めて2ヶ月ほどなのでいまいちfor文を回すことやAPIのパラメーターの概念がわからなかったので苦労しました。

func jsonAnaly() {

    let url = "https://app.rakuten.co.jp/services/api/Recipe/CategoryRanking/20170426?format=json&applicationId=1014272479943576132"

ここで楽天レシピのAPIのURLを持ってきています。

通信で持ってきたデータをfor文で回します。

for i in 0...json.count{
                        let foodImageUrl = json["result"][i]["foodImageUrl"].string

                        let recipeTitle = json["result"][i]["recipeTitle"].string


                        var contentModel = Contents(foodImageUrl:foodImageUrl , recipeTitle:recipeTitle)

                        self.contentArray.append(contentModel)
                }

楽天APIのデータ

{
  "result": [ "result"は配列になっている
   {
      "foodImageUrl":

  "result"の[i]番目の"foodImageUrl"

"https://image.space.rakuten.co.jp/d/strg/ctrl/3/34d4ce95b8674c8fb6c8f08b5115464a9f180c31.17.2.3.2.jpg",
      "recipeDescription": "小鉢がもう1品ほしいなっていう時に簡単でオススメです。",
      "recipePublishday": "2011/08/22 19:04:07",
      "shop": 0,
      "pickup": 1,
      "recipeId": 1200002403,
      "nickname": "JIMA88",
      "smallImageUrl": "https://image.space.rakuten.co.jp/d/strg/ctrl/3/34d4ce95b8674c8fb6c8f08b5115464a9f180c31.17.2.3.2.jpg?thum=55",
      "recipeMaterial": [
        "きゅうり",
        "ごま油",
        "すりごま",
        "鶏ガラスープのもと",
        "ビニール袋"
      ],
      "recipeIndication": "5分以内",
      "recipeCost": "100円以下",
      "rank": "1",
      "recipeUrl": "https://recipe.rakuten.co.jp/recipe/1200002403/",
      "mediumImageUrl": "https://image.space.rakuten.co.jp/d/strg/ctrl/3/34d4ce95b8674c8fb6c8f08b5115464a9f180c31.17.2.3.2.jpg?thum=54",
      "recipeTitle": "1分で!うまうま胡麻キュウリ"
    },

let foodImageUrl = json["result"][i]["foodImageUrl"].string
"result"の[i]番目のイメージをlet foodImageUrlに入れる。

ここまでがかなり苦労しました。

今回はここまでです。

続きはまた、近日中に公開します。

1
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?