Archive for category Iphone

www.ioscodesnips.com


hi friends,

follow my new web blog for latest updates

http://www.ioscodesnips.com

Leave a comment

Adding Custom Fonts in ios


1) Add the .TTF or .OTF font file/s you wish to use as a resource

2) Open your info.plist and create a new key called UIAppFonts(Fonts provided by application), set the key to be an array type.

3) For each font you want to use in your app, add it as a new object in the array with its full name and extension and save the info.plist when finished

4) Now in your code you can simply call

[UIFont fontWithName:@”Insert font name here” size:18]

 

 

7 Comments

Scrolling Images in Image Gallery iphone


.h file

#import <UIKit/UIKit.h>

@interface ScrollingImageViewController : UIViewController {

IBOutlet UIScrollView *FirstScrollView;

}

@property (nonatomic, retain) UIView *FirstScrollView;

@end

 

.m file

 

#import “ScrollingImageViewController.h”

@implementation ScrollingImageViewController

@synthesize FirstScrollView;

const CGFloat kScrollObjHeight = 353.0;

const CGFloat kScrollObjWidth = 258.0;

const NSUInteger kNumImages = 3;

– (void)layoutScrollImages

{

UIImageView *view = nil;

NSArray *subviews = [FirstScrollView subviews];

// reposition all image subviews in a horizontal serial fashion

CGFloat curXLoc = 0;

for (view in subviews)

{

if ([view isKindOfClass:[UIImageView class]] && view.tag > 0)

{

CGRect frame = view.frame;

frame.origin = CGPointMake(curXLoc, 0);

view.frame = frame;

curXLoc += (kScrollObjWidth);

}

}

// set the content size so it can be scrollable

[FirstScrollView setContentSize:CGSizeMake((kNumImages * kScrollObjWidth), [FirstScrollView bounds].size.height)];

}

– (void)viewDidLoad

{

 

// 1. setup the scrollview for multiple images and add it to the view controller

//

// note: the following can be done in Interface Builder, but we show this in code for clarity

[FirstScrollView setBackgroundColor:[UIColor blackColor]];

[FirstScrollView setCanCancelContentTouches:NO];

FirstScrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;

FirstScrollView.clipsToBounds = YES; // default is NO, we want to restrict drawing within our scrollview

FirstScrollView.scrollEnabled = YES;

FirstScrollView.pagingEnabled = YES;

NSUInteger i;

for (i = 1; i <= kNumImages; i++)

{

NSString *imageName = [NSString stringWithFormat:@”Snap%d.jpg”, i];

UIImage *image = [UIImage imageNamed:imageName];

UIImageView *imageView = [[UIImageView alloc] initWithImage:image];

CGRect rect = imageView.frame;

rect.size.height = kScrollObjHeight;

rect.size.width = kScrollObjWidth;

imageView.frame = rect;

imageView.tag = i;

[FirstScrollView addSubview:imageView];

[imageView release];

}

[self layoutScrollImages];

}

– (void)dealloc

{

[FirstScrollView release];

[super dealloc];

}

– (void)didReceiveMemoryWarning

{

[super didReceiveMemoryWarning];

}

@end

 

in .xib file add a scroll view with (353.0,258.0)  frame

1 Comment

MBProgressHUD implementation in iphone


Download the project from GitHub,

here is the link

https://github.com/jdg/MBProgressHUD

Leave a comment

Playing video from url in iPhone


.h file

#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>

@interface ViewController : UIViewController
{
MPMoviePlayerController *moviePlayerController;
}

@end

.m file

– (void)viewDidLoad
{
[super viewDidLoad];
NSURL *fileURL = [NSURL URLWithString:@”http://www.w3schools.com/html/movie.mp4″%5D;
moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[moviePlayerController.view setFrame:CGRectMake(0, 70, 320, 270)];
[self.view addSubview:moviePlayerController.view];
moviePlayerController.fullscreen = YES;
[moviePlayerController play];
}

//for rotation
– (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
if (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
[moviePlayerController.view setFrame:CGRectMake(0, 70, 320, 270)];
} else if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight) {
[moviePlayerController.view setFrame:CGRectMake(0, 0, 480, 320)];
}
return YES;
}

 

 

 

Leave a comment

iPhone Application Lifecycle


I just went through one presentation for iPhone application. So this post to share what I understood.
First of all the main focus is how actually iPhone applications work.
When user touches the icon of the application main() gets called by the system.
main() creates the autoreleasepool and starts the application with UIApplicationMain object. UIApplicationMain creates the instance of the UIApplication which actually starts the application.
UIApplication loads the main nib file and sets the application. Then UIApplication forwards the events to interface elements.
For terminating the application user taps home button, UIApplication tells the delegate that application is terminating. And so UIApplication gets exited , then main gets exited. This way the process ends.
This is the way iPhone applications works.
Happy coding…..!!!!

 

, ,

2 Comments

converting Float to NSString


Float myFloat=3.85;

NSString *myString = [NSString stringWithFormat:@”%f”, myFloat];

Leave a comment

Conversion Formulas


