I am new to iPhone Programming
I want to read the content of text file which is in my Resourse folder. i did a lot of googling but failed to get proper way for doing this task.
Please suggest
The files in your "Resource folder" are actually the contents of your application bundle. So first you need to get the path of the file within your application bundle.
NSString* path = [[NSBundle mainBundle] pathForResource:#"filename"
ofType:#"txt"];
Then loading the content into a NSString is even easier.
NSString* content = [NSString stringWithContentsOfFile:path
encoding:NSUTF8StringEncoding
error:NULL];
As a bonus you can have different localized versions of filename.txt and this code will fetch the file of the currently selected language correctly.
The first thing you want to do is get the path for the text file you want to read. Since it's in your Resources folder, I'm assuming you're copying it into the main bundle of the application. You can get the path for a file in your main bundle using NSBundle's pathForResource:ofType:
NSBundle *mainBundle = [NSBundle mainBundle];
NSString *filePath = [mainBundle pathForResource:#"filename" ofType:#"txt"];
Then you can read the file at that path into an NSString directly using initWithContentsOfFile:usedEncoding:error:
NSStringEncoding encoding;
NSError *error;
NSString *fileContents = [[[NSString alloc] initWithContentsOfFile:filePath
usedEncoding:&encoding
error:&error]
autorelease];
Related
I have a web app, all its resources are contained in a folder named "war".I've imported these into my xcode project (via File -> "Add Files to 'my project'").
Now my xcode project looks like:
MyProject
|- war
|- foo.html
|- AppDelegate.h
|- AppDelegate.m
...
so now I try loading like:
NSBundle *bundle = [NSBundle mainBundle];
NSURL *url = [NSURL fileURLWithPath:[bundle pathForResource:#"foo" ofType:#"html"] isDirectory:NO];
NSURLRequest *req = [NSURLRequest requestWithURL:url];
[self.mWebView loadRequest:req];
I don't get compile errors, but my UIWebView is empty. I do get webViewDidLoad callbacks too. Is there some other way we need to load local pages like this?
Thanks
If it's a true subdirectory (i.e., the folder name shows up in blue):
Then you can use URLForResource:withExtension:subdirectory::
NSURL *url = [[NSBundle mainBundle] URLForResource:#"foo"
withExtension:#"html"
subdirectory:#"war"];
Alternatively, if it's simply a "group" (i.e. if the "folder" doesn't show up in blue):
Then you use the plain old URLForResource:withExtension::
NSURL *url = [[NSBundle mainBundle] URLForResource:#"foo"
withExtension:#"html"];
This is how I usually load local html files without a problem.
[webView loadRequest:[NSURLRequest requestWithURL:[[NSBundle mainBundle] URLForResource:self.fileName withExtension:nil]]]
Since your code mostly looks right to me I think it may be some other problem. I suggest setting up a delegate for your webView and checking its - (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error method to see if that gives more information.
Also make sure your foo.html is a target member.
I want to create simple iPhone app with local HTML file.
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:#"index" ofType:#"html"];
NSData *htmlData = [NSData dataWithContentsOfFile:htmlFile];
[waWebView loadData:htmlData MIMEType:#"text/html" textEncodingName:#"UTF-8" baseURL:[NSURL URLWithString:#""]];
I made one webView and make it show index.html file in same directory with .m file.
Then make an another HTML file; index2.html, in same directory
I want to see index2.html through a link in index.html
So, in index.html, I put
View html 2
index.html is successfully shown, but above link doesn't work.
What do I need more? Help meT.T
In additional to this problem, this app doesn't support javascript code too...
the link is relativ.. you need to set the baseURL of your webView to the path containing the index html file
NSString *file = [[NSBundle mainBundle] pathForResource:#"index" ofType:#"html"];
NSURL *base = [NSURL fileURLWithPath:file.stringByRemovingLastPathComponent];
[webview loadHTMLString:text baseURL:base];
It is done by creating a NSURL using its fileURLWithPath and then a NSURLRequest and loading that rather loading the html itself:
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:#"index" ofType:#"html"];
NSURL *url = [NSURL fileURLWithPath:htmlFile];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[waWebView loadRequest:request];
You may need to add javascript to it for it to work. If you want to make a simple iPhone app just use app mobi(http://www.appmobi.com/) and they let you put css html and js files into a builder and it does the magic for you. Try that.
Hope I Helped
LucaSpeedStack
I have created sqlite database.
I add this sqlite file in my application but when I run the app in the simulator and check the sqlite file's path in document directory and I open the sqlite file on document directory's path at that time the table which I have created does not appear.
In local copy I can see the table also in my project folder I can see the table but on the path of simulator I am not able to see the table
so in my console it shows me the error:
Sqlite Prepare failed: no such table
My response below assumes that your database is properly formatted and functional. You could test easily via the command line. In my own apps, all tab bar controllers apps, I place the db access code in the app delegate. The two methods in the app delegate are as follows:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions (NSDictionary *)launchOptions {
[self createEditableCopyOfDatabaseIfNeeded];
[self.window setRootViewController:rootController];
[window makeKeyAndVisible];
return YES;
}
Also:
- (void)createEditableCopyOfDatabaseIfNeeded {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSString *dbPath =[documentsDir stringByAppendingPathComponent:#"dbName.sqlite"];
BOOL success = [fileManager fileExistsAtPath:dbPath];
if(!success)
{
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"dbName.sqlite"];
success = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error];
if (!success)
NSAssert1(0, #"Failed to create writable database file with message '%#'.", [error localizedDescription]);
}
}
If you've tried everything and are still getting this error.. even if you've tried resetting the iOS Simulator. Try installing the app in the iOS simulator and then manually uninstalling it (select Home and long select the app icon and delete it) and then reset the simulator.
I don't know why but I tried everything else, spent hours banging my head against the wall. Then this worked.
You can put the sqlite file in your xcode project and locate it like so:
NSString *pathToSQL = [[NSBundle mainBundle]
pathForResource:#"sqlname" ofType:#"sql"];
Make sure the "Target Membership" of the sql file is set properly, so that the project "sees" it:
1) click on the sql file in the left-pane of Xcode
2) open/show the File Inspector (right pane)
3) Under "Target Membership", make sure the "check" is "checked"
That's it. Hope, this is what you're looking for. Any concern get back to me.
I've downloaded a file from a webserver, and saved it in my Documents Directory.
I try and pass the url of the file to MPMoviePlayerViewController using
initWithContentURL:url];
Where the url is created with
[NSURL URLWithString:#"/var/mobile/Applications/7A21A941-A54C-4116-857D-A34EDEE2F2E8/Documents/lesson.m4v"];
However whenever I try to play the video, the MoviePlayer appears for a second, with "loading" then dismisses itself.
Am I doing something wrong with the local file URL?
(The video plays fine when streamed from the webserver)
Thanks
I don't know why it is dismissing itself, but you actually are using web url instead of fileUrl. You need to initiate it like this:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:#"lesson.m4v"];
NSURL *url = [NSURL fileURLWithPath:path];
and pass this url to your MPMovieController
For local files must use:
[NSURL fileURLWithPath: somethingPath];
For server files can use:
[NSURL URLWithString: somethingPath];
HI all,
Is there any way by which we can edit a html file that is in our resource directory and we want to edit some tags in that html first and want to show that html in web view.
Is it possible ??
Thanks ...
NSBundle *bundle = [NSBundle bundleForClass:[self Class]];
NSString *htmlPath = [bundle pathForResource:#"html file name" ofType:#"htm"];
NSString *htmlContent = [NSString stringWithContentOfFile:htmlPath encoding:NSUTF8StringEncoding error:nil];
// Do your HTML edits here
[[self webView] loadHTMLString:htmlContent baseURL:nil];
This assumes that you've got a webview property of your view controller. Also, I don't know what the baseURL should be for local files. I know I've done this before, but I don't have access to those files right now. Hopefully this gets you part of the way there.
Yes,
1) Locate and load the file
2) Make your edits
3) Save the file
You'll have to refresh the webview though for the changes to take effect.