使用 Swift 连接到 PHP 服务器 [英] Connect to PHP Server using Swift

查看:65
本文介绍了使用 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)

这篇关于使用 Swift 连接到 PHP 服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