Search code examples
facebooktitanium-mobiletitanium-alloytitanium-modulestitanium-android

Object #<Facebook> has no method 'initialize' Titanium Alloy


I have a problem with my Titanium app ... I looked for some tutorials on the internet and Appcelerator on facebook module, more specifically login / logout. I'm trying to adjust this error the day, but I can not in any way ... I am offering my project for download if you can download and test would be helpful!

I'm trying to log in to android, but the app has to be for ios too ... if you can tell me how I do for me two or indicate items that really work ....

Download my Project: http://we.tl/hqvEpb2aeG

Below my files:

Login.js

//Abir Fluxo de Caixa
function openFluxoDeCaixa(){
    var winFluxo = Alloy.createController('fluxo').getView();
    winFluxo.open();
}

fb.addEventListener('login', fbLogin);

$.btnLoginFacebook.addEventListener('click',function(e){
    fb.authorize();
});


function fbLogin(e){

    fb.removeEventListener('login', fbLogin);

    if(e.success){

        var fbdata = {};
        fbdata = JSON.parse(e.data);

        var uFoto = 'https://graph.facebook.com/' + fbdata.id + '/picture?width=300&height=300';
        var uNome = fbdata.name;

        var slipName = uNome.split(" ");

        Ti.App.Properties.setString('logged', 'true');
        Ti.App.Properties.setString('foto', uFoto);
        Ti.App.Properties.setString('nome', slipName[0]);

        openFluxoDeCaixa();
    } 
}


$.windowLogin.open();

Alloy.js

var fb = require('facebook');
fb.permissions = ['public_profile', 'read_stream'];
fb.appid = 172237596444060;
fb.forceDialogAuth = false;
fb.initialize();

if(OS_ANDROID){
    $.windowLogin.fbProxy = fb.createActivityWorker({lifecycleContainer: $.windowLogin});
}



Alloy.Globals.LocalFunctions = {
    setLogin : function setLogin(uId, uNome, uPlataforma){

    var getLogin = Titanium.Network.createHTTPClient();
        getLogin.open("POST","http://www.skrow.cc/ws-qia/controller.php");

        var params = {
            action: 'addLogin',
            id: uId,
            nome: uNome,
            plataforma: uPlataforma
        };

        getLogin.send(params);
    }
};

TiApp.xml

