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.
Related
I've got this weird issue with my apps that uses internet connection. The app doesn't seem to update the data from internet in the right way. I've got a UIWebView with a simple chat. When I write a message in the chat with safari or in my web browser everything works fine. When writing something in the chat in the app, the data doesn't update, even when reloading the page.
It updates totally un-logically as well. Even when restarting the app the new data isn't shown, then all of a sudden it shows.
Had the same issue with another app too, which was not handled by a UIWebView. Any idea what it might be?
Example code:
- (void) viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
NSString *username = [[Database singletonDataBase] currentUserName];
NSString *urlAddress = [NSString stringWithFormat:#"%#?name=%#", #"http://webapi.com/index.php", username];
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:requestObj];
}
In your app delegates, didFinishLoading method, set the NSURLCache to not cache the data:
// Remove and disable all URL Cache, but doesn't seem to affect the memory
[[NSURLCache sharedURLCache] removeAllCachedResponses];
[[NSURLCache sharedURLCache] setDiskCapacity:0];
[[NSURLCache sharedURLCache] setMemoryCapacity:0];
If that doesn't work, you can also try setting the cache policy on the NSURLRequest:
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url
cachePolicy:NSURLRequestReloadIgnoringCacheData
timeoutInterval: 10.0];
I finally found the answer. Its nothing wrong with my code, as I thought. The problem lies on the server side. And, as they say in this thread:
NSURLConnection is returning old data
It seems to be badly configured server-side or proxy. Which makes the cache act all wierd. I tried another server and everything worked just fine!
Hope this helps someone with the same issue, cause it sure wasted me ALOT of time.
I am writing an App where I pass a Link as a NSString from a TableView to my DetailView. The String gets passed correctly, as I can see it in the log. The problem is, that the WebView just stays blank; unless I hard code any URL in. Even the ones that I can see in the log by just copy pasting them. What am I missing? Any help is greatly appreciated. Here's a sample log output and my code. I also checked this thread, where the guy hab the exact same problem as me, but none of the solutions posted there where helpful. UIWebView not loading URL when URL is passed from UITableView
2013-06-17 13:19:52.147 Feuerwehr[1661:c07] http://oliverengelhardt.de/ffw_app/Einsaetze/GB0505/index.html
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *urlString = url;
NSURL *myURL = [NSURL URLWithString:urlString];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:myURL];
[webView loadRequest:requestObj];
NSLog(urlString);
}
I think you stringUrl is nil when it loads (so it doesn't get passed. Break point view did load and see the value of the urlstring
Based on what you've said, your url isn't formatted correctly
i think you are missing delegate
self.webView.delegate = self;
NSURL *url = [NSURL URLWithString:#"http://locationofurlonlocalhost.mp4"];
...
NSMutableURLRequest *req;
req = [NSMutableURLRequest requestWithURL:url // <-- CRASH IS HERE!! thx to a breakpoint stepthrough
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:30.0];
a log of url will result in:
Printing description of url:
<NSURLRequest http://locationofurlonlocalhost.mp4>
the crash will log
-[NSURLRequest absoluteURL]: unrecognized selector sent to instance 0x88003e0
The URL looks just fine so I'm not sure what's going on. Could this happen because the url is unreachable the simulator? I can connect to the url on the sim's safari as well as my desktop safari. What should I do?
** EDIT **
it's been determined that the url object is being released too early. In the ... the url is passed to a different selector and then the rest of the code takes place there. How can I force retain the url? I have already tried:
__strong NSURL *url = [[NSURL alloc] initWithString:#"http://locationofurlonlocalhost.mp4"];
This most definitely looks like your url has been released somehow. If you print the description of the url variable and wind up with an NSURLRequest, then you're looking at freed up memory.
Try using alloc / initWithString: as a troubleshooting step.
The function loadHTMLString from UIWebView costs too much memory and will activate the didReceiveMemoryWarning of the current UIViewController.
How do I solve it please :D
it might be that your html NSString for loadHTMLString: too long, which I mean that the NSString's size for loadHTMLString: is too large. And if you have some image url in your html string, it might have problem,too.
How about storing your HTML in a file and then:
NSURL *fileURL = [NSURL fileURLWithPath:filePath];
NSURLRequest *request = [NSURLRequest requestWithURL:fileURL];
[webView loadRequest:request];
if you use webview only to load one html page
i think you should optimize your page th solve it
if you load many pages you should clean the webview before you load another or dealloc
like this
[webView loadHTMLString: #"" baseURL: nil];
I use the google maps API to trace the route between two points, my code is this :
NSString *urlString = [NSString stringWithFormat:#"https://maps.google.com/maps?saddr=%f,%f&daddr=%f,%f&hl=fr&output=embed&om=5",latUtilisateur,longUtilisateur,latStation,longStation];
//Create a URL object.
NSURL *url = [NSURL URLWithString:urlString];
//URL Requst Object
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
//Load the request in the UIWebView.
[webView loadRequest:requestObj];
However, this didn't has the textual instructions feature. Am i missing some parameters in the request above or something else?
The output=embed parameter is resulting in the full screen map. You can try a couple of alternatives:
For the standard map/directions page.
http://maps.google.com/maps?saddr=Sydney&daddr=Brisbane&hl=fr&output=html&om=5
For a mobile friendly directions
page. This version includes links to
static maps for each step of the
journey.
http://maps.google.com/maps?saddr=Sydney&daddr=Brisbane&hl=fr&output=mobile&om=5