Last active
August 29, 2015 14:04
-
-
Save tototti/b12c0befaef32e4f49f8 to your computer and use it in GitHub Desktop.
SwiftでYoApiを呼び出してみた。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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