Dynamic Webview Height

- (void) webViewDidFinishLoad: (UIWebView *)sender
{
[self performSelector:@selector(calculateWebViewSize) withObject:nil afterDelay:0.1];
}

- (void) calculateWebViewSize
{
UIScrollView *webScrollView = [[myWebView subviews] objectAtIndex:0];
CGSize webScrollRect = webScrollView.contentSize;
NSLog(@"webScrollrect: %f", webScrollRect.height);
}



Add UIWebViewDelegate in .h file

To specify minimum OS version

iPhone SDK Tips & Tricks
iPhone OS Deployment Target

To specify which OS version is the minimum that your application will support, you set the deployment target. Your application will then run on this minimum OS as well as all later versions.



source

How to install new iOS from IPSW file

iPhone SDK Tips & Tricks
  • You’ll use iTunes. Backup your phone as you always do.
  • Download and install the iTunes.
  • Once it’s installed, click Alt + Restore in iTunes
  • Select the IPSW file that you downloaded – let the restore happen.
That’s it, you now have new iOS.

Count text height

iPhone SDK Tips & Tricks

-(float)getHeightByWidth:(NSString*)myString:(UIFont*)mySize:(int)myWidth
{
CGSize boundingSize = CGSizeMake(myWidth, CGFLOAT_MAX);
CGSize requiredSize = [myString sizeWithFont:mySize constrainedToSize:boundingSize lineBreakMode:UILineBreakModeWordWrap];
return requiredSize.height;
}

Http POST Request

iPhone SDK Tips & Tricks


NSString
*post =[NSString stringWithFormat:@"userName=%@&password=%@",userName.text,password.text];


NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:@"https://localhost:443/SSLLogin/login.php"]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];

NSError *error;
NSURLResponse *response;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *data=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
NSLog(@"%@", data);

Determine a CLLocation from an address

iPhone SDK Tips & Tricks

-(CLLocationCoordinate2D) addressLocation {
NSString *urlString = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%@&output=csv",
[addressField.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSString *locationString = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString]];
NSLog(@"locationString = %@",locationString);

NSArray *listItems = [locationString componentsSeparatedByString:@","];

double latitude = 0.0;
double longitude = 0.0;
if([listItems count] >= 4 && [[listItems objectAtIndex:0] isEqualToString:@"200"]) {
latitude
= [[listItems objectAtIndex:2] doubleValue];
longitude
= [[listItems objectAtIndex:3] doubleValue];
/*
NSString *nameString = [NSString stringWithFormat:@"http://ws.geonames.org/findNearestAddress?lat=%f&lng=%f",latitude,longitude];
NSString *placeName = [NSString stringWithContentsOfURL:[NSURL URLWithString:nameString]];
NSLog(@"placeName = %@",placeName);
*/

}
else {
//Show error
}
CLLocationCoordinate2D location;
location
.latitude = latitude;
location
.longitude = longitude;

return location;

}

Search Specific word in string

iPhone SDK Tips & Tricks


NSString *searchForMe = @"Hi";
NSRange range = [searchThisString rangeOfString : searchForMe];

if (range.location != NSNotFound)
{
NSLog(@"I found something.");
}

Creating an UIImage from a URL

iPhone SDK Tips & Tricks

NSURL *url = [NSURL URLWithString:path];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc] initWithData:data];

iPhone Vibrate

iPhone SDK Tips & Tricks


AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);

Don't forget to import AudioServices.h:
#import <AudioToolbox/AudioServices.h>

Text Button Alignment

iPhone SDK Tips & Tricks


UIButton *accountButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
accountButton.frame = CGRectMake(10, 40, 100, 45); // size and position of button
[accountButton setTitle:@"label" forState:UIControlStateNormal];
accountButton.contentVerticalAlignment = UIControlContentVerticalAlignmentBottom;


typedef enum {
UIControlContentVerticalAlignmentCenter = 0,
UIControlContentVerticalAlignmentTop = 1,
UIControlContentVerticalAlignmentBottom = 2,
UIControlContentVerticalAlignmentFill = 3,
} UIControlContentVerticalAlignment;


typedef enum {
UIControlContentHorizontalAlignmentCenter = 0,
UIControlContentHorizontalAlignmentLeft = 1,
UIControlContentHorizontalAlignmentRight = 2,
UIControlContentHorizontalAlignmentFill = 3,
} UIControlContentHorizontalAlignment;