如何使用swift将数据发送到服务器(api)
大家好我正在开发一个使用swift2的应用程序,我在Json格式中使用api键我可以使用它并提供一些数据,例如成功:1,错误: 0如果我的价值成功1意味着我想发送输入的数据或保存的数据到服务器(api键)。怎么做?如何使用swift将数据发送到服务器(api)
在此先感谢
您可以通过使用Alamofire API将数据发送到服务器。这是文件和实施所有的东西都在下面的链接中提到。
https://github.com/Alamofire/Alamofire
使用吊舱只要安装它,它很容易实现。
创建网络类并在其中创建以下功能。
func get_Request(currentView : UIViewController,action : NSString,completionHandler: (NSDictionary -> Void)) {
print("Url==>>>",mainURl + (action as String))
Alamofire.request(.GET,mainURl + (action as String), parameters: nil)
.responseJSON { response in
if let JSON = response.result.value {
completionHandler(JSON as! NSDictionary)
}
else{
printf(response.result.error?.localizedDescription)
}
}
}
其他类
viewDidLoad{
NetworkClass.get_Request(self,action: "yourServiceName/" + (yourParameters as String), completionHandler: self.resultHandler)
}
func resultHandler(result : NSDictionary) -> Void {
printf("result is -->>",result)
}
我不想要使用cocoapod,因为我已经使用两个豆荚..如果我再次添加cocoapod意味着我的应用程序将耗尽电池...所以有任何其他怎么办? – antonyraja
@antonyraja:谁说使用多个cocoapod会消耗电池? – Poles
豆荚只是从源文件存储到您的项目,而不是你手动导入them.Pods不会影响你的电池消耗。 –
你了解afnetwoking? – KKRocks
没有家伙....你能指点什么吗? – antonyraja
@antonyraja我甚至不知道你为什么想更多豆荚=更多的电池消耗......真的是男人?我认为你应该在尝试开发任何东西之前先学习编码的基本原理,否则它将无法正常工作,并且可能导致许多错误 – Tj3n