Load different html file on one UIWebView - iphone

iam going to change my UIWebView files via UISwipeGuesture but have problem :
i have 100 HTML files that loaded on UIWebView , so i want implement something like this :
if user swipes the view my webView will load next html file but the problem is my code just load first HTML file :
NSString *path = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:#"file%d",+1] ofType:#"html"];
NSFileHandle *readHandle = [NSFileHandle fileHandleForReadingAtPath:path];
NSString *htmlString = [[NSString alloc] initWithData:
[readHandle readDataToEndOfFile] encoding:NSUTF8StringEncoding];
[self.myWebView loadHTMLString:htmlString baseURL:nil];

[NSString stringWithFormat:#"file%d",+1] is always going to return the string file1, so you're always loading file1.html.
Do you mean to be using a counter or index instead of the number 1?
EDIT: More code. This isn't how I'd recommend implementing this, but it should give you an idea and it's relatively compact. (I'd use an ivar or property to track the current file index.)
static NSInteger currentIndex = 0;
if (currentIndex < 99) {
currentIndex++;
}
NSString *path = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:#"file%d",currentIndex] ofType:#"html"];
NSFileHandle *readHandle = [NSFileHandle fileHandleForReadingAtPath:path];
NSString *htmlString = [[NSString alloc] initWithData:
[readHandle readDataToEndOfFile] encoding:NSUTF8StringEncoding];
[self.myWebView loadHTMLString:htmlString baseURL:nil];

Related

Webview displaying one HTML file in iPhone sdk

