Use UIWebView in console [duplicate] - ios

This question already has an answer here:
Async request does not enter completion block
(1 answer)
Closed 5 years ago.
I want to use UIWebView in the console, so I define a delegate:
#interface webViewDelegate : NSObject <UIWebViewDelegate>
and write the protocol:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;
- (void)webViewDidStartLoad:(UIWebView *)webView;
- (void)webViewDidFinishLoad:(UIWebView *)webView;
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error;
and then loadRequest:
webView = [[UIWebView alloc] init];
webView.delegate = WVdelegate;
NSURL *htmlURL = [NSURL URLWithString:#"http://192.168.0.100"];
NSURLRequest *request = [NSURLRequest requestWithURL:htmlURL];
[webView loadRequest:request];
The problem is that this is a console, and I want to finish the console after the delegate has been invoked. What should I do to wait for the delegate after calling loadRequest?

The problem is that you have no runloop. Thus, when your code comes to an end, the command-line tool comes to an end; things do not persist long enough for the asynchronous code to run.

You can get HTML source in console:
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
// Check here if still webview is loding the content
if (webView.isLoading)
return;
NSString *fullHtml = [webView stringByEvaluatingJavaScriptFromString:#"document.getElementsByTagName('html')[0].outerHTML"];
NSLog(#"html:%#", fullHtml);
}

Related

shouldStartLoadWithRequest: not called for images

I have a UIWebView and I make it load an HTML string. The string contains http urls to images. The images get loaded, but shouldStartLoadWithRequest: method is not called for them. It is called just once, for about : blank. However all the image requests are successfully passed into my NSURLCache subclass, into cachedResponseForRequest: method. But I would like to handle the requests in the shouldStartLoadWithRequest: method. Can anything be done about that? How can I make these requests go into the delegate method?
Here is the code:
#interface URLCache : NSURLCache
#end
#implementation URLCache
- (NSCachedURLResponse *)cachedResponseForRequest:(NSURLRequest *)request
{
NSLog(#"REQUEST = %#", request.URL);
return [super cachedResponseForRequest:request];
}
#end
#interface ViewController ()<UIWebViewDelegate>
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[NSURLCache setSharedURLCache:[URLCache new]];
NSString* path = [[NSBundle mainBundle] pathForResource:#"test" ofType:#"html"];
NSString* html = [NSString stringWithContentsOfFile:path
encoding:NSUTF8StringEncoding
error:nil];
self.webView.delegate = self;
[self.webView loadHTMLString:html baseURL:nil];
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSLog(#"URL = %#", request.URL);
return YES;
}
#end
The shouldStartLoadWithRequest method is called only for page loads (that is, when the page as a whole gets loaded or when a frame's contents get loaded), not when loading individual resources such as images, JavaScript, or CSS.
If you want to manipulate all HTTP/HTTPS requests from a UIWebView, the only way I'm aware of to do so is by using a custom NSURLProtocol subclass. See Apple's Custom HTTP Protocol sample code project for an example of how to do this.

UIWebView Current Link

I'm trying to get my webview's current URL by using the following code in webViewDidStartLoad:
NSString *currentURL = _webview.request.URL.absoluteString;
I set a break point and I found out that currentURL is actually empty. I went through the Internet some saying that you won't be able to get the URL until the webpage has compeletely loaded. Is that true?
You are trying to get the current URL of the webpage. You just have to log the request object and that will give you the URL that you clicked on the webview.
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)**request** navigationType:(UIWebViewNavigationType)navigationType{
NSLog(#"request on invite -=- %#", request);
}
You just have to use that request object and this will provide you the URL that is clicked.
Try to implement the following feature
<UIWebViewDelegate>
webview.delegate = self;
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
return YES;
}
//get url from webView augment variable
- (void)webViewDidStartLoad:(UIWebView *)webView{
NSString *currentURL = webView.request.URL.absoluteString;
NSLog(#"url:%#",currentURL);
}

pdf file is not showing in UIWebView when downloading from url in iOS 8.3?

I am using iOS 8.3 and when showing pdf file from url in UIWebView it is showing white screen. When I am using local file then it was showing correctly. Can anyone help me ?
NSURL *websiteUrl = [NSURL URLWithString:#"http://kmmc.in/wp-content/uploads/2014/01/lesson2.pdf"];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:websiteUrl];
_webView.backgroundColor=[UIColor lightGrayColor];
_webView.delegate=self;
[_webView loadRequest:urlRequest];
Your code seems perfect. Please make sure that the IB connections are proper. Then implement the delegate methods
- (void)webViewDidStartLoad:(UIWebView *)webView {
NSLog(#"Start Loading...");
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
NSLog(#"Finished Loading");
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
NSLog(#"Error Occured:%#", error);
}
It'll take some time to download, and keep eye on the delegate methods, see what are you getting on console

UIWebview disable redirect to AppStore

I have UIWebview in my app and this is how i navigate some times in the application to URL:
NSURL *url = [NSURL URLWithString:#"http://mp3skull.com/mp3/nirvana.html"];
NSURLRequest* request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
And sometimes i noticed that if i click on link the AppStore application is opened with some app.
It is possible to disable it?
In the method - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
You could do
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSString *requestedURL = [[request URL] absoluteString];
// URL for opening itunes according to Apple docs is something like this #"http://itunes.apple.com
// But I do believe that it will be some sort of URL scheme that opens it specifically to the app on the app store.
// So without an example this is the best I can provide.
if([requestedURL isEqualToString:#"http://itunes.apple.com"]) {
// or if([requestedURL rangeOfString:#"itunes.apple.com"].location==0) {
// What is happening here is that if the request url that is being request is
// "http://mp3skull.com/mp3/nirvana.html" then we don't want to continue with the request so stop.
return NO;
}
// Otherwise for all other requests continue
return YES;
}
Remember you will need to set the delegate on your UIWebView as this method is a UIWebViewDelegate method - see Apple Documentation on UIWebViewDelegate for more information
Yes it happens, some times when you click on some links, the application will open another application.
Because, those links contain other app schemas. (link)
so, to disable opening such url-schemas, we have to detect them and not load them.
As appStore has "itunes.apple.com",
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSString *requestedURL = [[request URL] absoluteString];
if([requestedURL rangeOfString:#"itunes.apple.com"].location==0) {
return NO;
}
return YES;
}

Can't Load Webpage with // in it to UIWebview

I am having difficulties gettin a webpage to load in my app. I think the issue has to do with it having back to back / in it at one point, but am not sure how to work around this. The URL I want it to visit is http://kaiopublications.org/content//iLuminateVol1.1/index.html
Here is my code:
- (void)viewWillAppear:(BOOL)animated {
NSString *html = _entry.articleUrl;
NSURL *url = [NSURL URLWithString:html];
NSLog(#"URL%#", html);
[_webView loadRequest:[NSURLRequest requestWithURL:url]];
}
The log for html comes back with the correct address, but if I run a log on url, it comes back null.
It must be something else or you may just need to wait. I tried it quickly myself and I can load the site with this URL in my test app. But I realize the site did load very slowly even on the simulator with a regular internet connection. If you try it on your device with a poor mobile bandwidth it maybe just takes very long.
One more thought. Is there any "noise" character at the end of the string?
Try this to see if it is that:
NSURL *url = [NSURL URLWithString:#"http://kaiopublications.org/content//iLuminateVol1.1/index.html"];
[_webView loadRequest:[NSURLRequest requestWithURL:url]];
Add some delegate methods to help you diagnose this. So you can display an activityIndicatorView while waiting for loads. It could be a slow site or it could be the delegate not setup.
#pragma mark - UIWebView delegate methods
- (void)webViewDidStartLoad:(UIWebView *)webView
{
if (!_activity.isAnimating)
[_activity startAnimating];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
[_activity stopAnimating];
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
if (!_activity.isAnimating)
[_activity startAnimating];
return YES;
}

Resources