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 Development

Reply
 
LinkBack Thread Tools Display Modes
Old 09-07-2011, 10:12 AM   #1 (permalink)
Gold Orange
 
orange gold's Avatar
 
Join Date: Sep 2008
Posts: 686
orange gold is an unknown quantity at this point
Default NSData WriteToFile Not working?

In my resources folder I have a file "index.html" .. I can load it into a webview just fine but I want to change the file to have different text and then reload it into the webview when a button is pressed.. I can't get it to work though? No clue what's wrong... keep getting sigbart...

Here is my code:

Code:
-(IBAction)editWebView {
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error = [[NSError alloc] init]; 
    
    [fileManager removeItemAtPath:[[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"] error:&error];
    
    NSString *myString = [[NSString alloc] initWithFormat:@"test"];
    
    [myString writeToFile:[[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"] automatically:YES encoding:NSASCIIStringEncoding];
    
    [coloredText loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"]isDirectory:NO]]];
}
__________________
APPS4LIFE
search for me in the app store.
orange gold is offline   Reply With Quote
Old 09-07-2011, 10:23 AM   #2 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

You can't write into the bundle.
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 09-07-2011, 10:24 AM   #3 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by orange gold View Post
In my resources folder I have a file "index.html" .. I can load it into a webview just fine but I want to change the file to have different text and then reload it into the webview when a button is pressed.. I can't get it to work though? No clue what's wrong... keep getting sigbart...

Here is my code:

Code:
-(IBAction)editWebView {
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error = [[NSError alloc] init]; 
    
    [fileManager removeItemAtPath:[[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"] error:&error];
    
    NSString *myString = [[NSString alloc] initWithFormat:@"test"];
    
    [myString writeToFile:[[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"] automatically:YES encoding:NSASCIIStringEncoding];
    
    [coloredText loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"]isDirectory:NO]]];
}
In iOS the app bundle is read-only. Any attempts to save, delete, or add files to the bundle will fail.

You need to copy the file to your documents directory on first launch, and then read/write that file instead of the one in the bundle.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 09-07-2011, 11:22 AM   #4 (permalink)
Gold Orange
 
orange gold's Avatar
 
Join Date: Sep 2008
Posts: 686
orange gold is an unknown quantity at this point
Default

So what would I put inside my awakeFromNib code to copy the file and allow a re-writeable one? What's the function called?
__________________
APPS4LIFE
search for me in the app store.
orange gold is offline   Reply With Quote
Old 09-07-2011, 11:32 AM   #5 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by orange gold View Post
So what would I put inside my awakeFromNib code to copy the file and allow a re-writeable one? What's the function called?
Take a look at the NSFileManager class. It has a couple of methods who's names start with "copyItem" that should do the trick.

By the way, you do not want this code in awakeFromNib. You want it in in the init method for your app delegate, so the file is copied over very early in the launch process.

You should check to see if the file already exists in the documents directory. (Do a search in the NSFileManager class for "exists" for a method to check if a file exists.) If so, you're done. If not, copy the file.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.

Last edited by Duncan C; 09-07-2011 at 11:36 AM.
Duncan C is offline   Reply With Quote
Old 09-07-2011, 04:41 PM   #6 (permalink)
Gold Orange
 
orange gold's Avatar
 
Join Date: Sep 2008
Posts: 686
orange gold is an unknown quantity at this point
Default

Awesome, thanks!
__________________
APPS4LIFE
search for me in the app store.
orange gold 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
» Online Users: 371
8 members and 363 guests
apatsufas, JackReidy, jeroenkeij, Sami Gh, tim0504, UMAD, yomo710
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,671
Threads: 94,121
Posts: 402,904
Top Poster: BrianSlick (7,990)
Welcome to our newest member, JackReidy
Powered by vBadvanced CMPS v3.1.0

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