I have three HTML files in my local resources bundle. I need to display it as a reader. So i used a web view to display that using following code,
- (void)viewDidLoad
{
[super viewDidLoad];
totalArray=[[NSMutableArray alloc]init];
[totalArray addObject:#"file1"];
[totalArray addObject:#"file2"];
[totalArray addObject:#"file3"];
NSLog(#"totalArray count:%d",[totalArray count]);
for (int i=0;i<3;i++)
{
NSLog(#"i count:%d",i);
NSString *bundlePath = [[NSBundle mainBundle] bundlePath];
NSURL *bundleBaseURL = [NSURL fileURLWithPath: bundlePath];
NSLog(#"webview %#", bundlePath);
NSString *filePath1= [[NSBundle mainBundle] pathForResource:[totalArray objectAtIndex:i] ofType:#"html"];
NSLog(#"filePath1:%#",filePath1);
[htmlView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:filePath1]]];
}
}
Am getting the total count as 3, current index counts and file path. But still it displays only my first html file. My other two files were missing. What could be the problem here? Kindly help me. Thanking you.
You need wait till the file are loaded , looping is not god idea , instead you can achieve same using below code. Use fileURLWithPath to load local file.
- (void)viewDidLoad
{
[super viewDidLoad];
count = 0;
totalArray=[[NSMutableArray alloc]init];
[totalArray addObject:#"file1"];
[totalArray addObject:#"file2"];
[totalArray addObject:#"file3"];
[_htmlView setDelegate:self];
NSString *filePath1= [[NSBundle mainBundle] pathForResource:[totalArray objectAtIndex:count] ofType:#"html"];
NSURL *url = [NSURL fileURLWithPath:filePath1];
[_htmlView loadRequest:[NSURLRequest requestWithURL:url]];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView{
count++;
if (count < 3) {
NSString *filePath1= [[NSBundle mainBundle] pathForResource:[totalArray objectAtIndex:count]
ofType:#"html"];
NSLog(#"filePath1:%#",filePath1);
NSURL *url = [NSURL fileURLWithPath:filePath1];
[_htmlView loadRequest:[NSURLRequest requestWithURL:url]];
}
}
For the local file urls you have to use fileURLWithPath: method of NSURL. Also, you could get file url from the bundle with [[NSBundle mainBundle] URLForResource: withExtension:] method. Further more, I don't think that loading urls in the loop is a good idea, you should try something else like loading url on button action. Good Luck!

How to add CSS file to iphone project in xcode?

I am working on a web based application for iphone, I have created new cssfile to the project this css file doesn't affect the html file, but when I have uploaded the css file a host it worked fine and styled the html
What is the problem ?
edit 1
This the head element .
css.css is working fine and is linked to the html correctly
jquery.mobile-1.0.min.css is NOT !
Despite the both files exist in the same border
Onotha.com
<link rel="stylesheet" type="text/css" href="css/jquery.mobile-1.0.min.css" />
edit 2
This is the Objective-C code, I have an exception, I think in the last line of code
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[window makeKeyAndVisible];
NSString *path= [[NSBundle mainBundle] pathForResource:#"index" ofType:#"html" inDirectory:NO];
// [webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:path]]];
// NSString *path = [[NSBundle mainBundle] bundlePath];
// NSURL *baseURL = [NSURL fileURLWithPath:path];
// [webView loadHTMLString:htmlString baseURL:baseURL];
// load css styles
NSString *cssPath = [[NSBundle mainBundle] pathForResource:#"css/jquery.mobile-1.0.min.css" ofType:#"css"];
NSData *cssData = [NSData dataWithContentsOfFile:cssPath];
NSString *cssString = [[NSString alloc] initWithData:cssData encoding:NSASCIIStringEncoding];
// load js
NSString *jsPath = [[NSBundle mainBundle] pathForResource:#"MyJS" ofType:#"js"];
NSData *jsData = [NSData dataWithContentsOfFile:jsPath];
NSString *jsString = [[NSString alloc] initWithData:jsData encoding:NSASCIIStringEncoding];
// compose full html page
NSString *pageContent = [NSString stringWithFormat:#"%#%#%#", cssString, jsString, path];
[webView loadHTMLString:pageContent baseURL:baseURL];
return YES;
}
edit 3
I got this after using code posted by Srikar
You can load CSS from local project directory
NSString *path = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:path];
[webView loadHTMLString:htmlString baseURL:baseURL];
detail info check this site.
More elaborate code here -
// load css styles
NSString *cssPath = [[NSBundle mainBundle] pathForResource:#"MyCSS" ofType:#"css"];
NSData *cssData = [NSData dataWithContentsOfFile:cssPath];
NSString *cssString = [[NSString alloc] initWithData:cssData encoding:NSASCIIStringEncoding];
// load js
NSString *jsPath = [[NSBundle mainBundle] pathForResource:#"MyJS" ofType:#"js"];
NSData *jsData = [NSData dataWithContentsOfFile:jsPath];
NSString *jsString = [[NSString alloc] initWithData:jsData encoding:NSASCIIStringEncoding];
// compose full html page
NSString *pageContent = [NSString stringWithFormat:#"%#%#%#", cssString, jsString, actualPageMarkup];
[webView loadHTMLString:pageContent baseURL:[NSURL URLWithString:#""]];
More info here
I don't know if you have solved this, but I think you have to load in "pageContent" the content of the page instead of the path.
Something like this:
NSString *htmlString = [NSString stringWithContentsOfFile:path encoding:NSASCIIStringEncoding];
NSString *pageContent = [NSString stringWithFormat:#"%#%#%#", cssString, jsString, htmlString];
[webView loadHTMLString:pageContent baseURL:baseURL];

what is the wrong when loading local resource files in webview in iphone

I am trying to load local css and html files into WebView , but the web view displays the content of css file in webview as you see in the following image ..
objective-c code:
NSString *htmlPath= [[NSBundle mainBundle] pathForResource:#"index" ofType:#"html" inDirectory:NO];
//[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:htmlPath]]];
// load css styles
NSString *cssPath = [[NSBundle mainBundle] pathForResource:#"css" ofType:#"css"];
NSData *cssData = [NSData dataWithContentsOfFile:cssPath];
NSString *cssString = [[NSString alloc] initWithData:cssData encoding:NSASCIIStringEncoding];
// load js
NSString *jsPath = [[NSBundle mainBundle] pathForResource:#"MyJS" ofType:#"js"];
NSData *jsData = [NSData dataWithContentsOfFile:jsPath];
NSString *jsString = [[NSString alloc] initWithData:jsData encoding:NSASCIIStringEncoding];
// compose full html page
NSString *pageContent = [NSString stringWithFormat:#"%#%#%#", cssString, jsString, htmlPath];
[webView loadHTMLString:pageContent baseURL:[NSURL URLWithString:#""]];
what is the problem ?
You must not use
[webView loadHTMLString:pageContent baseURL:[NSURL URLWithString:#""]];
instead loadHTMLString try to use stringByEvaluatingJavaScriptFromString
And if you have any problem check the following post. I think you will get it easily
UIWebView and local css file
I have not used it for CSS but hope this will help you...Good Luck...

Images not showing in device for Web view in iphone

I am working on one application. I stuck with one problem. I have HTML page and some images are in Resource Directory. Whenever i run the application on Simulator it works perfectly fine. But when i run the application on the device the image are not displaying but text is displaying only and instead of image Question mark is showing.
Code:
NSString *path = [[NSBundle mainBundle] pathForResource:#"test1" ofType:#"html"];
NSFileHandle *readHandle = [NSFileHandle fileHandleForReadingAtPath:path];
NSString *htmlString = [[NSString alloc] initWithData: [readHandlereadDataToEndOfFile]encoding:NSUTF8StringEncoding];
NSURL *baseurl = [NSURL fileURLWithPath:path];
[self.webView loadHTMLString:htmlString baseURL:baseurl];
HTML Code:
<img src="mainGrid.png">
Change your HTML code to (file:// tells that it's local resource):
<img src='file://%#'>
Then use it in this way:
NSString *pathToLocalImage = [[NSBundle mainBundle] pathForResource:#"mainGrid" ofType:#"jpg"];
NSString *content = [NSString stringWaithFormat:htmlString, pathToLocalImage];
[self.webView content baseURL:nil];

The eXe-SCORM generated html file has to be transparent to show the backround UIIMage view

Hii....i am new to iPhone programming..Can anybody help me out please
i have an html which is generated by eXe tool.I need to show it on an UIImageView,created programmatically. so the html has to be transparent then the image looks like backround.
is it possible to make that html as transparent..?
Thank u
NSString *path = [[NSBundle mainBundle] pathForResource:#"filename" ofType:#"html"];
NSFileHandle *readHandle = [NSFileHandle fileHandleForReadingAtPath:path];
NSString *htmlString = [[NSString alloc] initWithData:
[readHandle readDataToEndOfFile] encoding:NSUTF8StringEncoding];
webView.opaque = NO;
webView.backgroundColor = [UIColor clearColor];
[self.webView loadHTMLString:htmlString baseURL:nil];
[htmlString release];
This code helped me
Thank u all...