SwiftyVK/SwiftyVK
关于
Easy and powerful way to interact with VK API for iOS and macOS
网站与应用开发实测星标增长
- 24小时
- 0
- 7天
- -1
- 15天
- -1
- 1个月
- 正在收集历史
各周期内的星标净变化,周期相互重叠,并非每日新增。缺失的测量保持空白。星标代表关注度,不代表软件质量。
README.md
原文Easy and powerful way to interact with VK API for iOS and macOS.
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 🔊
Table of contents
- Requirements
- Integration
- Getting started
- Interaction with VK API
- Configuring
- Upload files
- Long Poll
- Share dialog
- FAQ
- License
Requirements
- Swift 5.10 + (Xcode 15.3 +)
- iOS 12.0 +
- macOS 10.13 +
- Xcode 15.3 +
Swift Concurrency APIs require iOS 13.0+, macOS 10.15+. No separate integration is needed: add SwiftyVK using any dependency manager below, then use its async APIs from your structured-concurrency code. The GCD API remains available on the original deployment targets.
Session.validate(redirectUrl:) remains synchronous: it validates a completed OAuth redirect immediately and does not suspend. The async surface covers authorization, API requests, token events, and Long Poll events.
Run the ordinary and analyzer rules locally with:
swiftlint lint --config .swiftlint.yml
bash scripts/swiftlint-analyze.sh
Integration
Swift Package Manager
Minimum deployment targets: iOS 12.0, macOS 10.13.
Add SwiftyVK to your Package.swift dependencies and target as usual:
dependencies: [
.package(url: "https://github.com/SwiftyVK/SwiftyVK.git", from: "3.5.0")
]
Carthage (recommended)
github "SwiftyVK/SwiftyVK"
CocoaPods
use_frameworks!
target '$MySuperApp$' do
pod 'SwiftyVK'
end
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
Getting started
For apps targeting iOS 13.0+ or macOS 10.15+, configure SwiftyVK with closures.
VKTokenEventsStream exposes token lifecycle events as an AsyncStream.
If your app does not use Swift Concurrency, see the GCD guide.
let tokenEvents = VKTokenEventsStream()
VK.setUp(
appId: "YOUR_APP_ID",
scopeProvider: { _ in [.offline, .friends] },
onViewNeedsToPresent: { viewController in
// Present viewController from your current UI context.
},
tokenEvents: .stream(tokenEvents)
)
func observeTokenEvents(_ tokenEvents: VKTokenEventsStream) async {
for await event in tokenEvents.stream {
print(event)
}
}
Run observeTokenEvents from your app's structured-concurrency task tree. VK.release() finishes the stream.
To handle token events with a callback instead, pass tokenEvents: .callback { event in ... }.
Setting up VK application
- Create new standalone application
- Save
application IDfrom Preferences -> Application ID - Set up SwiftyVK with your application ID:
VK.setUp(
appId: "YOUR_APP_ID",
scopeProvider: { _ in [.offline, .friends] },
onViewNeedsToPresent: { viewController in
// Present viewController.
}
)
Releasing
in order to free up resources that holds SwiftyVK use:
VK.release()
note you must setup it again for further using
Authorization
SwiftyVK provides several ways to authorize user. Choose the one that's more suitable for you.
oAuth WebView
This is a standard authorization method which shows web view with oAuth dialog. Suitable for most cases.
func authorize() async throws {
let tokenInfo = try await VK.sessions.default.logIn()
// Start working with SwiftyVK session here.
print(tokenInfo)
}
Cancelling the Swift task immediately throws CancellationError; it does not dismiss an already presented authorization UI.
Official VK Application
If a user has the official VK app installed on their device, SwiftyVK can be authorized using it. To do that:
In Xcode -> Target -> Info -> URL Types
- Add new URL Type like
vk$YOUR_APP_ID$(e.g. vk1234567890) - Add app schemas to Info.plist file:
- Add new URL Type like
<key>LSApplicationQueriesSchemes</key>
<array>
<string>vkauthorize</string>
<string>vk$YOUR_APP_ID$</string>
</array>
Copy
Application Bundlefrom Xcode -> $App Target$ -> General -> Bundle Identifier (e.g. com.developer.applicationName)Set copied
Application Bundleto https://vk.ru/apps?act=manage -> Edit App -> Settings -> App Bundle ID for iOS fieldAdd the following code to AppDelegate:
- For iOS 9 and below
func application(
_ application: UIApplication,
open url: URL,
sourceApplication: String?,
annotation: Any
) -> Bool {
VK.handle(url: url, sourceApplication: sourceApplication)
return true
}
- For iOS 10 and above
func application(
_ app: UIApplication,
open url: URL,
options: [UIApplicationOpenURLOptionsKey : Any] = [:]
) -> Bool {
let app = options[.sourceApplication] as? String
VK.handle(url: url, sourceApplication: app)
return true
}
Authorize as described in oAuth WebView.
If user denies authorization in VK App, SwiftyVK will present oAuth dialog
Raw token string
If you have previously received user token, just pass it to the following method:
VK.sessions.default.logIn(rawToken: String, expires: TimeInterval)
// Start working with SwiftyVK session here
TimeInterval is a time, after which the token will no longer be valid. Pass 0 if you want token to never expire.
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:
Request
The basic request calls look like VK.methodGroup.methodName().
For example, to get short info about current user:
func loadCurrentUser() async throws -> Data {
try await VK.API.Users.get(.empty).send()
}
Do not configure callbacks on a request that you send with await: the two result-handling styles are mutually exclusive.
Object created with
VK.API.Users.get(.empty)
represents a request that can be sent immediately or can be configured first and sent later.
Parameters
If you want to get additional fields for a user in previous example, you can set request parameters:
VK.API.Users.get([
.userId: "1",
.fields: "sex,bdate,city"
])
Use .empty if you don't want to pass any parameters.
Cancellation
func loadCurrentUser() async throws -> Data {
try await VK.API.Users.get(.empty).send()
}
Cancellation is propagated from the parent task; send() throws CancellationError when the task is cancelled.
Chaining
Compose requests with ordinary sequential await calls. Errors and cancellation propagate through the task naturally:
func sendMessage() async throws -> Data {
let response = try await VK.API.Users.get(.empty).send()
let user = try JSONDecoder().decode(User.self, from: response)
return try await VK.API.Messages.send([
.userId: user.id,
.message: "Hello"
]).send()
}
Configuring
In SwiftyVK each session has default configuration for its requests.
Each request gets configuration from its session.
Configuration contains settings such as httpMethod, attemptTimeout and others.
You can change configuration for a single request
// Set different httpMethod only for this request
VK.API.Users.get(.empty)
.configure(with: Config(httpMethod: .POST))
or for the whole session
// Set default apiVersion value for all requests in default session
VK.sessions.default.config.apiVersion = "5.68"
You may change following configuration properties:
| Property | Default | Description |
|---|---|---|
httpMethod |
.GET |
HTTP method. You can use GET or POST. For big body (e.g. long message text in message.send method) use POST method. |
apiVersion |
latest version |
VK API version. By default uses latest version. If you need different version - change this value. |
language |
User system language |
Language of response. For EN Pavel Durov, for RU Павел Дуров. |
attemptsMaxLimit |
3 |
Maximum number of attempts to send request before returning an error. |
attemptTimeout |
10 |
Timeout in seconds of waiting for a response before returning an error. |
handleErrors |
true |
Allow to handle specific VK errors automatically by presenting a dialog to a user when authorization, captcha solving or validation is required. |
Request progress
Uploading requests can emit progress and a final response. Use sendWithProgress():
func uploadPhoto(_ media: Media) async throws {
for try await event in VK.API.Upload.Photo.toWall(media, to: .user(id: "4680178")).sendWithProgress() {
switch event {
case let .progress(progress):
print(progress)
case let .response(response):
print(response)
}
}
}
The GCD onProgress, onSuccess and onError callbacks remain available.
Upload files
SwiftyVK provides the ability to easily upload a file to VK servers. For example:
func uploadPhoto() async throws {
guard let path = Bundle.main.path(forResource: "testImage", ofType: "jpg") else {
return
}
let data = try Data(contentsOf: URL(fileURLWithPath: path))
let media = Media.image(data: data, type: .jpg)
for try await event in VK.API.Upload.Photo.toWall(media, to: .user(id: "4680178")).sendWithProgress() {
switch event {
case let .progress(progress):
print(progress)
case let .response(response):
print(response)
}
}
}
If progress is not needed, upload methods also support the one-result API:
let response = try await VK.API.Upload.Photo.toWall(media, to: .user(id: "4680178")).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.
Long Poll
Use the throwing event stream from a structured-concurrency context:
func consumeLongPoll() async throws {
for try await events in VK.sessions.default.longPoll.eventsStream() {
for event in events {
switch event {
case let .type1(data):
print(JSON(data))
default:
break
}
}
}
}
There may be only one Long Poll consumer: either this stream or a callback consumer.
Creating a second stream finishes it with VKError.longPollAlreadyObserved without stopping the first consumer.
Cancellation of the parent task propagates to consumeLongPoll() and stops the underlying Long Poll.
stop() intentionally does not stop an active async stream.
LongPollEvent has two special cases:
.forcedStop is returned when Long Poll experiences an unexpected error and stops. You can restart it again.
.historyMayBeLost is returned when Long Poll was disconnected from the server for a long time and either lpKey or timestamp is outdated. Refresh data that could have changed while the network was unavailable.
Share dialog
With SwiftyVK can make a post to user wall. To do this, you need:
- Set up SwiftyVK
- SetUp VK application
- Present share dialog with context:
let context = ShareContext(
text: "This post made with #SwiftyVK 🖖🏽",
images: [ShareImage(data: data, type: .jpg)],
link: ShareLink(title: "Follow the white rabbit", url: link)
)
func share(_ context: ShareContext) async throws -> Data {
try await VK.sessions.default.share(context)
}
Images and link are optional, text is required Sharing not available on macOS 10.10. If you want to use it, please make pull request to this repo.
FAQ
I can't find some API method or parameter in library
License
SwiftyVK is released under the MIT license. See LICENSE for details.
探索相关项目
加载中…
