I'm trying to make a WebView load a page from HTML code I have stored as an NSData. I get a blank page when I try to do this. Is there anything wrong with what I'm doing when I load the page? If not, I need to look elsewhere in my program.
if (essence.html){ //essence.html is an NSData
NSLog(#"Inserting HTML code into browser window: %#", [[NSString alloc] initWithData:essence.html encoding: NSUTF8StringEncoding]);
[webView.mainFrame loadData:essence.html MIMEType: #"text/html" textEncodingName: #"utf-8" baseURL:nil]; //webView is a WebView
}
I created the conditions so essence.html contains HTML code from the page http://kathleenmelian.com/test.html (which just says "hello"). The NSLog prints this when the above code runs:
Inserting HTML code into browser window: <html><head></head><body>hello</body></html>
So essence.html definitely contains valid code that a browser should be able to load.
You could use
[webView loadHTMLString:
[[NSString alloc] initWithData:essence.html encoding:NSUTF8StringEncoding]
baseURL:nil];
The other idea would be to replace "utf-8" with "UTF-8", which in some cases is known to make a difference (not sure about UIWebView).
Sorry to bother you guys. I fixed some other bug in my program's model, and that somehow fixed THIS problem as well. I don't know how. Crisis averted.
Related
I have a UIWebView. When there is internet access I want it to load the actual webpage and when there is no internet I want it to load a saved html complete website file. I implemented a test webView with the local file, and it works perfectly. The code is this:
[myWebView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"My Website" ofType:#"html"]isDirectory:NO]]];
Of course I synthesized the webview and made it in the .h as well. I also made a normal webview accessing the website online, which is exactly the same except that instead of the above code in the viewDidLoad it says:
NSString *fullURL = #"http://www.mywebsite.com";
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[myWebView loadRequest:requestObj];
However, when I tried making the webview load the html when no internet was available and the website when internet was available, it worked, but the offline site stopped supporting anything but the main page and lost all its photos (they work fine in the separate view, as described above, just not in the demo offline-online view). I used the following code:
NSURL *scriptUrl = [NSURL URLWithString:#"http://www.mywebsite.com"];
NSData *data = [NSData dataWithContentsOfURL:scriptUrl];
if (data) {
NSLog(#"Device is connected to the internet");
NSString *fullURL = #"http://www.mywebsite.com";
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[myWebView loadRequest:requestObj];
} else {
NSLog(#"Device is not connected to the internet");
[myWebView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"My Website" ofType:#"html"]isDirectory:NO]]];
}
In desperation, I tried adding a navigation bar to each of my original views, so that in the offline view you could click the button saying "online" and segue to the online view, and vice versa. This failed miserably: the offline view (which was the initial one) worked fine but when I pressed the "online" button I got the following error:
*** First throw call stack: (0x321cb3e7 0x3a065963 0x321cb0d5 0x32a397d9 0x32a35543 0x321518a5 0x34176e7d 0x341766ff 0x3406f079
0x33ff9451 0x34085357 0x340c6cd9 0x340c5fab 0x341e7da3 0x340c5087
0x340c5111 0x340c5087 0x340c503b 0x340c5015 0x340c48cb 0x340c4db9
0x33fed5f9 0x33fda8e1 0x33fda1ef 0x35cce5f7 0x35cce227 0x321a03e7
0x321a038b 0x3219f20f 0x3211223d 0x321120c9 0x35ccd33b 0x3402e2b9
0x8194d 0x3a492b20) libc++abi.dylib: terminate called throwing an
exception
I tried making the online view be the initial one and the offline one be the secondary one that you click a bar button to get to, and the offline one stopped working. It appears as if the initial view will load, whichever it is, but not the secondary one. Maybe I am incorrectly using bar button items, though I am experienced with storyboarding and have many bar button segues located in the rest of my project. And yes, I did change the names of my web views to be different (myWebView and myOfflineWebView) when implementing the last mentioned bar button solution, after it initially failed, because I thought maybe having two different uiWebViews with the same name, albeit in different viewControllers, might be the problem, but that didn't help.
Any feedback is appreciated. Possibly the code for checking internet availability did not work because it was made for OSX. I found it here. I would prefer not to bog down my project with big libraries (like this one, which is often mentioned) as it had many libraries all ready and is a bit slow. I cannot use the usual Apple Reachability sample project because I am using ARC. I could refactor it but I do not have much time and would prefer a simpler solution, even if it is not 100% perfect or elegant. Thank you!
So, regarding the internet availability, you will want to use this reachability library.
Also, your images and links probably aren't loading because your saved html has absolute urls: http://foo.com/image.jpg which can't be loaded because you have no connection.
i want to take a normal html page (a page from tf2 wiki) and convert it into a view.
For example it should take a text and put it into a label or a Image into an UIImageView.
Is there any Framework to do something like that or should i convert the site on a webserver
to make it easier for the app at runtime to render ?
Hope you understand, what i want to do.
Tim
there could be different ways depending on your scenario, you could load them in UIWebView within IOS, If you don't want show the UIWebView then you can get the html from it ans use it however you want. or other way you could transfer the HTML as json data to IOS and then show that data via UIView, UILabel or anything else. otherwise as suggested by Zakaria, you could use PhoneGap.
UPDATE with sample code for sending a request and getting its contents if you don't want to use UIWebView
//your html page with url
NSURL *url = [NSURL URLWithString:#"www.google.com"];
NSURLRequest *request=[NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:30.0f];
NSOperationQueue *queue=[[NSOperationQueue alloc]init];
[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
if ([data length]>0 && error==nil) {
NSString *html=[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
NSLog(#"this string contains all html page you have \n %#",html);
}
}
];
Not sure if i understand the question properly, but Cordova lets you develope iOS apps in html/css/js.
Check their website: http://cordova.apache.org/
If your page is a static one, then use PhoneGap : It will embed your HTML+JS+CSS in an iOS app and will be loaded in a webview.
But there is no framework that will allow you to convert your HTML elements into native ones.
I'm displaying two HTML5 documents on my iPad application, using a UIWebView on iOS 6.0.1.
One of them has no problems and I can open and close the webview as much as I want.
The other one can be displayed once and then when I close my web view and want to re-open the document again, the webview shows a black screen.
I haven't prepared the HTML5 documents myself and I don't have much knowledge on HTML5, so I can't tell the difference between them that causes this behavior.
Here's how I create my webview and how I load the HTML5:
UIWebView *theWebView = [[UIWebView alloc] init];
theWebView.scalesPageToFit = YES;
theWebView.clearsContextBeforeDrawing = YES;
theWebView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
//adjust frame and add to view controller's view here
NSURL *url = [NSURL URLWithString:#"http://www.handypdf.net/davsan"];
[theWebView loadRequest:[NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:60.0]];
Everytime I open an HTML5 document, I re-create the web view. So I don't re-use it.
To fix the problem, I tried cleaning the cache, cookies, etc.. explained on these SO answers, but it didn't work.
Here are the links to the problematic and the not-problematic HTML documents:
This one works ok.
This one doesn't work the second time.
Also, if you want to check these HTML5 pages online here are the links to them:
This one works ok.
This one doesn't work the second time.
This problem does not happen on iOS 5.x. It's also replicable using the iOS simulator.
What would you suggest to fix it?
Thx
Not a satisfying answer, but here's how I progressed with this problem:
I couldn't solve this for a couple of days. Then I deleted all my code and re-wrote the functionality from scratch, and then it was solved. I couldn't understand the exact reason, because I didn't write anything fancy on my second attempt, so unfortunately I can't tell how it's solved :(
Here is what I just did:
Create the web view once, and cache it:
+(UIWebView *)getTheWebView {
if (theWebView) return theWebView;
theWebView = [[UIWebView alloc] init];
theWebView.scalesPageToFit = YES;
theWebView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
return theWebView;
}
Then, when I want to show the web view:
UIWebView *theWebView = [HTML5Manager getTheWebView];
theWebView.frame = self.view.frame;
[self.view addSubview:theWebView];
And here's how I load the URL:
[theWebView loadRequest:[NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60.0]];
This is just it and it works. I know it doesn't make sense but I hope it helps you, too...
I am trying to pull down the code from an HTML website that has no more than 2 lines on it. The code contains a word that I need to retrieve. Is there a simple way to pull down that code and put it in an NSString?
Further details: I am going to have an app that checks for a word on a page. If that word is what I am looking for, the app will show the text "confirmed". The purpose of the app is to check to see if the page is accessible.
If you need a http library to hit the server try asihttp. Apart from this i need more info of what you are trying to do...
If you just want to check if that website is reachable, you can go with HTTP Success Status Codes.
Using ASIHTTPRequest simplifies communication over the web.
If you still want to evaluate the text on that website, can also just retrieve it using:
[request responseString];
Depending on what you get from the website, it's up to you how to update the UI.
Just change the link between the quotes and it'll work!
-(void) viewDidLoad {
NSString * sFeedURL = [NSString stringWithFormat:#"http://www.google.com/ig/api?weather=,,,270000,960000"];
//RSS Feed URL goes between quotes
NSString * sActualFeed = [NSString stringWithContentsOfURL:[NSURL URLWithString:sFeedURL] encoding:1 error:nil];
NSLog(#"%#", sActualFeed);
}
I'm trying to display HTML source code in my NSDocument based application. However, it renders the page as Safari would show it.
Here's the code that I use to open HTML:
NSData*data;
NSMutableDictionary *dict = [NSDictionary dictionaryWithObject:NSHTMLTextDocumentType
forKey:NSDocumentTypeDocumentOption];
data = [NSData dataWithContentsOfFile:[self fileName]];
mString = [[NSAttributedString alloc]
initWithData:data options:dict documentAttributes:NULL
error:outError];
What am I doing wrong?
The correct solution is a mix of your original code and the bogus solution I gave you in my previous answer (which I've deleted). Use NSPlainTextDocumentType as the type, as you were doing originally, but use initWithData:options:documentAttributes:error:, not initWithHTML:options:documentAttributes:.
Alternatively, create a plain NSString holding the source code, and then create an attributed string with that plain string plus whatever attributes you want to apply to the whole document (e.g., fixed-pitch font).