• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

使用 Swift 连接到 PHP 服务器

用户头像
it1352
帮助1

问题说明

我有这个脚本让目标 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.

正确答案

#1

我对你的第三方库不熟悉,但是你应该可以用内置的方法来实现,因为它看起来很简单同步请求.试试这个:

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
系列文章
更多 icon
同类精品
更多 icon
继续加载