UIScrollView Case: Who are they?

Found something of interest, I noticed it during the use of UIScrollView. I was adding multiple UIImageView to scrollview dynamically, at some point later I was iterating through subviews of scrollview and doing some manipulation stuff. I was getting weird results. I printed a log for total count of scrollview’s subviews, found that total count was two more extra than I was supposed to have. I was adding five UIImageView to UIScrollView and total count of subview array was 7. Going more deeper with log prints, found that those two extra views were UIImageView [sighs] I was wondering how was those two extra UIImageView getting added to UIScrollView?

I removed all subviews from UIScrollView before adding my other UIImageView/s. This trick worked just partially. There were no extra two UIImageView and I was not getting any weird result from my processing code. I was thinking if it was some bug with UIScrollView but I was so wrong. I looked more closer to the output and found that two scroll indicators (horizontal and vertical) are missing :P, then it struck me that those two extra UIImageView were actually indicators. I changed my processing/iteration code instead of just plainly removing all subviews. All was well in the end.

Happy Valentines Day 🙂


Posting photo to instagram from your app could be tricky if some basic things isn’t handled properly. I’l be talking about small things where i made mistake. We cannot post image directly to instagram from app/code. Instead we have to use Documentation Interaction API, UIDocumentInteractionController and URL Schemes to open photo on instagram and share/post from there. Here control also won’t be returned back to your app.

The identifier for UTI is “com.instagram.photo”. Before triggering instagram to open your photo, always check if instagram is available on your device:

