quinta-feira, 24 de janeiro de 2013

Facebook, iPad and the cookie's issue

The ones who follow me in the social networks were, sometime ago, witnesses of a short storm of "unusual" posts (I fear the less patient ones may have blocked me :P).

One of my most recent activities at Gaz Games was the study of plugin by Prime 31 (company specialized in developing plugins for Unity), the iOS Social Networking Plugin, responsible to connect Unity iOS apps with Facebook, preventing the developer of having to deal with Objective-C to do so. The plugin allows you to publish images, status, links, photo albums and retrieve user information such as email, name and age.

The so called storm of posts was part of a series of tests regarding the settings and functionalities of the plugin, resulting in the publication of many "Awesome iPad + Unity Integration test!" and prototype interfaces in my timeline (all of them deleted shortly after being posted).

Ironically, the plugin, which was acquired with the intention to avoid the direct use of Objective-C, was exactly the cause of a fast study about the language and its integration with Unity.

Here's the situation: develop an iPad app where multiple users would be able to interact with augmented reality and post about it on Facebook.
Here's the problem: most of the apps and systems built for iOS are developed considering that the tablet will have only one owner (and consequently one user). Therefore, although the logout()function is available on the Social Networking Plugin library, it simply invalidates the acess token and the session, but it keeps the user cookies.
Here's the result: it's impossible to use a different account once you logged in for the first time.

Informed by the Prime 31 user on Unity's forum that there was no easy way to solve the "problem", I started the research of how to solve the issue the hard way: to write a function in Objective-C the clean the app cookies between sessions.

After facing a few tutorials about one of the most bizarre sintaxes I've ever seen and learn about the way C-based scripts work with Unity, I found a topic where a developer that shared my problem (but not specifically with an app made with the engine) found a relatively simples solution

#import "CleanCookies.h"
@implementation CleanCookies
extern "C"{
   void SignOut(){
        NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
        for (NSHTTPCookie *each in cookieStorage.cookies) {
            [cookieStorage deleteCookie:each];

The code above, written in Obj-C, runs through and deletes all cookie instances stored by the app using the class NSHTTPCookieStorage, native of the language and responsible for the management of the cookies.

There are two main ways of integrating a non C#, Javascript or Boo script with Unity: you can add the files in a folder named "Plugins" and integrate it with C# though the
 [DllImport()] statement or, when dealing with non C-based languages, convert the scripts into a dll and interact with it normally.

Although relatively simple, it was an issue that took some time, which makes me question the reason why Prime 31 didn't implement that feature themselves. Specially considering I'm not the first one to have that problem.

If you're going through the same situation and have any doubt, don't hesitate to ask. :)

Nenhum comentário:

Postar um comentário