B4A Library Vk.com SDK Wrapper

Introduction

Vk.com is the largest russian social network with a powerful api integration. It is also rapidly expanding in europe with a very large userbase.

Used SocialApi classes:
  • VkProvider: A non activity object that must be declared in any Sub Process_Globals.
  • SocialApiActivity: An activity object that can be only declared in a Sub Globals and is used to bind a VkProvider object to an activity through the VkProvider.SetActivity method.
Getting started
  1. Download the latest socialapi package, extract it anywhere on your hard disk and note the folder name.
  2. Copy the wrapper files (jar + xml) in the socialapi folder into your B4A Libraries folder.
  3. Each sample in the socialapi\vk\samples folder has AdditionalRes and AdditionalJar directives that have to be changed. Change the C:\b4a-dev folder to where you have extracted the socialapi archive.

The samples archive contains:
  • Sample1: Simple connect that can be used as a bare-bone template for your new apps

Step-by-step new app tutorial (based on Sample1)

1. Add following code to your manifest:
B4X:
AddApplicationText(
    <activity
        android:name="com.vk.sdk.VKOpenAuthActivity" />)


2. Add following directives to your project's main file:
B4X:
#AdditionalRes: <your-installation-folder>\socialapi\vk\sdk\res com.vk.sdk
#AdditionalJar: <your-installation-folder>\socialapi\vk\sdk\vksdk.jar


3. Select the SocialApi library from the list of the available libraries in the IDE. Declare a VkProvider and a SocialApiActivity object in Sub Process_Globals and Sub Globals respectively. Also initialize the VkProvider with your app-id.
B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

    Dim Vk As VkProvider : Vk.Initialize("<your-app-id>")
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

    Dim ThisActivity As SocialApiActivity
End Sub


4. Log app's unique HashKey in Sub Activity_Create because you will need this later. Then call the VkProvider.SetActivity method in your Sub Activity_Resume in order to bind the VkProvider instance to the current activity passing the name of the event that will be raised every time the Vk.com session changes.
B4X:
Sub Activity_Create
    Log(Vk.HashKey)
End Sub

Sub Activity_Resume
    Vk.SetActivity(ThisActivity.Initialize("Vk")) 'The event prefix
End Sub


5. Create the event handlers
B4X:
Sub Vk_Event (Provider as SocialApiProvider)
    BtnConnect.Enabled = Not(Provider.Connected)
    BtnDisconnect.Enabled = Provider.Connected
End Sub

Sub Vk_Connected (Provider as SocialApiProvider)
    Msgbox(Provider.Me, "JustConnected!")
End Sub

Sub Vk_Disconnected (Provider as SocialApiProvider)
    Msgbox("Bye bye!", "JustDisconnected!")
End Sub

Sub Vk_Failed (Provider as SocialApiProvider)
    If Msgbox2("Failed to actualize your details."&CRLF&CRLF&"Retry?", "Error", "Yes", "No", "", Null) = DialogResponse.POSITIVE Then
        Provider.Retry
    End If
End Sub


6. Create the login/logout handlers. The login and logout actions are performed asynchronously
B4X:
Sub BtnConnect_Click
    Vk.Login(Array as String (VkConstants.Permissions.FRIENDS))
End Sub

Sub BtnDisconnect_Click
    Vk.Logout
End Sub


7. Do something and evaluate the result
B4X:
Sub BtnMe_Click
    Dim Result As VkResult = Vk.GetMe(Array As String(Vk.Constants.UserFields.BDATE))
    If Result.Success Then
        Msgbox(Result, "Success")
    Else
        Msgbox(Result.Message, "Error")
    End If
End Sub

Done! Now we have to setup our app in the Vk.com Developer Dashboard.

Set up our app in the Vk.com Developer Dashboard

Follow the instructions here: https://vk.com/editapp?act=create


Please test and post any feedback, questions, comments you may have!
That's all for now folks!~

:D
 

Attachments

  • sample1-1.png
    sample1-1.png
    19.1 KB · Views: 354
  • sample1-2.png
    sample1-2.png
    21.4 KB · Views: 215
Last edited:
Top