本文主要是介绍swift使用ASIHttpRequest,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
- 添加第三方库ASIHttpRequest
- 添加SystemConfiguration.framework, MobileCoreServices.framework,CoreGraphics.framework和libz.1.2.3.dylib类库
- 在Build Phases --> Compile Source 中将.m文件的Compiler Flags添加 -fno-objc-arc标识
- 在swift和objective-c的桥接文件中插入 #import "ASIHTTPRequest.h"
- 编写访问后台的代码
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.errorvar 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.errorvar requestData:NSDictionary?if(requestError == nil){requestData = request.responseString().objectFromJSONString() as? NSDictionary}else{println("\(requestError)")}return requestData}
}
这篇关于swift使用ASIHttpRequest的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!