我是ios的新手..请任何人帮我发布请求。实际上,当我登录Facebook时,它会获取用户信息,例如用户名,从中有登录(lattitude,经度)。是否可以使用api 链接:http://buddysin.aumkiiyo.com/fbc
我的代码是:
@IBAction func btnAPI(sender: UIButton)
{
//startConnection()
connectToWebAPI()
}
func connectToWebAPI()
{
//setting up the base64-encoded credentials
let id = "1620912344817986"
//let password = "pass"
let loginString = NSString(format: "%@:%@", id)
let loginData: NSData = loginString.dataUsingEncoding(NSUTF8StringEncoding)!
let base64LoginString = loginData.base64EncodedStringWithOptions(nil)
//creating the requestz
let url = NSURL(string: "http://buddysin.aumkiiyo.com/fbc")
var request = NSMutableURLRequest(URL: url!)
let config = NSURLSessionConfiguration.defaultSessionConfiguration()
let session = NSURLSession.sharedSession()
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")
let urlConnection = NSURLConnection(request: request, delegate: self)
request.HTTPMethod = "POST"
request.setValue(base64LoginString, forHTTPHeaderField: "Authorization")
let task = session.dataTaskWithURL(url!, completionHandler: {data, response, error -> Void in
if (error != nil) {
println(error)
}
else {
// converting the data into Dictionary
var error: NSError?
let jsonResult = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &error) as! NSDictionary
println(jsonResult)
}
})
//fire off the request
task.resume()
}
当我跑步时,致命错误显示为
`fatal error: unexpectedly found nil while unwrapping an Optional value`
" jsonResult" 中的
答案 0 :(得分:0)
我认为最好使用Alomafire。作为Objective-C中的AFNetWorking,它是一个简化了很多http请求的库。
答案 1 :(得分:0)
您应该找到由于此错误导致的变量:
例如data
,
if let dataTemp = data as? NSDictionary {
}
供参考:
这是一种' POST' swift中AFNetworking
的方法,下面的代码应该在你的connectToWebAPI
方法中,将你的网址打包成NSURL。
let manager = AFHTTPRequestOperationManager(baseURL: NSURL(string: yourURL))
manager.POST("path", parameters: ["key":value], success: { (opeartion:AFHTTPRequestOperation!, data:AnyObject!) -> Void in
},failure: { (operation:AFHTTPRequestOperation!, error:NSError!) -> Void in
})
安装AFNetworking
的教程。
https://github.com/AFNetworking/AFNetworking/wiki/Getting-Started-with-AFNetworking
答案 2 :(得分:0)
访问this问题以检查我的请求后功能(如果是 你不想出于任何原因使用Alamofire)
访问this问题,检查您需要执行的步骤 想要将Alamofire添加到您的XCode项目
如果您需要从服务器获取json数据,请使用
SwiftyJSON。就像将SwiftyJSON.swift拖入其中一样简单
您的项目选中“如果需要复制项目”并使用类似
让jsonData = JSON(data:yourDataFromServer)
希望我帮助过:)
答案 3 :(得分:0)
func postSomething(completionHandler: @escaping CompletionHandler){
let loginString = NSString(format: "%@:%@", id)
let loginData: NSData = loginString.dataUsingEncoding(NSUTF8StringEncoding)!
let base64LoginString = loginData.base64EncodedStringWithOptions(nil)
let headers: HTTPHeaders = [
"Content-Type": "application/json",
"Accept": "application/json",
"Authorization": "base64LoginString",
]
let parameters: Parameters = [
"parameter": value,
"parameter2": value2
]
Alamofire.request("http://buddysin.aumkiiyo.com/fbc", method: .post, parameters: parameters, encoding: URLEncoding.default, headers: SFAppServicesManager.sharedInstance.genericHeader()).responseJSON { response in
if let JSON = response.result.value {
let userDictionary = JSON as! NSDictionary
completionHandler(true)
} else {
completionHandler(false)
}
}
}