BEGIN WITH FORMULA for CONVERTING
(Multiply Number of Units by Conversion Number to Obtain New Number of Units)
millimeters (mm) millimeters x 0.03937 = inches
centimeters (cm) centimeters x 10 = millimeters
centimeters x 0.3937 = inches
meters (m) meters x 1000 = millimeters
meters x 100 = centimeters
meters x 3.281 = feet
meters per second x 3.281 = feet per second
square meters x 10.76 = square feet
square meters x 1.196 = square yards
square meters x 0.0002471 = square acres
cubic meters x 35.31 = cubic feet
cubic meters x 1.308 = cubic yards
cubic meters x 0.0008107 = acre-feet
cubic meters per second x 35.31 = cubic feet per second
cubic meters per second x 15,850.00 = gallons per minute
kilometers (km) kilometers x 1000 = meters
kilometers x 0.6214 = miles
square kilometers x 0.3861 = square miles
cubic kilometers x 0.2399 = cubic miles
inches (in) inches x 25.4 = millimeters
inches x 2.54 = centimeters
square inch x 6.4516 = square centimeters
feet (ft) feet x 12 = inches
feet x 0.3048 = meters
square feet x 0.09294 = square meters
cubic feet x 0.02832 = cubic meters
acre-foot x 1233 = cubic meters
yard (yd) yard x 3 = feet
yard x 0.9144 = meters
miles (mi) miles x 5280 = feet
miles x 1609.3 = meters
miles x 1.609 = kilometers
square miles x 2.590 = square kilometers
cubic miles x 4.168 = cubic kilometers
acre-foot (acre-ft) acre-ft x 1233 = cubic meters
microradian microradian x 0.2 = second of arc
Temperature Conversions
degrees Fahrenheit (F)
degrees Celsius (C) (F – 32) x 5/9 = C
(C x 1.8) + 32 = F
(C x 2) – (C x 2 x 0.1) + 32 = F
a) double the Celsius temperature
b) subtract 10% of the doubled temperature
c) add 32
Weights and Measures
(Left Column Equals Right Column)
1 cup = 8 fluid ounces = 4.8 ounces = 137 grams dry weight
= 8 ounces
= 16 tablespoons
= 48 teaspoons
1 gallon = 0.1337 cubic feet
= 231 cubic inches
= 3.785 liters
= 8.336 pounds of water
= 133.376 ounces of water
1 liter = 0.001 cubic meter
= 1.057 quarts
= 0.2642 gallons
= 0.03531 cubic feet
1 ounce = 0.0625 pounds
= 28.35 grams
= 437.5 grains
1 pound (Avoirdupois) = 16 ounces
= 0.4536 kilograms
= 7000 grains
= 1.2153 pounds Troy
1 quart = 2 pints
= 0.25 gallons
= 0.9464 liters
= 2.084 pounds of water

2 Comments

opening a native map application in xcode


NSString *stringURL = [NSString stringWithFormat:@”http://maps.google.com/maps?saddr=40.754458,-73.990302&daddr=40.7504610,-73.9910452″%5D;
NSURL *url = [NSURL URLWithString:stringURL];
[[UIApplication sharedApplication] openURL:url];

,

Leave a comment

Xml parsing iphone


.h

#import <UIKit/UIKit.h>

@interface samplexmlViewController : UIViewController{
NSMutableArray *array1;
NSMutableDictionary *dict1,*tempDict;
NSString *xmlString;
IBOutlet UILabel *lbl1,*lbl2,*lbl3;
int i,counts,table;
}
-(void)showQ:(NSMutableDictionary *)dicts;
@end

.m

– (void)viewDidLoad {

NSURL *url = [[NSURL alloc] initWithString:@”url”];
NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:url];

//Initialize the delegate.

//Set delegate
[xmlParser setDelegate:self];

//Start parsing the XML file.
BOOL success = [xmlParser parse];

if(success)
NSLog(@”No Errors”);
else
NSLog(@”Error Error Error!!!”);

tempDict=[[NSMutableDictionary alloc]init ];
tempDict=[array1 objectAtIndex:i];
[self showQ:tempDict];
[super viewDidLoad];
}

– (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
if ([elementName isEqualToString:@”parentnode”]) {
array1=[[NSMutableArray alloc]init ];
}
if ([elementName isEqualToString:@”child”]) {
dict1=[[NSMutableDictionary alloc]init];
}
}
– (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if ([elementName isEqualToString:@”subchild1″]) {
[dict1 setObject:xmlString forKey:@”subchild1″];
}
if ([elementName isEqualToString:@”subchild2″]) {
[dict1 setObject:xmlString forKey:@”subchild2″];
}
if ([elementName isEqualToString:@”subchild3″]) {
[dict1 setObject:xmlString forKey:@”subchild3″];
}

if ([elementName isEqualToString:@”child”]) {
[array1 addObject:dict1];
NSLog(@”array 1 is %@”,array1);
}
if ([elementName isEqualToString:@”parent”]) {
NSLog(@”total array is %@”,array1);
}
xmlString=nil;
}
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{

if(xmlString == nil)
xmlString = [[NSMutableString alloc] initWithString:string];
else
[xmlString appendString:string];

}
-(void)showQ:(NSMutableDictionary *)dicts
{
lbl1.text=[dicts objectForKey:@”subchild1″];
lbl2.text=[dicts objectForKey:@”subchild1″];
lbl3.text=[dicts objectForKey:@”subchild1″];

}

1 Comment