首页 > 代码库 > swift使用ASIHttpRequest

swift使用ASIHttpRequest

  1. 添加第三方库ASIHttpRequest
  2. 添加SystemConfiguration.framework, MobileCoreServices.framework,CoreGraphics.framework和libz.1.2.3.dylib类库
  3. 在Build Phases --> Compile Source 中将.m文件的Compiler Flags添加 -fno-objc-arc标识
  4. 在swift和objective-c的桥接文件中插入 #import"ASIHTTPRequest.h"
  5. 编写访问后台的代码

class ServerManager: NSObject, ASIHTTPRequestDelegate{
    
     var server = "http://192.168.0.101:3001"
    
    func getMagazineList()->NSDictionary?{
        var url = NSURL(string: NSString(format: "%@/magazine/list", server))
        var request = ASIHTTPRequest(URL: url)
        request.startSynchronous()
        var requestError = request.error
        var requestData: NSDictionary?
        if(requestError == nil){
            requestData = request.responseString().objectFromJSONString() as? NSDictionary
        }else{
            println("\(requestError)")
        }
        return requestData
    }
    
    func getMagazine() -> NSDictionary?{
        var url =  NSURL(string: NSString(format: "%@/magazine/info", server))
        var request = ASIHTTPRequest(URL: url)
        request.startSynchronous()
        var requestError = request.error
        var requestData:NSDictionary?
        if(requestError == nil){
            requestData = request.responseString().objectFromJSONString() as? NSDictionary
        }else{
            println("\(requestError)")
        }
       return requestData
      
    }
}



swift使用ASIHttpRequest