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 ;)