Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Tutorials > Tutorial Discussion

Reply
 
LinkBack Thread Tools Display Modes
Old 01-25-2011, 12:23 PM   #1 (permalink)
Knows SQL
 
iisword's Avatar
 
Join Date: Oct 2009
Location: Somewhere the streets are on fire, the sewers are flooded, and the cats are high on catnip
Posts: 529
iisword is on a distinguished road
Default [TUTORIAL] Making Folders

Hi everyone!! I'm going to talk about making Folders in your app.

1) Why would you want to make folders?
A: Folders help keep things organized and can keep data separated. For example, lets say I have students and employees and I'm naming the files after the person's last name. Well if one student is Smith and an employee is named Smith, the file would overwrite, meaning I have the data for either the student or the employee(which ever came second). If I have a employee and student folder, I don't have to worry about that problem occurring.

So Here's what we do:

1)Make a new View based app named Text.

2)In TextViewController.h type this
Code:
#import <UIKit/UIKit.h>

@interface TextViewController : UIViewController 
{
	IBOutlet	UILabel		*textLabel;
}
@property (nonatomic, retain) UILabel *textLabel;
-(void)createFolder;
-(void)getText:(NSString *)path;
@end
and save.

3)Go to TextViewController.xib and add a UILabel and connect it. Also make it blank. Save.

4)In TextViewController.m type this:
Code:
#import "TextViewController.h"

@implementation TextViewController
@synthesize textLabel;



// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad 
{
    [super viewDidLoad];
	[self createFolder];
	[textLabel setText:[self getText:[self getPathToTextFile]]];
}



/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


- (void)dealloc 
{
    [super dealloc];
	[textLabel release];
}

-(void)createFolder
{
	NSLog(@"This creating folder");
	NSFileManager *manager = [NSFileManager defaultManager];
	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
	NSString *docDir = [paths objectAtIndex:0];
	NSString *path = [docDir stringByAppendingFormat:@"/text/"];
	NSError *error;
	if (![manager fileExistsAtPath:path])
	{
		NSLog(@"Creating");
		if (![manager createDirectoryAtPath:path 
			withIntermediateDirectories:NO 
							 attributes:nil 
								  error:error])
		{
			NSAssert1(0,@"Failed to make directory with '%s'.", [error localizedDescription]);
		}
	}
}

-(NSString *)getPathToTextFile
{
	NSLog(@"Working");
	NSString *string = [[NSString alloc] initWithString:@"This is a nice tutorial"];
	NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
	NSString *docDir = [array objectAtIndex:0];
	NSString *path = [docDir stringByAppendingFormat:@"/text/text.txt"];
	[string writeToFile:path 
			 atomically:NO 
			   encoding:NSStringEncodingConversionAllowLossy 
				  error:nil];
	NSLog(path);
	return path;
}

-(NSString *)getText:(NSString *)path
{
	NSLog(@"Is getting text");
	NSString *string = [[NSString alloc] initWithContentsOfFile:path];
	return string;
}

@end
and build and run and thats how you do it.

2 important things:
you need to have
Code:
if (![manager fileExistsAtPath:path])
otherwise your app will fail with no console message.
Second if you want to check to see that a folder was made, the NSLog(path); will give you the location.
__________________
iisword is offline   Reply With Quote
Old 02-27-2011, 02:50 PM   #2 (permalink)
Registered Member
 
Join Date: Sep 2010
Location: KSA
Posts: 109
aziz is on a distinguished road
Send a message via MSN to aziz Send a message via Yahoo to aziz Send a message via Skype™ to aziz
Default

Thank U Thank U Thank U !!!!

;P
aziz is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Stats
Members: 175,696
Threads: 94,139
Posts: 402,961
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jasper_muc
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 01:57 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0