Skip to content

Instantly share code, notes, and snippets.

@erdemildiz
Forked from dmathewwws/URLSession POST.swift
Last active September 26, 2020 16:11
Show Gist options
  • Select an option

  • Save erdemildiz/c75e41a929d7014b934663513cbdf301 to your computer and use it in GitHub Desktop.

Select an option

Save erdemildiz/c75e41a929d7014b934663513cbdf301 to your computer and use it in GitHub Desktop.

Revisions

  1. erdemildiz revised this gist Sep 26, 2020. No changes.
  2. @dmathewwws dmathewwws created this gist Nov 5, 2016.
    35 changes: 35 additions & 0 deletions URLSession POST.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    private static func createUserEventData(user:SKUser, eventType:SKEventType, sticker:Sticker?) {

    // server endpoint
    let endpoint = "https://app.stickerkit.io/userEvent/v1/\(user.projectID)"

    guard let endpointUrl = URL(string: endpoint) else {
    return nil
    }

    //Make JSON to send to send to server
    var json = [String:Any]()

    json[SKUser.PropertyKey.UUID] = user.UUID
    json[SKUser.PropertyKey.projectID] = user.projectID
    json[SKUser.PropertyKey.countryCode] = user.countryCode
    json[SKUser.PropertyKey.deviceType] = user.deviceType
    json[SKEvent.PropertyKey.type] = eventType.rawValue
    json[SKEvent.PropertyKey.stickerID] = sticker?.id ?? ""

    do {
    let data = try JSONSerialization.data(withJSONObject: json, options: [])

    var request = URLRequest(url: endpointUrl)
    request.httpMethod = "POST"
    request.httpBody = data
    request.addValue("application/json", forHTTPHeaderField: "Content-Type")
    request.addValue("application/json", forHTTPHeaderField: "Accept")

    let task = URLSession.shared.dataTask(with: request)
    task.resume()


    }catch{
    }
    }