Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
SwiftでYoApiを呼び出してみた。
//
// ViewController.swift
// swift-yoapi
//
// Created by tototti on 2014/07/21.
// Copyright (c) 2014 tototti. All rights reserved.
//
import UIKit
class ViewController: UIViewController, NSURLConnectionDelegate {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func yoButtonPressed(sender: AnyObject) {
var url = NSURL(string: "http://api.justyo.co/yoall/")
// request
var request = NSMutableURLRequest(URL: url)
request.HTTPMethod = "POST"
request.cachePolicy = NSURLRequestCachePolicy.ReloadIgnoringLocalAndRemoteCacheData
request.timeoutInterval = 10
// postdata
let postString = "api_token=" + API_KEY // enter your YO API KEY
let dataString = postString.dataUsingEncoding(NSUTF8StringEncoding)
request.HTTPBody = dataString
// start connection
var urlConnection = NSURLConnection(request: request, delegate: self, startImmediately: false)
println("sending request...")
urlConnection.start()
}
// NSURLConnectionDelegate
func connection(connection: NSURLConnection!, didReceiveData conData: NSData!) {
println("didReceiveData")
}
// NSURLConnectionDelegate
func connectionDidFinishLoading(connection: NSURLConnection!)
{
println("connectionDidFinishLoading")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment