Manually installing Twitter Plugin for Cordova (ios)
I always wished that Cordova would have come as default with a whole kitchen-sink full of plugins pre-installed as default, as it used to in the early days, when it was still called Phonegap. But alas, you have to install a plethora of basic functionality as plugins now. I guess it keeps app lean and small, and It’s just the way it is.
Anyway, I normally start an app from the command line, and include every plugin I think I would need, then once I’m happy with a “Hello World”, build the app, and then start working from XCode instead of the command line.
However, this does lead to a problem, whenever I want to add a new plugin after moving to XCode. It is much much much easier to use the command line tools to add a plugin automatically, using something like
cordova plugin add https://github.com/etiennea/phonegap-twitter-plugin.git
However, this post is for people who can’t do this, and have to resort to manually installing a plugin
So, the first step is to look at the plugin.xml, and see what is required, which boils down to three parts
<js-module src=”www/TwitterPlugin.js” name=”TwitterPlugin”>
<clobbers target=”Twitter” />
</js-module>
Which equates to a change in cordova_plugins.js with this code:
{
“file” : “plugins/TwitterPlugin.js”,
“id” : “TwitterPlugin”,
“clobbers”: [
“Twitter”
]
}
Next, is you look at this bit of XML:
<feature name=”TwitterPlugin”>
<param name=”ios-package” value=”TwitterPlugin” />
</feature>
Which you copy verbatim into the config.xml
<header-file src=”src/ios/TwitterPlugin.h” />
<source-file src=”src/ios/TwitterPlugin.m” />
Those files, just mean that you add the header and module file into your project, in the plugins folder
<framework src=”Twitter.framework” />
<framework src=”Accounts.framework” weak=”true” />
Mean that you have to add those frameworks (see Build Phases > Linked libraries) before the code will compile
Now, the real gotcha, is that if you try to copy/paste the javascript file into the project, it will cause a JavaScript exception because it won’t recognise “require” or “module”… This is because you need to modify the Javascript file by adding the call
cordova.define("TwitterPlugin", function(require, exports, module) {. <file contents>..}
The experienced JavaScript developer will see where “require” and “module” come from now!