Alamofire101

|

기본 구현

//APIService.swift
import Foundation
import Alamofire
import AlamofireActivityLogger //Alamofire에서 .log기능을 사용하기 위해 필요한 것이며 필수는 아님

public struct APIService{
 
    static func fetchItunes(errHandler:@escaping ()->Void , handler:@escaping ()->Void ){
        let urlString = "https://itunes.apple.com/kr/rss/topfreeapplications/limit=50/genre=6015/json"
        Alamofire.request(urlString)
            //.log()
            .responseJSON { (response ) in
            
                if let result = response.result.value {
                    let JSON = result as! NSDictionary
                
                    if let feedObj = JSON["feed"] as? NSDictionary {
                        
                        if let title = feedObj["title"]{
                            print("성공 title: \(title)")
                            handler()
                            return
                        }
                        
                    }
                    
                    
                }else{
                    errHandler() //mmm.
                }
               
                
        }
    }
    
    //간단 함수
    static func fetchCoinone(){
        let url = "https://api.coinone.co.kr/ticker/?currency=btc"
        
        Alamofire.request(url)
            .log()
            .responseJSON { (response ) in
                print(response)
                
        }
    }
}


호출방법

//호출
APIService.fetchItunes(errHandler: {
    print("fail")
}) {
    print("done")
}

Comments