Wednesday, December 11, 2013

How to enable or disable auto capitalization and auto correction in iOS programmatically


Usually auto capitalization and auto correction are enabled in iOS, however at times there will be a need to disable auto capitalization and auto correction. For example when user is setting up his account or when user is entering his password. This can be done either programmatically or can be done in the story board. Let us explore how to do this programmatically.

In iOS UITextInputTraits protocol defines the features that are associated with the key board input. This protocol is implemented by UITextField and UITextView. Property autocapitalizationType determines the characteristics of auto capitalization and property autocorrectionType determines the characteristics of auto correction. To disable auto capitalization in text view
UITextView *text = [[UITextView alloc] initWithFrame:CGRectMake(130.0, 10.0, 150.0, 100.0)]; text.autocapitalizationType = UITextAutocapitalizationTypeNone;
Similarly to disable auto capitalization in text field
UITextField *field = [[UITextField alloc] initWithFrame:CGRectMake(100.0, 10.0, 150.0, 30.0)]; field.autocapitalizationType = UITextAutocapitalizationTypeNone;
To disable auto correction in text view
UITextView *text = [[UITextView alloc] initWithFrame:CGRectMake(130.0, 10.0, 150.0, 100.0)]; text.autocorrectionType = UITextAutocorrectionTypeNo;

Similarly to disable auto correction in text field
UITextField *field = [[UITextField alloc] initWithFrame:CGRectMake(100.0, 10.0, 150.0, 30.0)]; field.autocorrectionType = UITextAutocorrectionTypeNo;

Wednesday, December 4, 2013

Fix for iOS application that crashes in the simulator due to core data changes.

Often it is possible that we make changes to the core data model during the development process. Whenever data model changes, existing version of the application will crash in the simulator due to the fact that the data in the persistent store is unusable in the new version of the application. To fix this select Reset Content and Settings option in the iOS simulator .

Thursday, August 29, 2013

iOS Text View Recipes

How to make a text view not editable but scrollable.

At times, specially in the view only mode, we would like a text view to be scrollable but not editable. It can be achieved as follows.
... UITextView *text = [[UITextView alloc] initWithFrame:CGRectMake(90.0, 5.0, 200.0, 100.0)]; //This is to make text non editable but scrollable text.editable = NO; text.scrollEnabled = YES; text.userInteractionEnabled = YES; ....

Saturday, August 24, 2013

iOS Table View Recipes

How to set table view in editable mode programmatically.

By default table view in iOS is set to be in non editable mode. Often it is required to set table view in editable mode programmatically, it can be done by setting editing property on table view to be yes.
For example :
- (void)viewDidLoad { [super viewDidLoad]; self.tableView.editing = YES; }


How to conditionally enable or disable editing of certain table cells.

It is possible to configure certain cells in table view to be editable while others are not. It can be achieved by implementing the method canEditRowAtIndexPath
For example :
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { if(indexPath.row < 2) { return NO; //Cell is not editable } else { return YES; //Cell is editable } }
How to remove cell separator line for table cells.

By default table cells are separated by line, these lines can be removed by setting separatorStyle property on table view.
For example:
- (void)viewDidLoad { [super viewDidLoad]; self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; }
How to reset content in a table view cell.

As per apple documentation "The table view'€™s delegate in tableView:cellForRowAtIndexPath: should always reset all content when reusing a cell"
Content can be reset as shown below.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"PasswordDetailsCell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; //Reset the content in cell [[cell textLabel] setText:@"" ]; [[cell detailTextLabel] setText:@""]; NSArray *subviews = [cell.contentView subviews]; for(UIView *view in subviews){ [view removeFromSuperview]; } }

Thursday, June 13, 2013

Track clicks on downloadable links

We always strive to make our web sites better by understanding the usage of the site. One such metric is to check how many times a given file is downloaded, or how many times a given button is clicked. In this article we will explore a way to measure the number of times a file is downloaded using Google Analytics tracking.

Let's say we have a link to download zip file.

<a href="/download.zip">Download zip file.</a>

We would like to know the number of times the file is downloaded. This can be achieved by adding a piece of inline javascript snippet to the onclick event. Assuming we already have basic Google Analytics setup, let's change the download link to as follows.
<a href="/download.zip" target="_blank" onclick="_gaq.push(['_trackEvent','event_category','event_action']);">Download zip file </a>"
Change "event_category" and "event_action" to names of your choice, as these will be reflected in Google Analytics. These statistics can be seen in Google Analytics under "Content" -> "Events" -> "Overview"

Reference : https://developers.google.com/analytics/devguides/collection/gajs/eventTrackerGuide