NSURL *instagramURL = [NSURL URLWithString:@"instagram://location?id=1"];
if ([[UIApplication sharedApplication] canOpenURL:instagramURL]) {

Here is the code to open photo on instagram app

-(void) openFileOnInstagram{
	CGRect rect = CGRectMake(0 ,0 , 0, 0);
    UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, self.view.opaque, 0.0);
    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
    NSString  *imagePath = [NSHomeDirectory() stringByAppendingPathComponent:@"InstagramPost.app/no_image.ig"];
    NSURL *imageAttachFile = [[NSURL alloc] initWithString:[[NSString alloc] initWithFormat:@"file://%@", imagePath]];
    self.dic.UTI = @"com.instagram.photo";
    self.dic = [self setupControllerWithURL:imageAttachFile usingDelegate:self];
    self.dic=[UIDocumentInteractionController interactionControllerWithURL:imageAttachFile];
    [self.dic presentOpenInMenuFromRect: rect inView: self.view animated: YES ];
- (UIDocumentInteractionController *) setupControllerWithURL: (NSURL*) fileURL usingDelegate: (id ) interactionDelegate {
    UIDocumentInteractionController *interactionController = [UIDocumentInteractionController interactionControllerWithURL: fileURL];
    interactionController.delegate = interactionDelegate;
    return interactionController;

First mistake i did was with getting filepath of my image. My failed attempt was:

NSString  *imagePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Document/no_image.ig"];

So i was assuming that device will have same path structure as my macbook have:

/Users/gaanza/Library/Application Support/iPhone Simulator/6.1/Applications/Documents/no_image.ig

It was my mistake, path in device is like this:


I simply replaced “Documents” with “InstagramPost.app”, InstagramPost is a name of my application so you have to provide yours with .app extension.

Second mistake i did was with file extension of image. You cannot trigger the instagram app to open if we don’t use “.ig” extension, so save the image in PNG/JPEG format but with .ig extension.

Third mistake i did was with not saving file with .ig extension, i was saving file with png extension but using .ig on code call, so this also won’t work :P, you have to save your file in png/jpeg format but with .ig extension.

Please also check instagram reference page.

Source: http://osx.wdfiles.com/local--files/icon:iruler/iRuler.png

Source: http://osx.wdfiles.com/local–files/icon:iruler/iRuler.png

I was supposed to detect the event when scroller/page of a webview reaches bottom so that i can show one decent translate animation of another small view(like an ad banner). That view will be overlayed definitely and when web page scrolls above center of page then view will hide via translate animation. I came up with a simple solution to detect scroll position. I made an subclass of webview and override onScrollChanged method. Inside this method i am checking scrollY position with the height of content height.

	public void  onScrollChanged (int l, int t, int oldl, int oldt){
		int height = (int) Math.floor(this.getContentHeight() * this.getScale());
		if(this.getScrollY() >= height){
			Log.i("THE END", "reached");

I could get content height from getContentHeight but i need to keep zoom level of page in mind too so i have multiplied scale factor. Now whenever scrollY reaches height then page end has been reached. It is not so, because we haven’t checked for offset value. That offset value is height of the scroller according to theory if scrollY is the exact Y coordinates of scroller :P. I tried many ways to calculate the height but still i am missing something. I checked the reference documentation and found lots of helpful stuffs. I’l cover everything that i used where i failed and had success. First i used getBottom and getTop, according to doc it returns top and bottom position relative to parent, since parent on my case was “relative layout” with “match_parent”. I would calculate the heght of scroller:

int webViewHeight = this.getBottom() - this.getTop();

Now i have height of content and height of web view so i calculated the height of the scroller from this:

int scrollerHeight = (webViewHeight * webViewHeight) / height; //height is content height

I calculated detection of end reach with this condition:

if(this.getScrollY() + scrollHeight >= height){
	Log.i("THE END", "reached");

But it wasn’t any way near to perfection 😛 I logged the values and saw that calculation for view of webview was getting wrong output. Even getBottom was no way near accurate since it was returning way low value than expected with respect to my 7 inch nexus tablet density.

Next, i used getMeasuredHeight() and this one gave me accurate value. So i applied same above calculation with new height of web view and result still not very accurate. After all this i did small change to my formula and BINGO!!

public void  onScrollChanged (int l, int t, int oldl, int oldt){
	int height = (int) Math.floor(this.getContentHeight() * this.getScale());
	int webViewHeight = this.getMeasuredHeight();
	if(this.getScrollY() + webViewHeight >= height){
		Log.i("THE END", "reached");

Yes above code worked absolutely accurate and am still analyzing the formula on above condition :P, i.e offset here is height of the webview but not height of scroll [sighs]. I would love to hear your opinion on this. You can optimize the above code though i just wanted to share the idea behind.

‘&’, what a flow breaker. When you send a string data that contains ‘&’ in it to a server then what do you notice? In my case server received only data preceding ‘&’. What happened to the other data ? Well it seemed that data from ‘&’ onwards got cut away. In my previous post i mentioned how to encode string that contains spaces in it. I tried same encoding in this case and it didn’t work. I found a working solution, courtesy stackoverflow.com

I added a new routine function to our NSString via category, whose task was to encode the string so that special characters be handled gracefuly.

@interface NSString (RFC3875)
- (NSString *)stringByAddingRFC3875PercentEscapesUsingEncoding:(NSStringEncoding)encoding;
@implementation NSString (RFC3875)
- (NSString *)stringByAddingRFC3875PercentEscapesUsingEncoding:(NSStringEncoding)encoding {
    CFStringEncoding cfEncoding = CFStringConvertNSStringEncodingToEncoding(encoding);
    NSString *rfcEscaped = (NSString *)CFURLCreateStringByAddingPercentEscapes(
    return rfcEscaped;

Everthing was smooth after that, just using above category to encode my input:

NSString *postDataString = [inputText stringByAddingRFC3875PercentEscapesUsingEncoding: NSUTF8StringEncoding];

Don’t forget to import:

#import "NSString+RFC3875.h"


Clear all: Core Data

Core Data is really super awesome and comes in a handy on almost my all of projects. I am posting quick code for deleting all your rows from entity. This code might do the job when you are in need of clearing all data.

- (void) clearAllRecords{
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Chat" inManagedObjectContext:managedObjectContext];
    [fetchRequest setEntity:entity];
    NSError *error;
    NSArray *items = [managedObjectContext executeFetchRequest:fetchRequest error:&error];
    fetchRequest = nil;
    for (NSManagedObject *managedObject in items) {
        [managedObjectContext deleteObject:managedObject];
    if (![managedObjectContext save:&error]) {

Why flush ?

Interesting stuff i came to know about which i was unaware of. This post is more of a discussion on the problem. I haven’t understood the solution and so would request you to have a say.

I have done json parsing numerous times. I can tell if json is valid or not by looking it. I was using google-gson for android application and its awesome. Application was supported by php web service. The solution that fixed the problem is related to php and i am not a php developer and don’t have that much inner understanding of it. It was regular json parsing routine. Application just crashed when gson was parsing the json response from php service.

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 2

From error I understood the reason but that was not it. I was very confident that something else was the problem because json response was perfect. I wrote quick objective-c routine(SBJSON) to parse same json response and it worked there. I even did validation on jsonlint.com
After doing bit of research I came across a working solution. In php before sending response I had to put this:

echo header('Content-Type: application/json');

I checked the php manual to understand what those lines means, header function was quite obvious. According to manual, ob_clean:

This function discards the contents of the output buffer.

Now I have no idea why discarding the contents of output buffer was working. This was the case for android, I mean in iphone I didn’t need ob_clean. I am still looking for the explanation on this.

Affair with core data: IPhone

I have lots of stuff to write upon. Problems that I have faced and how I have solved it. For this post i have picked up coredata topic. Core Data is a persistence framework. I would love to give one beginner’s tutorial on coredata but this post will be about the pitfall that most of the beginners falls into and i have tasted it. I assume that you have already used Core data with your project. I would refer this tutorial for quick basic walkthorough. There was a chat application where i had to manange chat history. I was using Core Data for saving all chat messages. This is how my rough sketch of chat entity looks like:


@interface Chat : NSManagedObject

@property (nonatomic, retain) NSNumber * chat_id;
@property (nonatomic, retain) NSString * message;
@property (nonatomic, retain) NSNumber * timestamp;
@property (nonatomic, retain) NSString * username;


Chat entity has four attributes. Everything was setup properly. When I ran the program, an error occured:

Failed to call designated initializer on NSManagedObject

I was initializing my chat like this:

Chat *chat = [[Chat alloc] init];

According to error init wasn’t appropiate initializer. I checked the class reference docs and according to it:

if you instantiate a managed object directly, you must call the designated initializer (initWithEntity:insertIntoManagedObjectContext:)

My chat implementation file looks like this:

#import "Chat.h"

@implementation Chat

@dynamic chat_id;
@dynamic message;
@dynamic timestamp;
@dynamic username;


I looked for initWithEntity in refrence doc and it says:

You are discouraged from overriding this method—you should instead override awakeFromInsert and/or awakeFromFetch (if there is logic common to these methods, it should be factored into a third method which is invoked from both). If you do perform custom initialization in this method, you may cause problems with undo and redo operations.

Then I checked for NSEntityDescription in reference because initWithEntity was taking it as a parameter:

- (id)initWithEntity:(NSEntityDescription *)entity insertIntoManagedObjectContext:(NSManagedObjectContext *)context

I was going through all the methods in class reference and there I found one method:

+ (id)insertNewObjectForEntityForName:(NSString *)entityName inManagedObjectContext:(NSManagedObjectContext *)context

According to reference docs:

Creates, configures, and returns an instance of the class for the entity with a given name.
Returns a new, autoreleased, fully configured instance of the class for the entity named entityName. The instance has its entity description set and is inserted it into context.

From reading those, I changed my initializer and new code looked like this:

Chat *chat = (Chat *)[NSEntityDescription insertNewObjectForEntityForName:@"Chat" inManagedObjectContext:managedObjectContext];

This worked smoothly for me.

Reverse geocoding: IPhone

There will be a time when we would have to get the address of location from lattitude and longitude value. This concept is called Reverse geocoding. Well as usual this post is again about the mistake that I made and the solution that I got for it. Apple has introduced CLGeocoder class from ios 5.0 onwards. According to reference doc:

The CLGeocoder class provides services for converting between a coordinate (specified as a latitude and longitude) and the user-friendly representation of that coordinate.

For reverse geocoding there is a method where you provide location and it will convert:

- (void)reverseGeocodeLocation:(CLLocation *)location completionHandler:(CLGeocodeCompletionHandler)completionHandler

Now I’ll start the boring tale of my mistake. If you see the reverseGeocodeLocation, it takes CLLocation as parameter which represent location data that also consist our latitude and longitude information. Now I had just lat and long value that I was receiving from my json object. So I had a simple requirement to parse those values and convert them to meaningful street address. I have a float values of lat and long, reverseGeocodeLocation takes CLLocation as parameter. I need to somehow convert float values to CLLocation or make CLLocation from my float values. So here is what I did first:

CLLocation *coordinates = [[CLLocation alloc]init];
coordinates.coordinate.latitude = [@"41.409261" floatValue];
coordinates.coordinate.longitude = [@"2.163517" floatValue];

But there lies a big problem. The thing is coordinate property(CLLocationCoordinate2D) is read only so I cannot assign my float values to respective lat and long directly. So now what I had to do was initialize CLLocation from CLLocationCoordinate2D which is a structure. This is how I represented my lat and long with CLLocationCoordinate2D:

CLLocationCoordinate2D coordinate;
coordinate.latitude = [@"41.409261" floatValue];
coordinate.longitude = [@"2.163517" floatValue];

Initialize CLLocation with newly created coordinate:

CLLocation *currentLocation = [[CLLocation alloc]initWithCoordinate:coordinate altitude:1 horizontalAccuracy:1 verticalAccuracy:-1 timestamp:[NSDate date]];

My location is ready now and I just pass it to reverseGeocodeLocation and on complete handler I receive my meaningful street address:

[self.geocoder reverseGeocodeLocation: currentLocation completionHandler:
     ^(NSArray *placemarks, NSError *error) {      
         //Get nearby address
         CLPlacemark *placemark = [placemarks objectAtIndex:0];
         NSString *addressName = [[placemark.addressDictionary valueForKey:@"FormattedAddressLines"] componentsJoinedByString:@", "];

         NSLog(@"My location name is %@",addressName);

Another not so interesting tale…Lots of thing you learn from mistakes and lots of knowledge you gain. When I started iphone development, I chose non-ARC way to know and learn the concepts of memory management. It was fun and good experience apart from objective-c being ugly(syntax). Whenever there was memory leak and any crash due to low on memory, it used to give me dose of excitement, debugging tits and bits had been good learning experience. It has been good journey for me. I started ARC later and life’s been good and easy for me.

I was trying to do some experiment with one iphone project based on facebook. I remember when my project was already using SBJSON for parsing json, then later when I had to integrate facebook to my project and I had lots of sbjson conflict errors. It seemed facebook was using another version of SBJSON library, so I had to remove my SBJSON and integrate the library that facebook was using. I found it in github facebook project. So that was another experience and at that time I was working on non-arc environment. So recently I tried to do something to see the result on what would happen if I do along these lines. So I created ARC based project and integrated facebook sdk 3.1 and then I integarted that same SBJSON that I had integrated on past to avoid conflict. This time my project and facebook sdk was ARC based and SBJSON was non-arc. So I decided to see the result by changing all lines of releasing memory to setting them to nil on the source codes of SBJSON. As release call will throw syntax error in arc environment so I was replacing them by nil assignment. Why nil assignment ? Well I wanted to see if there is any harm with that?

Everthing was fine. I was making rest call to my web services and parsing json. Then moment came and one crash sting me like a bee. So my crash error was this:

MyApp(2121,0xb0185000) malloc: *** error for object 0x1068310: double free
*** set a breakpoint in malloc_error_break to debug

From the description it was clear that I was trying to release already released memory. But big question was WHERE? Then as error asked me to do, I did set a breakpoint on malloc_error_break but still it was not enough for me. I need more clear error log, so I enabled zombies and trust me!! at this point of time zombies is your best friend. So as I knew I got a very clear picture, yeah NAKED CLEAR

[NSDecimalNumber retain]: message sent to deallocated instance

So it seems my decimal number has already been released before retain was called. Now another big question was NSDecimalNumber ????

I decided to play with parsing json because this crash was coming only parsing certain type of value. So I need to dig up more deeper. I was parsing every kind of value and trying to replicate the crash. Then crash happened and I had a big stupid grin on my face. I again tested parsing same kind of data and crash happened again and my stupid grin went wider. So crash was happening when I was parsing numbers. Something like this {“answer”:5} Now time was to follow the trails. So I started from [responseString JSONValue] , responseString is string and JSONValue is a method added via category. So I opened NSSting+SBJSON.m file and looked for the function. Now within function SBJsonParser was being used to parse the string, so I opened SBJsonParser.m file and searched for NSDecimalNumber because my best friend zombie told me I had a memory boom with decimal numbers. I got goosebumps when I found the stupid but very critical mistake I had made and it was the culprit of existence of this post that you are reading now. 😀 So my mistake happend on this method

(BOOL)scanNumber:(NSNumber **)o

See this is why I love zombie as it told me the problem was with decimalnumber and see where did I end up with. Yeah scanNumber! 😛 Now time for showing my mistake, but I would also like you to check the source code and look by yourself and I guess you already have SBJSON library.

@autoreleasepool {
    id str = [[NSString alloc] initWithBytesNoCopy:(char*)ns
                                            length:c - ns

    if (str && (*o = [NSDecimalNumber decimalNumberWithString:str]))
        return YES;

and original lines of code was like this:

id str = [[NSString alloc] initWithBytesNoCopy:(char*)ns
                                            length:c - ns
[str autorelease];
if (str && (*o = [NSDecimalNumber decimalNumberWithString:str]))
    return YES;

My mistake was that I used @autoreleasepool block because in hurry I couldn’t see that in original source code NSAutoreleasePool was not used and when I was using @autoreleasepool then all the autoreleased object was getting released after finish of the block. In my case decimalNumberWithString got released after releasepool block was finished and YES carriage was return. So later *o was pointing to deallocated memory and my app was crashing whenever I was parsing numbers.

But this was worth it and lots of fun debugging this crash. Also I am posting screenshot of enabling zombie objects

Facebook authentication error: Iphone

Facebook has released new 3.1 version of sdk for ios (and ios 6 compatible). You can target ios 5 also, just need to do small tweak during setup. They have details and tutorial on developers’ site. This post is not about any how to’s, here I will be talking about another few things that you need to be aware of and take care of it. I have integrated facebook sdk with android and iphone projects many times, but you can fall into one basic pitfall if not taken care of.

So your facebook app is ready. You have its app id and facebook sdk is well integrated with your project. You are ready to go. Now during facebook authentication from your app it gives this error (screenshot

the operation couldn’t be completed com.facebook.sdk error 2
Now you will be thinking what went wrong? You’ll double check the facebook app id, and go through all settings you made and see if you have missed anything? But you find that everything is in its place. So what the duck is wrong with the program? What we are missing out is one basic setting during facebook app creation. If you check the facebook app dashboard again and look for native IOS App section. There you will find Bundle ID field, here you will have to enter your application bundle identifier so that facebook knows which app to redirect back to once auth is complete. This is very safe and you should provide this field on dashboard setting. Your application might run without that detail as I have tested this and confirmed it too that it has run on many other application without that setting but then in some devices/scenario it will definitely hit you. Your simulator and testing device might be running without issue but when you distribute it to some other device then you will face it eventually. So just be on safe side and provide your bundle identifier to your facebook app settings.