""

Archive for October, 2012

Another post on phonegap and my not so called “adventure”. This time something interesting happened. I am working on phonegap project targeting both iphone and android devices. Its mostly been REST based. So i was testing on various android and iphone devices, things went well except few things like static footer and header not working from android 2.x and below phones but i’l come to this issue on my next post. I hadn’t tested on android 4.0 and above, so yeah when i ran the app on icecream sandwich(Sony Xperia U and Samsung galaxy s2) mighty exception punched me on my face. Here is the long excpetion trace

10-26 10:19:45.998: E/AndroidRuntime(23917): FATAL EXCEPTION: main
10-26 10:19:45.998: E/AndroidRuntime(23917): android.os.NetworkOnMainThreadException
10-26

10:19:45.998: E/AndroidRuntime(23917):  at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
10-26 10:19:45.998: E/AndroidRuntime

(23917):  at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
10-26 10:19:45.998: E/AndroidRuntime(23917):  at

java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
10-26 10:19:45.998: E/AndroidRuntime(23917):  at java.net.InetAddress.getAllByName

(InetAddress.java:220)
10-26 10:19:45.998: E/AndroidRuntime(23917):  at libcore.net.http.HttpConnection.(HttpConnection.java:71)
10-26 10:19:45.998:

E/AndroidRuntime(23917):  at libcore.net.http.HttpConnection.(HttpConnection.java:50)
10-26 10:19:45.998: E/AndroidRuntime(23917):  at

libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:351)
10-26 10:19:45.998: E/AndroidRuntime(23917):  at

libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:86)
10-26 10:19:45.998: E/AndroidRuntime(23917):  at

libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
10-26 10:19:45.998: E/AndroidRuntime(23917):  at

libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308)
10-26 10:19:45.998: E/AndroidRuntime(23917):  at

libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.makeSslConnection(HttpsURLConnectionImpl.java:460)
10-26 10:19:45.998: E/AndroidRuntime(23917):  at

libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.connect(HttpsURLConnectionImpl.java:432)
10-26 10:19:45.998: E/AndroidRuntime(23917):  at

libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282)
10-26 10:19:45.998: E/AndroidRuntime(23917):  at libcore.net.http.HttpEngine.sendRequest

(HttpEngine.java:232)
10-26 10:19:45.998: E/AndroidRuntime(23917):  at libcore.net.http.HttpURLConnectionImpl.getResponse

(HttpURLConnectionImpl.java:273)
10-26 10:19:45.998: E/AndroidRuntime(23917):  at libcore.net.http.HttpURLConnectionImpl.getInputStream

(HttpURLConnectionImpl.java:168)
10-26 10:19:45.998: E/AndroidRuntime(23917):  at libcore.net.http.HttpsURLConnectionImpl.getInputStream

(HttpsURLConnectionImpl.java:270)
10-26 10:19:45.998: E/AndroidRuntime(23917):  at com.facebook.android.Util.openUrl(Util.java:215)
10-26 10:19:45.998:
.
.

(FbDialog.java:146)
10-26 10:19:45.998: E/AndroidRuntime(23917):  at android.webkit.CallbackProxy.uiOverrideUrlLoading(CallbackProxy.java:234)
10-26

10:19:45.998: E/AndroidRuntime(23917):  at android.webkit.CallbackProxy.handleMessage(CallbackProxy.java:349)
10-26 10:19:45.998: E/AndroidRuntime(23917):  

at android.os.Handler.dispatchMessage(Handler.java:99)
10-26 10:19:45.998: E/AndroidRuntime(23917):  at android.os.Looper.loop(Looper.java:137)
10-26

10:19:45.998: E/AndroidRuntime(23917):  at android.app.ActivityThread.main(ActivityThread.java:4507)
10-26 10:19:45.998: E/AndroidRuntime(23917):  at

java.lang.reflect.Method.invokeNative(Native Method)
10-26 10:19:45.998: E/AndroidRuntime(23917):  at java.lang.reflect.Method.invoke(Method.java:511)
10-26 10:19:45.998: E/AndroidRuntime(23917):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
10-26 10:19:45.998:

E/AndroidRuntime(23917):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
10-26 10:19:45.998: E/AndroidRuntime(23917):  at

