使用 Swift 连接到 PHP 服务器
问题说明
我有这个脚本让目标 C 连接到服务器:
I have this script for Objective C to connect to server:
NSURL *url = [NSURL URLWithString:@"http://localhost/project"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request startSynchronous];
NSError *error = [request error];
if (!error) {
NSString *response = [request responseString];
NSArray *results = [response componentsSeparatedByString:@", "];
for (NSString* result in results) {
NSString * trimmedResult = [result stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if ([trimmedResult isEqualToString:@"failure"])
NSLog(@"operation %i failed.", i 1);
}
}
但现在我需要使用 Swift.在 Objective C 中连接并桥接到 swift 还是在 swift 中连接更容易?不过,我真的很想快速完成.
but now I need to use Swift. Is it easier to connect in Objective C and bridge to swift, or connect in swift? I would really like to just do it in swift though.
正确答案
我对你的第三方库不熟悉,但是你应该可以用内置的方法来实现,因为它看起来很简单同步请求.试试这个:
I'm not familiar with your third party library, but you should be able to achieve the same with the built in methods, since it seems to be a simple synchronous request. Try this:
let url = NSURL(string:"http://localhost/project")
let request = NSURLRequest(URL:url)
var response: NSURLResponse? = nil
var error: NSError? = nil
let reply = NSURLConnection.sendSynchronousRequest(request, returningResponse:&response, error:&error)
let results = NSString(data:reply, encoding:NSUTF8StringEncoding)
// Continue your processing of results here...
更新要使用 POST,请使用 NSMutableURLRequest.一般形式如下.很难找到的部分是 NSURLProtocol.setProperty(...).参见 此页面.
Update To use POST, employ an NSMutableURLRequest. General form is something like the following. The difficult to find part is NSURLProtocol.setProperty(...). See this page.
let url = NSURL(string:"http://localhost/project")
let cachePolicy = NSURLRequestCachePolicy.ReloadIgnoringLocalCacheData
var request = NSMutableURLRequest(URL: url, cachePolicy: cachePolicy, timeoutInterval: 2.0)
request.HTTPMethod = "POST"
// set Content-Type in HTTP header
let boundaryConstant = "----------V2ymHFg03esomerandomstuffhbqgZCaKO6jy";
let contentType = "multipart/form-data; boundary=" boundaryConstant
NSURLProtocol.setProperty(contentType, forKey: "Content-Type", inRequest: request)
// set data
var dataString = "your data here" boundaryConstant
let requestBodyData = (dataString as NSString).dataUsingEncoding(NSUTF8StringEncoding)
request.HTTPBody = requestBodyData
// set content length
NSURLProtocol.setProperty(requestBodyData.length, forKey: "Content-Length", inRequest: request)
var response: NSURLResponse? = nil
var error: NSError? = nil
let reply = NSURLConnection.sendSynchronousRequest(request, returningResponse:&response, error:&error)
let results = NSString(data:reply, encoding:NSUTF8StringEncoding)
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /reply/detail/tanhcfjjgf
-
YouTube API 不能在 iOS (iPhone/iPad) 工作,但在桌面浏览器工作正常?
it1352 07-30 -
iPhone,一张图像叠加到另一张图像上以创建要保存的新图像?(水印)
it1352 07-17 -
保持在后台运行的 iPhone 应用程序完全可操作
it1352 07-25 -
使用 iPhone 进行移动设备管理
it1352 07-23 -
在android同时打开手电筒和前置摄像头
it1352 09-28 -
扫描 NFC 标签时是否可以启动应用程序?
it1352 08-02 -
检查邮件是否发送成功
it1352 07-25 -
Android微调工具-删除当前选择
it1352 06-20 -
希伯来语的空格句子标记化错误
it1352 06-22 -
Android App 和三星 Galaxy S4 不兼容
it1352 07-20