Skip to content

Instantly share code, notes, and snippets.

@tototti
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tototti/b12c0befaef32e4f49f8 to your computer and use it in GitHub Desktop.
Save tototti/b12c0befaef32e4f49f8 to your computer and use it in GitHub Desktop.
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