Webservices (v5)

Use the Apps Panel iOS SDK to communicate with your backend.

The iOS Apps Panel SDK provides the easiest way to communicate with your web services. It implements the main http methods so you can take full advantage of the RESTful API provided by the Apps Panel back end.

Making your first call

πŸ“˜

In order for you to be able to make web services calls, your app must be configured. For help on how to do this, see Getting started.

RequestManager is the class that handle calls to web services. It adds all the required headers and eases certain aspects of making a HTTP request.

In most cases, you can use the default request manager. It will define the base URL by itself, depending on the app configuration.

RequestManager.default
  .request("users", method: .get, parameters: ["id": 1234])
  .responseData { result in
    switch result {
    case .success(let response):
        print(response.data)
    case .failure(let error):
        print(error)
    }
}

You can also decode automatically Decodable types.

struct User: Decodable {}

RequestManager.default
  .request("users", method: .get, parameters: ["id": 1234])
  .responseObject(User.self, jsonDecoder: JSONDecoder()) { result in
    switch result {
    case .success(let response):
        print(response.object)
    case .failure(let error):
        print(error)
    }
}

Options

There are multiple options for the requests that you can enable or not.

Client-side caching

If a client-side caching is configured on your web services, you might want to increase the default cache size. By default, the request manager uses URLCache.shared to cache the responses. Add the following lines, with the desired values, to you app delegate to change the size of the cache.

URLCache.shared.memoryCapacity = 20 * 1024 * 1024
URLCache.shared.diskCapacity = 40 * 1024 * 1024

Sending files

Uploading files alongside some JSON as form data is currently not possible. This will come in a future release.

However, there is another way to upload files on our servers. Apps have dedicated web services to upload files and, more specifically, images. The SDK provides an easy way to interact with those.

Keep in mind you can't send data alongside files. You will need to perform another request with the URL of your uploaded file.

❗️

Authentication required

Uploading files that way requires an authentication token, otherwise the request will fail. Don't forget to save the user's token with AuthenticationTokenManager beforehand.
See User Token Management for more information.

Basic file upload

The first upload method can be used for any file. You'll receive an URL of the uploaded file.

You can define an entity with a name and an optional id. This acts like a namespace. This allows to store files in a proper way on server. It will also add path segments to the URL. This is mainly a best practice feature. This won't have any impact on the app side.

For instance, if you want to send the profile picture of a user, you can define the entity like so:

let userEntity = UploadEntity(name: "users", id: "123")

The entity name can include slashes ("/") to create nested namespace/directory.

File objects have a default filename if you don't provide one. It must include the extension of the file.
Also, the SDK will try to determine the mime type from the binary. If it fails, a default one will be used. We recommend providing the mime type yourself if you can.

let pdf = try! Data(contentsOf: pdfURL)
let file = File(data: pdf)
let fileEntity = UploadEntity(name: "documents/reports")
let fileUpload = FileUpload(file: file, entity: fileEntity)

RequestManager.default.upload(fileUpload) { result in
    switch result {
    case .success(let response):
        print(response)
    case .failure(let error):
        print(error)
    }
}

Image upload

The other upload method can only be used for images. It allows you to generate multiple sizes of your image depending on the different use cases in your app. You'll receive an URL for each size of the uploaded image, including the original one.

If you're not interested in getting different sizes of your image, use the basic file upload instead.

let image = try! Data(contentsOf: imageURL)

let imageEntity = UploadEntity(name: "users", id: "123")

let uploadOptions = ImageUpload.Options(
    smallSize: ImageUpload.Size(width: 50, height: 50),
    mediumSize: ImageUpload.Size(width: 100, height: 100),
    largeSize: ImageUpload.Size(width: 500, height: 500)
)

let imageUpload = ImageUpload(
    data: image,
    entity: imageEntity,
    options: uploadOptions
)

RequestManager.default.upload(imageUpload) { result in
    switch result {
    case .success(let response):
        print(response)
    case .failure(let error):
        print(error)
    }
}

Options and sizes are optional. If you do not specify sizes, the server will use its own default values.

Global headers

You may want to add specific headers to all your requests. As of version 5.4.0, you can add global headers like this:

AppsPanel.shared.customHeaders = ["Custom-Header": "ABC"]