swift에서 web을 통해 json 받아오기
다른 방법도 많겠지만 로딩되자마자 콘솔창에 받아오는걸 보고 싶었다.
처음 프로젝트 만들고 나면
ViewController.swift 가 있다.
거기에 이런 부분이 만들어져 있다.
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
여기에다가 이렇게 추가하고
override func viewDidLoad() {
let apiURI = NSURL(string: "http://ip.jsontest.com")
let apidata : NSData? = NSData(contentsOfURL: apiURI!)
NSLog("API Result=%@",NSString(data:apidata!,encoding: NSUTF8StringEncoding)!)
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
돌리면 에러난다 에러는 여기서 시키는 대로 하니 해결되더라
http://stackoverflow.com/questions/31254725/transport-security-has-blocked-a-cleartext-http
실행시키면
2016-07-20 17:00:09.163 hello[4845:2248555] API Result={"ip": "우리.집.아이.피"}
잘나온다
- 테스트용 json은 http://www.jsontest.com 여기에서 ip받는 부분을 사용했습니다.
'COMPUTER > swift' 카테고리의 다른 글
uitableview 기타등등 기능. (0) | 2016.09.20 |
---|---|
swift tab (0) | 2016.09.19 |
swift 간단 animation 효과 (0) | 2016.08.10 |
API 호출시 UI 멈춤 해결 (1) | 2016.07.28 |
스위프트 기초 (0) | 2016.02.16 |