您的位置:首页 > 移动开发 > IOS开发

IOS 通过GET方式与服务器通信

2015-10-23 16:18 316 查看
//使用PHP编写一个服务器,GET方法传入一个参数

<?php

$name = $_GET['name'];

if( isset($name)){
echo 'hello '.$name;
}else
{
echo "No Args";
}

?>


import UIKit

class ViewController: UIViewController {

@IBOutlet var tvout: UITextView!
@IBAction func Connectbtnpress(sender: AnyObject) {

let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
let session = NSURLSession(configuration: configuration, delegate:nil, delegateQueue:NSOperationQueue())//创建一个新的线程

let request = NSURLRequest(URL: NSURL(string: "http://localhost:63342/untitled/learn.php?name=\(tvinput.text!)")!)//传入地址,参数为 tvinput.text的内容

let task = session.dataTaskWithRequest(request, completionHandler: { (data:NSData?, response:NSURLResponse?, error:NSError?) -> Void in

if let _ = data{
//把消息发给主线程
dispatch_sync(dispatch_get_main_queue(), { () -> Void in

self.tvout.text = (NSString(data: data!, encoding: NSUTF8StringEncoding)) as String!

})

}

})

task.resume()

}
@IBOutlet var tvinput: UITextField!
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.
}

}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: