Memory warning in webview - ios

I have the one webview which load the this url
https://sketchfab.com/models/9fb581d7cb414e0fbec4108099a73619/embed.
After loading this url my web view start showing the memory warning and after some time it crash. I am using the arc in my application
Here is my code:
(void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
NSURL *sUrl = [NSURL URLWithString:[NSString stringWithFormat:#"%#/embed",m_Data.m_sModelUrl]];
NSMutableURLRequest *sRequest = [NSMutableURLRequest requestWithURL:sUrl];
[sRequest setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[m_webView loadRequest:sRequest];
}
Please help me out.

You can try autoreleasepool to solve memory warning.
#autoreleasepool
{
NSURL *sUrl = [NSURL URLWithString:[NSString stringWithFormat:#"%#/embed",m_Data.m_sModelUrl]];
NSMutableURLRequest *sRequest = [NSMutableURLRequest requestWithURL:sUrl];
[sRequest setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
}
[m_webView loadRequest:sRequest];

Related

Get current URL in UIWebview but it not respond

I create app to use UIWebview and show log to url if touch in content in UIWebview.
this is my code
- (void)viewDidLoad {
[super viewDidLoad];
NSURL *url1 =[NSURL URLWithString:#"MyWebSite"];
NSURLRequest *request1 = [NSURLRequest requestWithURL:url1];
[_webView1 loadRequest:request1];
NSURL *url2 =[NSURL URLWithString:#"MyWebsite2"];
NSURLRequest *request2 = [NSURLRequest requestWithURL:url2];
[_webView2 loadRequest:request2];//url menu 2
NSString *currentURL = [_webView1 stringByEvaluatingJavaScriptFromString:#"document.title"];
NSLog(#"%#",currentURL);
}
but I touch the content log is not print and change webview log is print (null)
sorry for my poor English.
You can not get title of a website before you receive data from the URL.
So
Set your webview delegate to self
Then in
- webViewDidFinishLoad: to get title

Basic url not working in WebView

I am learing WebView.
when I do something like
NSString *url = #"www.google.com";
[_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]];
it fails.
But when I type www.google.com (or even google.com) in standard browser, it works fine.
I also noticed that after loading the page, the url text field in standard browser changes the link from www.google.com to https:// www. google.co.in/?gws_rd=ssl
In above code when I set NSString *url = #"https://www.google.co.in/?gws_rd=ssl" it works fine
So how do I implement my WebView view so that if should work like a standard browser it terms of above context
UIWebView always needs http or https when starting request, if you click on links inside the webView it will handle it itself. So here is how to handle it:
- (void)viewDidLoad
{
[super viewDidLoad];
UITextField *addressBar = [[UITextField alloc] init];
[addressBar setDelegate:self];
[self loadRequestFromString:#"www.google.com"];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[self loadRequestFromString:textField.text];
[textField resignFirstResponder];
return YES;
}
- (void)loadRequestFromString:(NSString *)urlString
{
NSURL *webpageUrl;
if ([urlString hasPrefix:#"http://"] || [urlString hasPrefix:#"https://"]) {
webpageUrl = [NSURL URLWithString:urlString];
} else if ([urlString containsString:#" "] || ![urlString containsString:#"."]) {
webpageUrl = [NSURL URLWithString:[NSString stringWithFormat:#"http://www.google.com/search?q=%#", [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];
} else {
webpageUrl = [NSURL URLWithString:[NSString stringWithFormat:#"http://%#", urlString]];
}
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:webpageUrl];
[_webView loadRequest:urlRequest];
}
to open any URL in UIWebView you should add url with protocol
eg #"http://www.google.com".
[NSURL URLWithString:#"http://www.google.com"];
then it will work.

UIwebview not working properly

I have three UIwebView added from story board when i load them i add activity indicator to show loading progress and when loading is finish. Delegate of DidFinishLoading called. But i see the white color on uiwebview. When i again load the UIwebview when white screen is visible then it shows the error "Operation couldn't be completed" but when loading of UIwebview is properly done and again i reload with new url. then it does not show any error.
Please somebody tell me.. Thanks in advance.
NSString* urlgmap=#"www.google.com"
NSURL *url_gmap = [NSURL URLWithString:urlgmap];
NSURLRequest *request_gmap = [NSURLRequest requestWithURL:url_gmap cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60];
[self.wrae_one loadRequest:request_gmap];
NSString* urlgrid=#"www.yahoo.com";
NSURL *url_grid = [NSURL URLWithString:urlgrid];
NSURLRequest *request_grid = [NSURLRequest requestWithURL:url_grid cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60];
[self.wrae_two loadRequest:request_grid];
NSString* urlSmap=#"www.facebook.com";
NSURL *url_smap = [NSURL URLWithString:urlSmap];
NSURLRequest *request_smap = [NSURLRequest requestWithURL:url_smap cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60];

UIWebview with SwipeGestureRecognizer

I have an UIWebview, when user taps a UIButton, my webview will be loaded. I have an 10 HTML files in bundle i want to show the first html in webview and when user **swipes** i want to show the next HTML file. When user wants to see previous HTML file, i want to allow swipe and see. I have used UISwipeGesture but how can i load the files one by one.. Please help me. I have loaded the files from bundle and i used
NSURLREQUEST *req = [NSURLREQUEST requestwithURL : myURL];
[webView loadRequest:req];
myURL is the path for first HTML. How can i do this for 10 HTML files using SWIPE
One simple thing you can do is rename your HTML files like 1.html, 2.html ..... 10.html
Declare an integar variable in your .h file initialize it in viewDidLoad and on detecting swipe right increment in that variable and use formatString to make correct url and reload the webview and on detecting swipe left decrement in that variable make correct url and reload the webview.
-(void)swipeRightDetected
{
if(fileNumber <= 10)
{
fileNumber++;
NSString *urlString = [NSString stringWithFormat:#"http://172.161.331.25/casa/admmathematic/Page00%d.html",fileNumber];
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
[webview loadRequest:request];
}
else
{
//No more files
}
}
-(void)swipeLeftDetected
{
if(fileNumber > 0)
{
fileNumber--;
NSString *urlString = [NSString stringWithFormat:#"http://172.161.331.25/casa/admmathematic/Page00%d.html",fileNumber];
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
[webview loadRequest:request];
}
else
{
//No more files
}
}

UIWebView doesnt load URL , iOS

I am trying to load a URL when my app starts.
I dragged an UIWebView Object , on my .nib , i created a property outlet , i connected it to the webview and in my view controller i place the following code :
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *fullURL = #"http://google.com";
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
}
However i just get a black screen on the device , like theres no view at all. Any idea?
This is the code I'm using in my application
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
NSURL *websiteUrl = [NSURL URLWithString:#"http://www.google.com"];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:websiteUrl];
[myWebView loadRequest:urlRequest];
}
This is short one
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
[myWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.google.com"]]];
}
Restart your IOS simulator.
It is really not evident, but check, at first, any site in Safari at IOS simulator.
After restart of IOS simulator, my webView opened successfully both at simulator and device.
By Amit

Resources