dalvik.system.NativeStart.main(Native Method)

So exception thrown was NetworkOnMainThreadException, so first thing, i checked out the documentation for the details. So according to reference:

“The exception that is thrown when an application attempts to perform a networking operation on its main thread.

This is only thrown for applications targeting the Honeycomb SDK or higher. Applications targeting earlier SDK versions are allowed to do networking on their main event loop threads, but it’s heavily discouraged”.

So one thing became clear to me that phonegap-facebook plugin is performing networking operation for facebook on main thread rather than separate thread like asynctask. From API level 11, httpclient is not allowed to execute on main thread. So i found the way to remove this restriction by changing the thread policy. Here is the small snippet that goes on onCreate():

if (android.os.Build.VERSION.SDK_INT >= 11) {
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);
}

initialize that view: Storyboard

This post is about a problem that is basic but starters are likely to fall into if not careful. When using a tableview, there will be a time when there will be a need to use custom table cell class. There will be outlets defined for views inside cell. Now there will be a time for need of initializing views. Now there is a chance of falling into one common problem when trying to initialize views of cell that is being loaded from Storyboard. What could be happening is stuff doesn’t get initialized when cell is created. Only one thing comes to mind is that init is not getting called at all. So lets see how init method looks like when xcode creates UITableViewCell subclass for us:

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
    }
    return self;
}

Yeah so we have initwithstyle for intializing our stuff. But when we write any initializing code then it doesn’t work. Thing is that when your views are being loaded from storyboard then initWithCoder is used not initwithstyle. So what we need is this:

- (id)initWithCoder:(NSCoder *)aDecoder{
    self = [super initWithCoder:aDecoder];
    if (self) {
        // Initialization code
    }
    return self;
}

Thanks.

I am tweeting: Android

Someone might be looking for integrating twitter to their android application. I found few alternatives(more than enough) on the web. I’l be talking about few of them on my upcoming posts. All this libraries are unofficial, not provided by twitter itself as there isn’t any official twitter sdk available for android. In this post i’l be talking about one library that i recently used, its Twitter4J. I am again not writing any tutorial for using it. I am here to share some pitfall i fell into, which many people can face for first time. But don’t worry, there is THIS BLOG which has full detail on setting up Twitter4J with your project and get you started tweeting in no time(techically 30 minutes :P).

So i suppose now you have setup the project as detailed in that blog. Now if everything is working than Good Job, if its not working for you yet then go through the steps again and see if you are not missing something. But if you are having problem that i faced and which i will be talking about it here then “GOOD JOB” too 😛

I faced two problems which are very basic but critical.

“oauth.signpost.exception.OAuthNotAuthorizedException: Authorization failed (server replied with a 401). This can happen if the consumer key was not correct or the signatures did not match.”

This error gave me lotsa pain since it was very difficult to find the reason. I spent many hours figuring out what was happening. Error is very specific which states that consumer key might not be matching. But i cross checked keys and everything, everything was in place, so what could be the possible reason ? Then all of a sudden, somehow i just fixed the issue like hitting my hand everywhere or shooting the arrow in dark. I was so desperate. So thing that was causing this issue was, during resgistration of app on dev.twitter.com, Callback URL value wasn’t provided. Yeah i didn’t provide any callback url because it was optional, so i skipped it. So i provided a callback url, you can provide anything there. Now you’l find that above error doesn’t happens anymore and you can start tweeting from your app.

“error – Read-only application cannot POST”

Second issue i faced was this, and this one is very easy to figure out and fix it. It means that your registered twitter app is read only. So go to settings page of your application and make it “read and write”. Thats it.

I am showing you the screenshot of how settings will look like.

Adios.

That’s my unique identity: IPhone

Recently i had a requirement to send unique identifier for device to my web service. I was going through the documentation of UIDevice class at apple’s developers site. Apple has depreceated the “uniqueIdentifier” property from IOS 5 onwards. My application project traget was IOS 5 onwards. I did a search on alternatives on web and forums, got many options. In this post i am not giving any tutorial, just about what option i chose and what silly mistake i made while integrating it into my project. One good option was recommended to me by many sources and after using it i would myself recommend this to others. I read Unique Identifier Is Dead, Long Live Unique Identifier”, author has stated the recommendation provided by apple, its pros and cons and better way to use them. Author has implemented the class that encapsulated all the work of getting and storing identifier. So i would recommend to read that blog.

