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. } }
相关文章推荐
- ios开发系列-新建项目
- IOS Sent Events 事件
- 添加pch文件
- iOS开发系列--地图与定位
- iOS tableView滑动隐藏/显示tabbar
- 运行百度语音识别官方iOS demo报错: load offline engine failed: 4001
- iOS--格式化日期时间
- iOS 开发中如何使用半透明模糊效果
- iOS开发—AFNetworking的使用
- iOS开发>学无止境 - 委托模式
- iOS三方-MJRefresh的使用
- iOS 日志自动上报
- iOS---开发时间戳与时间,时区的转换,汉字与UTF8,16进制的转换
- iOS---标准时间与时间戳相互转换
- iOS子视图中找到特定类型的视图
- 触摸点为scrollview上的子控件时,scrollview不能滚动(iOS8)
- iOS开发系列--地图与定位
- iOS-如何使用symbolicatecrash
- 开始iOS 7中自动布局教程(二)
- iOS开发必备--环境变量配置(Debug & Release)