Callbacks
Requests are executed asynchronously and provide some callbacks for handling execution results:
Cancellation
If you no longer need to send sheduled request (e.g. screen was popped out), just cancel it:
Chaining
SwiftyVK allows you to chain requests. If your second request needs to consume a response from the first one, just chain them together:
You can make very long chains with SwiftyVK!
Handle updates
Data format is described here.
LongPollEvent is an enum with associated value of type Data in each case.
You can parse this data to JSON using your favorite parser like this:
LongPollEvent has two special cases:
.forcedStop – returned when LongPoll has experienced unexpected error and stop. You can restart it again.
.historyMayBeLost – returned when LongPoll was disconnected from server for a long time
and either lpKey or timestamp is outdated.
You do not need to reconnect LongPoll manually, client will do it itself.
Use this case to refresh data that could have been updated while network was unavailable.
Implement swiftyvkdelegate
To start using SwiftyVK you should implement SwiftyVKDelegate protocol in your custom VKDelegate class.
It is used to notify your app about important SwiftyVK lifecycle events.
For example:
See full implementation in Example project
Interaction with vk api
SwiftyVK provides a very simple interface for interaction with VK API.
All requests are performed asynchronously in a private queue by API scheduler
(the scheduler sends no more than 3 requests per second by default).
You can just send a request and get a response without a lot of work.
All API methods are listed here
Let’s look closer to requests syntax:
Key features
😊 It’s not ios-vk-sdk😊
🍏 One library for iOS and mac OS 🍏
🤘 Fully written in Swift and doesn’t contain any Objective-C code 🤘
🎮 Very simple interface, made with care about those who will use it 🎮
⛑ Fully strong typed that you can not shoot yourself in the leg ⛑
🏆 High code quality with lot of unit tests, linter integration and CI 🏆
🚀 Frequent updates and bug fixes 🚀
🔊 LongPoll support 🔊
License
SwiftyVK is released under the MIT license.
See LICENSE for details.
Manually
- Just drag SwiftyVK.framework or include the whole SwiftyVK.xcodeproj into project
- Link SwiftyVK.framework with application in Your target preferences -> General -> Embedded binaries
Oauth webview
This is a standard authorization method which shows web view with oAuth dialog. Suitable for most cases.
Onsuccess
This callback will be called when request has succeeded and returned Data object.
You can handle and parse response using any JSON parsing method
(e.g. JSONSerialization, Codable, SwiftyJSON and others)
You can throw errors in onSuccess callback, which will cause onError to be called with your error.
Releasing
in order to free up resources that holds SwiftyVK use:
note you must setup it again for further using
Request
The basic request calls look like VK.methodGroup.methodName().
Requirements
- Swift 4.0
- iOS 8.0
- macOS 10.10
- Xcode 9.0
Setting up vk application
- Create new standalone application
- Save
application ID
from Preferences -> Application ID - Set up SwiftyVK with
application ID
andVKDelegate
obtained in the previous steps:
Stop longpoll
If you don’t need to receive LongPoll updates anymore, just call this function:
Upload files
SwiftyVK provides the ability to easily upload a file to VK servers. For example:
// Get path to image fileguardlet path = Bundle.main.path(forResource: "testImage", ofType: "jpg") else { return }
// Get data from image file by pathguardlet data =tryData(contentsOf: URL(fileURLWithPath: path)) else { return }
// Create SwiftyVK Media representation from given datalet media = Media.image(data: data, type: .jpg)
// Upload image to serverVK.API.Upload.Photo.toWall(media, to: .user(id: "4680178"))
.onSuccess { print($0) }
.onError { print($0) }
.onProgress {
// This callback available only for uploading requests// Use it to handle uploading status and show it to userswitch$0 {
caselet .sent(current, of):print("sent", current, "of": of)
caselet .recieve(current, of):print("recieve", current, "of": of)
}
}
.send()
Some upload requests do not immediately download files
e.g VK.API.Upload.Photo.toMessage will return photoId
which you can use in messages.send method.
See docs for more info.