It was very simple to integrate into project, just need to copy and reference one header and implementation file. So now time to state what silly mistake i made, after integrating those implementation files, i ran my project and got so many errors. I am posting some of errors below:

Undefined symbols for architecture i386:
“_SecItemUpdate”, referenced from:
+[BPXLUUIDHandler storeUUID:] in BPXLUUIDHandler.o
“_SecItemAdd”, referenced from:
+[BPXLUUIDHandler storeUUID:] in BPXLUUIDHandler.o
“_SecItemCopyMatching”, referenced from:
+[BPXLUUIDHandler UUID] in BPXLUUIDHandler.o
+[BPXLUUIDHandler reset] in BPXLUUIDHandler.o
“_SecItemDelete”, referenced from:
+[BPXLUUIDHandler reset] in BPXLUUIDHandler.o
“_kSecAttrAccount”, referenced from:
_CreateKeychainQueryDictionary in BPXLUUIDHandler.o
“_kSecAttrService”, referenced from:
_CreateKeychainQueryDictionary in BPXLUUIDHandler.o
“_kSecClass”, referenced from:
_CreateKeychainQueryDictionary in BPXLUUIDHandler.o
“_kSecClassGenericPassword”, referenced from:
_CreateKeychainQueryDictionary in BPXLUUIDHandler.o
“_kSecReturnAttributes”, referenced from:
+[BPXLUUIDHandler UUID] in BPXLUUIDHandler.o
+[BPXLUUIDHandler reset] in BPXLUUIDHandler.o
“_kSecReturnData”, referenced from:
+[BPXLUUIDHandler UUID] in BPXLUUIDHandler.o
“_kSecValueData”, referenced from:
+[BPXLUUIDHandler storeUUID:] in BPXLUUIDHandler.o

So it seemed “SecItemAdd”, “SecItemUpdate” etc were not being processed or recognized. After experiencing similar “Undefined symbols for architecture i386” errors many times in past, i can tell what could be the primary reason. The most primary cause could be that appropriate framework hasn’t been linked up to target. So my first job was to search on SecItemAdd and all above stuff and look for framework they belong to. It seemed that they belong to security farmework. So once again i happened to forgot including the framework. Once everything goes fine then just do:

import "BPXLUUIDHandler.h"

and

[BPXLUUIDHandler UUID]

So anyone who happens to fall into this pitfall like me, don’t forget to link “Security.framework” to your project target.

Thanks.

Phonegap doing facebook

I am here again to write on few more pitfalls that i fell into while setting up facebook-phonegap plugin. Set up is explained in the github project site. Just read the steps carefully and follow them one by one, don’t skip steps just for safety. Steps are clear enough to have setup your facebook plugin with phonegap project on xcode. My story starts when i finished all steps and finished setting up my project. So after everything was covered i built my project and fell straight into issues (not many issues and this particular one wasn’t any big):

1) “SenTestingKit.h not found”, yeah so some files from facebook sdk were using SenTestingKit.h which wasn’t created on my project. So to solve this issue, i had to link SenTestingKit.framework from Build phases tab -> Link Binary with libraries to project target.

Now my above issue was resolved. But real pain came next and new issue arose:

2) Undefined symbols for architecture i386: “_OBJC_CLASS_$_SenTestCase”, this gave me pain in my wherever you can think of, for hours. To be honest i was totally blank on this and didn’t know what to do. So i searched forums for this. It seemed that similar kind of problem was faced by many people. I tried every available answers and fixes but none worked for me. I even created and setup project from scratch again just to make sure if i hadn’t skipped any steps. But again, (yeah there is always a but somewhere) nothing worked. So i dig more deeper into my source files of facebook sdk. It seemed all the source files inside test folder were using this “SenTestingKit”. So i simply deleted that test folder from my project navigator, and it really resolved this issue or i removed the source of issue. No more such errors and no more pain and i don’t know about any gain.

But i wasn’t still sure if it didn’t break up the project as you don’t simply delete files and folders from external libraries without knowing. So i tested my project and performed all facebook activities and it seemed to worked fine. I haven’t found any issue till now but lets see.