Tuesday, May 22, 2012

iPhone | Problems with NSURLConnections to Run consecutively (in parallel)

I was trying to get multiple NSURLConnections (async) to run in sequence (consecutively).

The first call worked well (made request & fetched response) while on new async call from "connectionDidFinishLoading" (fyi, I was in a situation that based on first call response) the next call connection doesn't seem to work, as none of the NSURLConnection delegate methods are triggered.

Background threads don't automatically have an active run loop on them. You need to start up the run loop after you create the NSURLConnection in order to get any input from it. Fortunately, this is quite simple:
[[NSRunLoop currentRunLoop] run];
The default mode of NSURLConnection is asynchronous -- it creates and manages a new background thread for you, and calls back to the delegate on the original thread. You therefore don't need to worry about blocking the main thread.

Hope it will work for you, cheers!

Tuesday, May 15, 2012

iPhone | Change Section header in UITableView

Go for it!
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection: (NSInteger)section {

if ([self tableView:tableView titleForHeaderInSection:section] != nil) {
return 22;
}
// If no section header title & header needed
return 0;

}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
NSString *sectionTitle = [self tableView:tableView titleForHeaderInSection:section];

if (sectionTitle == nil) {
return nil;
}
// Create label with section title
UILabel *label = [[[UILabel alloc] init] autorelease];
label.frame = CGRectMake(0, 0, 320, 18);
label.backgroundColor = ANY_COLOR_OR_IMAGE_PATTERN;
label.textColor = [UIColor whiteColor];
label.shadowColor = [UIColor whiteColor];
label.shadowOffset = CGSizeMake(0.0, 1.0);
label.font = [UIFont boldSystemFontOfSize:14];
label.text = sectionTitle;

// Create header view and add label as a subview
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, SectionHeaderHeight)];
[view autorelease];
[view addSubview:label];

return view;
}

Test with iOS 5 as well.. please!
Let me know if any issue.. thanks!

gson error in android application while JSON parsing | type mismatch JSON error with gson

While developing an android app having loads of usage of JSON (actually REST server based), We decided to go ahead with GSON library(I think pretty awesome except following issue).

issue : It worked fine on all android phones (fyi.. tested on few Motorola,Samsung and HTC devices..) except the HTC Explorer(a good Value for money phone as a testing device even for personal usage).

btw, error was in JSON parsing (library).. and While I was trying to run,..YUCK! I was getting a "Force Close" with some type mismatch JSON based error!

Solution:
The "fix" of renaming the gson package with jarjar did the trick..
If anyone else has this problem, here's a quick solution; (fyi, we are using gson 1.6)
Steps - how-to fix it:

1) Download jarjar (http://code.google.com/p/jarjar/downloads/list)
2) Put jarjar-1.0.jar and gson-1.6.jar in the same folder
3) Create a new text file in this folder (rules.txt)
4) Write the following line in the textfile: rule com.google.gson.** com.google.myjson.@1
5) From the command line, open jarjar with the command "java -jar jarjar.jar process rules.txt gson-1.6.jar myjson-1.6.jar"
6) Replace the gson library in your project with myjson and update the imports

so what was the reason.. OOuch! many of the android devices (probably the HTC, as I observed) use GSON library for internal apps... this is what I know.. not sure :) ..

Droid guys! Stay tuned for more bugs..ah! solutions ;)

Tuesday, March 6, 2012

Eclipse – Your project contains error(s), please fix them before running your application.

Today I got trapped in a really weird situation. while compiling an android app in eclipse,I got alerts Your project contains error(s), please fix them before running your application.
ACTUALLY there was no error in code; I tried project -> clean. TOUGH LUCK :(
I was pretty annoyed, because I was not able to run my app to test on my Android device.
After doing a lot of investigation:

I got that this is a little annoyance you’re going to get now and then. To fix it, just use the “Problems” window which is docked at the bottom by default, right click on the Error(s) listed and delete.

Try to run now. Yay! Its running now...

If still any issue, might be you need to run project - clean.

Hope this helps!

Tuesday, January 17, 2012

iPhone | Getting touch event in UIScrollView Or scrollable UITableView

Unable to get tap event in scrolling view or scrollable table view?

hmm... Really a bad situation.. by the way here is the solution :)

Put this in viewDidLoad method :

(YOUR_SCROLL_VIEW may be UIScrollView or scrollable UITableView)

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapGestureCaptured:)];

[YOUR_SCROLL_VIEW addGestureRecognizer:singleTap];

and you will get the touches with :

- (void)singleTapGestureCaptured:(UITapGestureRecognizer *)gesture {

CGPoint touchPoint=[gesture locationInView:YOUR_SCROLL_VIEW];

NSLog(@"Touch point coordinates are : %f - %f", touchPoint.x , touchPoint.y );

}

this worked for me, hope this helps to save your time!

Monday, November 28, 2011

iPhone | Convert UILabel to Circle

This is how you can convert a UILabel to circle:

steps:
1)Add Quartz Core framework to application frameworks

2) Import QuartzCore.h file to implementation file.

3) #define kRectArmLength 24.0 (Not necessary, as you can decide while initializing label with frame)

//Label will be a square (equal width & height)

UILabel *lblCircle = [[UILabel alloc] initWithFrame:CGRectMake(10,20, kRectArmLength, kRectArmLength)];

//set corner radius just half of the square arm length

lblCircle.layer.cornerRadius= kRectArmLength/2;

lblCircle.clipsToBounds=YES;

lblCircle.backgroundColor = [UIColor redColor];

[self.view adsSubview: lblCircle];