0

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);
   }
 }

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.

  • You are using third party library(which is outdated) `ASIHTTPRequest`. Since that library is written in Obj C, its better to bridge. – GoodSp33d Jul 01 '14 at 11:05

1 Answers1

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...

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)
Grimxn
  • 22,115
  • 10
  • 72
  • 85
  • how would you pass a parameter through? like a POST parameter for username and password –  Jul 01 '14 at 13:03
  • hi grimxn , this is really useful , what's boundaryConstant for ? – Yank Oct 01 '14 at 13:22
  • It's a random string that delimits the data. Obviously there is a small but finite possibility that the string *will* appear in the data. See this answer http://stackoverflow.com/questions/2305218/what-is-the-boundary-parameter-in-an-http-multi-part-post-request – Grimxn Oct 01 '14 at 14:59