This page provides documentation for the SwiftyVK framework, which is designed to simplify the integration of VK (VKontakte) API into Swift applications. Whether you’re building a social networking app or adding VK functionality to your existing app, SwiftyVK is here to help.
Installation
To use SwiftyVK, you need to install it in your project. You can do this either manually or by using CocoaPods.
Manual Installation
- Download the SwiftyVK framework from the official GitHub repository: https://github.com/AndrewShmig/SwiftyVK
- Drag and drop the SwiftyVK.xcodeproj file into your project’s file structure.
- Select your project’s target and open the “General” tab.
- Scroll down to the “Frameworks, Libraries, and Embedded Content” section.
- Click the “+” button and choose the SwiftyVK.framework.
- Make sure that SwiftyVK is added to your “Linked Frameworks and Libraries” and “Embed & Sign” sections.
CocoaPods Installation
If you prefer using CocoaPods, simply add the following line to your Podfile:
pod 'SwiftyVK', '~> 2.0'
Configuration
Before you can start using SwiftyVK, you need to configure it with your VK API credentials.
- Create an application on VK using the VK App Settings page.
- Make a note of your Application ID and your Service Token.
- In your AppDelegate.swift file, add the following code:
// Import SwiftyVK module
import SwiftyVK
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Configure SwiftyVK with your VK API credentials
VK.setUp(appId: "YOUR_APP_ID", delegate: self)
return true
}
Usage
SwiftyVK provides a convenient API for working with VKontakte. Here are some examples to get you started:
Authorization
SwiftyVK simplifies the process of user authorization. You can authorize users with VK using the following code:
VK.sessions.default.logIn(
onSuccess: { info in
// User authorized successfully
},
onError: { error in
// An error occurred during authorization
}
)
Making API Requests
You can make requests to VK API endpoints using SwiftyVK’s API methods. Here’s an example:
VK.API.Users.get(
[.userId: "1"],
onSuccess: { response in
// Handle the successful response
// response["response"] contains the retrieved user information
},
onError: { error in
// Handle the error
}
)
Additional Features
SwiftyVK provides additional features to enhance your VKontakte integration. Here are some notable features:
Long Poll
SwiftyVK includes support for Long Polling, allowing you to receive real-time updates from VK for things like messages, friend requests, and more. You can enable Long Polling by:
- Importing the SwiftyVKLongPoll module into your project.
- Adding the following code:
VK.setUpLongPoll(
with: VKLongPoll.Configuration(
useSynchronization: true,
rules: [.messageNew],
startFrom: .lastStored
),
delegate: self
)
To handle Long Poll updates, you need to implement the delegate methods provided by VKLongPollDelegate protocol.
func receivedNewMessages(messages: [VKLPMessage])
– called when new messages are received.func receivedUserTypingInDialog(userId: VKLPUserId)
– called when a user starts typing.func receivedUserTypingInChat(userId: VKLPUserId, chatId: VKLPChatId)
– called when a user starts typing in a chat.func receivedFriendsAreOnline(friends: [VKLPUserId])
– called when friends come online.func receivedFriendsAreOffline(friends: [VKLPUserId])
– called when friends go offline.
Extensions
SwiftyVK provides additional extensions to simplify VK integration:
VKUIViewController
– Use this extension to present VK authorization view controllers.VKDelegate
– Implement VKDelegate methods to handle SwiftyVK authorization callbacks.
With SwiftyVK, integrating VK functionality into your Swift application has never been easier. By following this documentation and exploring the official GitHub repository, you can unlock the full potential of VK API in your app.