androidiosobjective-cfacebookapportable

Apportable: how to use android native facebook adk


We are trying to use android native facebook sdk for facebook wall post.

Here we are using Bridge class like this

.h file

#ifdef ANDROID
#import <BridgeKit/JavaObject.h>
#import <BridgeKit/AndroidActivity.h>

@interface BridgeKitExt : JavaObject

- (id)initWithString:(NSString*)str;

- (void)loginFacebook:(AndroidActivity*)activity;

@end

#endif

and .m file like this

#ifdef ANDROID
#import "BridgeKitExt.h"

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wobjc-property-implementation"
#pragma clang diagnostic ignored "-Wincomplete-implementation"
@implementation BridgeKitExt

+ (void)initializeJava
{
    [super initializeJava];

    [BridgeKitExt registerConstructorWithSelector:@selector(initWithString:)
                                    arguments:[NSString className], nil];


    [BridgeKitExt registerInstanceMethod:@"loginFacebook"
                            selector:@selector(loginFacebook:)
                           arguments:@"android/app/Activity", nil];
}

+ (NSString *)className
{
    return @"com.duksel.apportable.BridgeKitExt";
}

@end
#pragma clang diagnostic pop

#endif

BridgeKitExt.java file is like this

package com.duksel.apportable;

import java.util.Arrays;
import java.util.List;

import org.json.JSONObject;

import android.app.Activity;
import android.graphics.Color;
import android.os.Handler;
import android.provider.Settings;
//import android.util.Log;
import android.view.*;
import android.view.ViewGroup.LayoutParams;
import android.widget.*;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
//import android.widget.Toast;

import com.facebook.FacebookRequestError;
import com.facebook.HttpMethod;
import com.facebook.Request;
import com.facebook.RequestAsyncTask;
import com.facebook.Response;
import com.facebook.Session;
import com.facebook.SessionLoginBehavior;
import com.facebook.SessionState;
import com.facebook.Session.Builder;
import com.facebook.Session.OpenRequest;
import com.facebook.model.GraphObject;
import com.facebook.model.GraphUser;

public class BridgeKitExt extends Activity
{
//    private static final String TAG = "BridgeKitExt";

private String initMessage = "";

private String SHARE_MESSAGE="sample text";
private boolean pendingPublishStory = false;

// For Facebook
private static final String TAG = "Facebook";
private static final String KEY_FB_SHARE = "fb_share";
private static final String STORY_PUBLISH_KEY = "story_required_publish";

public void postFacebookWall(String message) {
    SHARE_MESSAGE = message;
    publishStory();
}
private void publishStory() {
    Session session = Session.getActiveSession();
    if (session != null && session.isOpened()) {
        Bundle bundle = new Bundle();
        bundle.putString("message", SHARE_MESSAGE);
        //bundle.putString("link", Purchase_Detail.link);
        //bundle.putString("picture", ICON_LINK);

        Request.Callback callback = new Request.Callback() {
            @Override
            public void onCompleted(Response response) {
                GraphObject object = response.getGraphObject();
                if (object != null) {
                    JSONObject jsonResponse = object.getInnerJSONObject();
                    String postId = "";
                    try {
                        postId = jsonResponse.getString("id");
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    FacebookRequestError error = response.getError();
                    if (error != null) {
//                          Toast(error.getErrorMessage());
                    } else {
//                          Toast("Post Succsessfully Done");
                    }
                } else {
//                      Toast("Error in Posting");
                }
            }
        };

        Request request = new Request(session, "me/feed", bundle,HttpMethod.POST, callback);
        RequestAsyncTask task = new RequestAsyncTask(request);
        task.execute();
    } else {
        pendingPublishStory = true;
        loginFacebook();
    }
}

private void onSessionStateChange(Session session, SessionState state,
                                  Exception exception) {
    if (state.isOpened()) {
//          Log("onSessionStateChange " + state.toString());
        makeMeRequest(session);
        if (pendingPublishStory) {
            pendingPublishStory = false;
            publishStory();
        }
    } else if (state.isClosed()) {
//          Log("onSessionStateChange " + state.toString());
    }
}

private Session.StatusCallback statusCallback = new Session.StatusCallback() {
@Override
public void call(Session session, SessionState state,
                 Exception exception) {
onSessionStateChange(session, state, exception);
}
};

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(data==null)
return;

}
}

@Override
protected void onResume() {
super.onResume();
Session session = Session.getActiveSession();
if (session != null && (session.isOpened() || session.isClosed())) {
onSessionStateChange(session, session.getState(), null);
}
}

@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBoolean(STORY_PUBLISH_KEY, pendingPublishStory);
}

private void makeMeRequest(final Session session) {
Request request = Request.newMeRequest(session,
new Request.GraphUserCallback() {
@Override
public void onCompleted(GraphUser user, Response response) {
if (session == Session.getActiveSession()) {
if (user != null) {
//nothing here
}
}
if (response.getError() != null) {
//Log("Error : newMeRequest");
}
}
});
request.executeAsync();
}
protected void loginFacebook() {
Session currentSession = Session.getActiveSession();
if (currentSession == null || currentSession.getState().isClosed()) {
Session session = new Session.Builder(this).build();
Session.setActiveSession(session);
currentSession = session;
}
if (currentSession.isOpened()) {
//Toast("User Already Logged In");
} else if (!currentSession.isOpened()) {
OpenRequest op = new Session.OpenRequest(this);
op.setLoginBehavior(SessionLoginBehavior.SUPPRESS_SSO);
op.setCallback(statusCallback);
List<String> permissions = Arrays.asList("publish_stream", "email");
op.setPermissions(permissions);
Session session = new Builder(this).build();
Session.setActiveSession(session);
session.openForPublish(op);
}
}
}

Added Android FacebookSDK to project folder and in configuration.json file

"header_paths": ["./FacebookSDK/src"],
"sources": [ "java-wrappers/BridgeKitExt.m" ],
"java_sources": [ "java/src/com/duksel/apportable/BridgeKitExt.java" ],
"java_sourcepaths": [ "java/src" , "FacebookSDK/src" ],

but when build, getting this error

.../BridgeKitExt.java:128: class, interface, or enum expected
Session session = Session.getActiveSession();
^
/BridgeKitExt.java:129: class, interface, or enum expected
if (session != null && (session.isOpened() || session.isClosed())) {
^

Solution

  • It seems like there is just a syntax error in your BridgeKitExt.java file...

    It's hard to match braces in the code you posted because the indenting seems off, but is there maybe an extra curly brace at the end of your onActivityResult function?