<?xml version="1.0" encoding="UTF-8"?>
<ti:app xmlns:ti="http://ti.appcelerator.org">
    <id>br.com.queroinvestiragora</id>
    <name>Quero Investir Agora</name>
    <version>1.0</version>
    <publisher>silviosampaio</publisher>
    <url>http://www.queroinvestiragora.com.br</url>
    <description>undefined</description>
    <copyright>2015 by silviosampaio</copyright>
    <icon>appicon.png</icon>
    <fullscreen>false</fullscreen>
    <navbar-hidden>false</navbar-hidden>
    <analytics>true</analytics>
    <guid>284edc0c-c599-4cae-9444-721cd3d22937</guid>
    <property name="ti.ui.defaultunit" type="string">dp</property>
    <ios>
        <plist>
            <dict>
                <key>UISupportedInterfaceOrientations~iphone</key>
                <array>
                    <string>UIInterfaceOrientationPortrait</string>
                </array>
                <key>UISupportedInterfaceOrientations~ipad</key>
                <array>
                    <string>UIInterfaceOrientationPortrait</string>
                    <string>UIInterfaceOrientationPortraitUpsideDown</string>
                    <string>UIInterfaceOrientationLandscapeLeft</string>
                    <string>UIInterfaceOrientationLandscapeRight</string>
                </array>
                <key>UIRequiresPersistentWiFi</key>
                <false/>
                <key>UIPrerenderedIcon</key>
                <false/>
                <key>UIStatusBarHidden</key>
                <false/>
                <key>UIStatusBarStyle</key>
                <string>UIStatusBarStyleDefault</string>
                <key>CFBundleURLTypes</key>
                <array>
                    <dict>
                        <key>CFBundleURLName</key>
                        <string>com.appcelerator.kitchensink</string>
                        <key>CFBundleURLSchemes</key>
                        <array>
                            <string>kitchensink</string>
                            <string>936940282991759</string>
                        </array>
                    </dict>
                </array>
            </dict>
        </plist>
    </ios>
    <android xmlns:android="http://schemas.android.com/apk/res/android">
        <manifest>
            <application android:theme="@style/Theme.NoActionBar">
                <activity android:label="@string/app_name"
                    android:name="com.facebook.LoginActivity" android:theme="@android:style/Theme.Translucent.NoTitleBar"/>
                <meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/app_id"/>
            </application>
        </manifest>
    </android>
    <mobileweb>
        <precache/>
        <splash>
            <enabled>true</enabled>
            <inline-css-images>true</inline-css-images>
        </splash>
        <theme>default</theme>
    </mobileweb>
    <modules>
        <module platform="commonjs">ti.cloud</module>
        <module platform="android" version="3.0.3">facebook</module>
        <module platform="iphone" version="3.2.0">facebook</module>
    </modules>
    <deployment-targets>
        <target device="android">true</target>
        <target device="blackberry">false</target>
        <target device="ipad">false</target>
        <target device="iphone">true</target>
        <target device="mobileweb">false</target>
    </deployment-targets>
    <sdk-version>3.5.1.GA</sdk-version>
    <plugins>
        <plugin version="1.0">ti.alloy</plugin>
    </plugins>
    <property name="appc-app-id" type="string">55ec85b523ec2d9730087189</property>
    <property name="acs-authbase-url-development" type="string">https://secure-identity.cloud.appcelerator.com</property>
    <property name="acs-base-url-development" type="string">https://api.cloud.appcelerator.com</property>
    <property name="acs-oauth-secret-development" type="string">0VlBf7xO3Z3aWr2ISM6BXr2vxQH8OPaU</property>
    <property name="acs-oauth-key-development" type="string">T8j8E5AOcEyUGOPtSZWaA4ygJLpi0pYL</property>
    <property name="acs-api-key-development" type="string">5p3UAhCmk7I3H9lYzip5Wb97H0h9I38V</property>
    <property name="acs-username-development" type="string">appc_app_user_dev</property>
    <property name="acs-password-development" type="string">fUoxkjYTGqxoE8ZTJ0</property>
    <property name="acs-authbase-url-production" type="string">https://secure-identity.cloud.appcelerator.com</property>
    <property name="acs-base-url-production" type="string">https://api.cloud.appcelerator.com</property>
    <property name="acs-oauth-secret-production" type="string">2JHJ4oZ4V0Ch5Gvy3GAnEkY4stnycUYn</property>
    <property name="acs-oauth-key-production" type="string">UTUK7lzz2Vuq54fSAErTk4NaN3kghPOJ</property>
    <property name="acs-api-key-production" type="string">3xGYGFJQAYXBK4quHslbKCfBtgAcCgXg</property>
    <property name="acs-username-production" type="string">appc_app_user</property>
    <property name="acs-password-production" type="string">rDD4YG0qQxtmdXg0yM</property>
    <property name="appc-org-id" type="string">100026191</property>
    <property name="appc-creator-user-id" type="string">baea2ab2-d377-4f02-aa1b-6b4553ffe12d</property>
</ti:app>

Solution

  • alloy.js is always executed when your app starts but variables you create inside it are not accessible from other controllers by default.

    You will need to make your facebook module accessible in the global scope to access it outside alloy.js, you can do this by changing the following lines in alloy.js:

    var fb = require('facebook');
    fb.permissions = ['public_profile', 'read_stream'];
    fb.appid = 172237596444060;
    fb.forceDialogAuth = false;
    fb.initialize();
    

    To:

    Alloy.Globals.fb = require('facebook');
    Alloy.Globals.fb.permissions = ['public_profile', 'read_stream'];
    Alloy.Globals.fb.appid = 172237596444060;
    Alloy.Globals.fb.forceDialogAuth = false;
    

    In login.js you also need to set the callback function for a successful facebook login before calling fb.initialize() Eg:

    Alloy.Globals.fb.addEventListener('login', fbLogin);
    

    Also you must set your login/logout functions before you try to call fb.initialize() or fb.authorize() so you will need to move fb.initialize() to login.js or just remove it from alloy.js since you are already calling fb.authorize().