Proper way to load in html file from resources into UIWebView? - ios

I thought it would be easy to load in a html file from my resources into a UIWebView, but it looks like it requires something extra that I'm not doing. So far I have in my viewDidLoad in my controller:
NSURL *url = [NSURL URLWithString:#"radialGradient.html"];
NSURLRequest *req = [NSURLRequest requestWithURL:url];
[webView loadRequest:req];
webView.frame = CGRectMake(0, 0, 768, 1024);
I've tried regular web addresses (google.com) and it works fine. But with the file in my sources it just renders a white blank screen.

Two things:
1)
What you have now is not a URL. It's just a filename. You need to be prepending a "file:///" in front of that radialGradient.html.
2)
And, you'll likely also need to put in a resolvable path to radialGradient.html Right now your app doesn't know where to look for it.
Something like:
NSURL * resourcePathURL = [[NSBundle mainBundle] resourceURL];
if(resourcePathURL)
{
NSURL * urlToLoad = [resourcePathURL URLByAppendingPathComponent: #"radialGradient.html"];
if(urlToLoad)
{
NSURLRequest * req = [NSURLRequest requestWithURL: urlToLoad];
[webView loadRequest: req];
}
}

Related

Download PDF from Resources Bundle

I am able to successfully view a PDF from a website.
The code I am currently using:
PDFAddress = [NSURL URLWithString:#"http://www.msy.com.au/Parts/PARTS.pdf"];
request = [NSURLRequest requestWithURL:PDFAddress];
[webView loadRequest:request];
webView.scalesPageToFit = YES;
However, I have added that pdf in my bundle resources and I want user to download it from there, by doing so, there is no network required. I wonder how it could be done?
Make a url that points to your resource bundle
NSURL *url = [[NSBundle mainBundle] URLForResource:#"myPDF" withExtension:#"pdf"];

UIWebView does not load full content from an url

I have a UIWebView where i load a url. But it does not load full content. I have checked all possible forums for solution but couldn't find a breakthrough yet. Any help would be really appreciated? You can try out the below url yourself. Basically, the url is supposed to contain html5 content/player.
-(void)loadPlayerUrl
{
activityIndicatorView.hidden = NO;
NSURL* nsUrl = [NSURL URLWithString:#"http://watch.nimbletv.com/tv"]
NSURLRequest* request = [NSURLRequest requestWithURL:nsUrl cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:30];
[self.playerWebView loadRequest:request];
}
However, this works perfectly in safari or any other browser.
You should create ViewControllerWebPage
Then
- (void)viewDidLoad
{
[super viewDidLoad];
self.screenName = #"Info Ekran";
NSURL *mobileCreaURL = [NSURL URLWithString:#"http://mobilecrea.com/"];
NSURLRequest *myRequest = [NSURLRequest requestWithURL:mobileCreaURL];
[mobileCrea loadRequest:myRequest];
}
Also create new view, select your new custom class (ViewControllerWebPage) and than create UIWebView

UIWebView not displaying URL

Pretty easy enough problem, but It's driving me crazy. I connected my uiwebview to the .h and called it myWebView. then in the viewdidLoad, i did this:
NSString *urlString = #"www.google.com";
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[myWebView loadRequest:request];
But when I run the app I just see a blank screen...
I don't know what's going on.
All help is appreciated, thanks
Your urlstring isn't well-formed (for your intent)
Use http://www.google.com/
Technically www.google.com is a valid URL (according to things like RFC 1738 and 3986), but since it lacks the scheme (the http part), the UIWebView doesn't quite know what to do with it.

Need a way to build an NSURL correctly

I am storing website addresses from users in regular NSStrings.
Some are given as "www.somewebsite.com" and some as "http://somewebsiteothersite.org".
My app should open a UIWebView and open that webpage:
NSURLRequest *requestObj = [NSURLRequest requestWithURL:[NSURL URLWithString:[self websiteString]]];
NSLog(#"visiting: %#",websiteString);
[webView loadRequest:requestObj];
But what happens is that if the http:// is omitted, the UIWebView won't open the page.
Is there a descent way to build the NSURL correctly for UIWebView?
Thanks!
Just add http:// if it's not there?
if (![urlStr hasPrefix:#"http://"] && ![urlStr hasPrefix:#"https://"]) {
urlStr = [#"http://" stringByAppendingString:urlStr];
}
beware of links that are intended to not have http:// for example ftp:// or mailto:
I know Mathiass answered quicker but I want to share a similar solution
//first detect if your string has contains http:// if not add http:// to your string
NSMutableString *websiteString = #"www.x.com";
if ([websiteString rangeOfString:#"http://"].location == NSNotFound) {
NSLog(#"contains http:// == false");
websiteString = [#"http://" stringByAppendingString:websiteString];
} else {
NSLog(#"contains http:// == true");
}
NSURLRequest *requestObj = [NSURLRequest requestWithURL:[NSURL URLWithString:[self websiteString]]];
NSLog(#"visiting: %#",websiteString);
[webView loadRequest:requestObj];

How do i convert UIWebView to bitmap

Hi everybody i'm stuck with trying to convert my UIWebView, in which i'm currently displaying a ppt file, to a bitmap image.
I have this code:
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 768, 708)];
NSString *path = [[NSBundle mainBundle] pathForResource:documentName ofType:nil];
NSURL *targetURL = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[webView loadRequest:request];
NSLog(#"Ouverture du contexte image");
UIGraphicsBeginImageContext(webView.bounds.size);
[webView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
The webview is actually well displayed but I didn't manage to create it's render to an image,
If anybody knows where is the issue ;)
Thankss
There's nothing wrong with your screenshot-taking code — you're just using it prematurely.
[webView loadRequest:request]; initiates an asynchronous client request. That means you're taking a screenshot of webView before it even loads its contents.
Move your screenshot-taking code to UIWebViewDelegate's method webViewDidFinishLoad:. Of course you're going to have to assign a delegate to your webView to achieve this.

Resources