在VCard中将CNContact发送到swift中的服务器

时间:2016-07-06 14:36:06

标签: json swift contacts vcard

我现有JSON需要更改为将VCard发送到我的服务器。我有VCard进入CNContact对象时,当我在下面添加它时我不知道如何将它放在VCard格式中

 public func updateUser(hostToUse:String ,contact: CNContact, completion : (success: Bool, json: AnyObject? )->Void ) -> Void {


let url =  "http://\(hostToUse)/update"

var json:AnyObject = [:]
let urlPath = NSURL(string: url)
let request = NSMutableURLRequest(URL: urlPath!)
request.HTTPMethod = "POST"
request.setValue("application/x-www-form-urlencoded; charset=utf-8", forHTTPHeaderField: "Content-Type")
var postString  = "data={\"userName\":\"\(contact)\"}"
postString = postString.stringByReplacingOccurrencesOfString(" ", withString: "")
request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding)

let config = NSURLSessionConfiguration.defaultSessionConfiguration()
let session = NSURLSession(configuration: config)
let task = session.dataTaskWithRequest(request, completionHandler: {
    (data, response, error) in
    if error != nil {
        print("json error in login \(error)")
        completion(success : false, json :nil )
    } else {
        do {
            json = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers)
            if(json["result"] as! Int == 0){
                print("Login Failed: \(json)")
                completion(success : false, json :nil )
            } else {
                completion(success : true, json :json )
            }

        } catch {
            print("Login parsing failed: \(json)")
            completion(success : false, json :nil )

        }

    }
})
task.resume()

}

0 个答案:

没有答案