Appcelerator Titanium: Learning by examples #4


Titanium modules: Facebook

  1. Module collections: https://marketplace.appcelerator.com/home. For Facebook we do not need to download anything from market place.
  2. Download a module and save it under /modules dir, inside Titanium SDK folder. Make sure to place module in proper OS (iOS, Android etc)
  3. Add the module in tiapp.xml file. Click on Add button and add Facebook
  4. Add the module in alloy.js file. For example,
    Alloy.Globals.Facebook = require('facebook')
  5. Lets add Login button. Add the code in index.html
    <LoginButton id="fbLoginButton" ns="Alloy.Globals.Facebook"/
  6. Add the code in index.js
  7. var facebook = Alloy.Globals.Facebook;
    facebook.appid = 122761107792145;
    facebook.permissions = ['publish_stream'];
    facebook.addEventListener('login', function(e) {
    	if (e.success) {
    		alert('Logged in!');
    	}
    });
    
  8. For iOS, the following code need to be added manually in tiapp.xml file.
    <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>FacebookAppID</key>
                <string>1227611077092145</string>
                <key>FacebookDisplayName</key>
                <string>MyAppName</string>
                <array>
                    <dict>
                        <key>CFBundleURLName</key>
                        <!-- Application ID same as the id value in the tiapp.xml file -->
                        <string>1227611077092145</string>
                        <key>CFBundleURLSchemes</key>
                        <array>
                            <!-- Prefix the Facebook App ID with 'fb' -->
                            <string>fb1227611077092145</string>
                        </array>
                    </dict>
                </array>
                </dict>
            </plist>
        </ios>
    

File System

function saveFile() {
	var file = Ti.Filesystem.getFile(Ti.Filesystem.applicationCacheDirectory, "file.txt");
	/** other options are
	 * Ti.Filesystem.applicationDataDirectory
	 * Ti.Filesystem.applicationDirectory
	 * Ti.Filesystem.applicationSupportDirectory
	 */
	
	if (file.exists) {
		file.deleteFile();
	}
	file.write("hello world from file");
	file = null;
	
}

function loadFile() {
	var file = Ti.Filesystem.getFile(Ti.Filesystem.applicationCacheDirectory, "file.txt");
	alert("File content: " + file.read().text);
	file = null;
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s