iOS webview connection error handling - ios

I'm trying to load a url . But sometimes it gives a error url in the following delegate method.
- (BOOL)webView:(UIWebView *)theWebView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
NSURL* url = [request mainDocumentURL];
NSString* absoluteString = [url absoluteString];
NSLog(#"%#",absoluteString);}
I want to know that how can i cancel the current url request and reload a new url ?

try this
if([webView isLoading])
{
[webView stopLoading];
}

Related

IOS Webkit to track the url which use is clicked and URL Scheme

Am trying to migrate my iOS UIWebview to Webkit, but along the line I ran into lot of problems. I have a code in webView:shouldStartLoadWithRequest:navigationType: before which I used to monitor urls and url-scheem like tell:, exit:, refresh:, mailto: and to make sure that only my url can open in the WebView. But trying to implement that same way using webkit it didn't work please am not sure if am doing it in the write method can anyone help me.
/Trying to implement it with webkit/
- (BOOL)webView:(WKWebView *)inWeb decidePolicyForNavigationAction:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType{
NSLog(#"Event: %#", #"shouldStartLoadWithRequest inType called");
NSArray *schemeArray = #[#"share", #"map", #"rate", #"reload", #"exit"];
NSString *url = [[inRequest URL] query];
NSString *scheme = [[inRequest URL] scheme];
NSString *StrPurl = [NSString stringWithFormat:#"%#",url];
NSLog(#"[inRequest URL] == %#", StrPurl);
NSLog(#"[inRequest NSURL] == %#", url);
NSLog(#"[[inRequest URL] scheme] == %#", scheme);
if ([StrPurl containsString:#"mysite.com"]){
//coninue open
}else if ( [schemeArray containsObject:[[inRequest URL] scheme]] ){
//open share intent
NSLog(#"Event: Share URL %#", [[inRequest URL] scheme]);
}
NSLog(#"Event: Request URL %#", url);
return NO;
}
Using the above example in UIWebview it work
- (BOOL)webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType{
if (self.validatedRequest || inType != UIWebViewNavigationTypeLinkClicked){
NSString *url = [[inRequest URL] query];
NSString *scheme = [[inRequest URL] scheme];
NSString *StrPurl = [NSString stringWithFormat:#"%#",url];
NSLog(#"[inRequest URL] == %#", StrPurl);
NSLog(#"[inRequest NSURL] == %#", url);
NSLog(#"[[inRequest URL] scheme] == %#", scheme);
}
}
Also I have this method but it doesn't get called when I click on tel: or other external url
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
NSLog(#"Event: %#", #"shouldStartLoadWithRequest navigationType called");
//if (navigationAction.navigationType == WKNavigationTypeLinkActivated) {
if (navigationAction.navigationType == UIWebViewNavigationTypeLinkClicked) {
}
NSString *url = [navigationAction.request.URL query];
NSLog(#"Event: Request URL %#", url);
decisionHandler(WKNavigationActionPolicyAllow);
}
Try this delegate method:
- (void)webView:(WKWebView *)webView
decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction
decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler;
Instead of returning NO you will need to call either decisionHandler(WKNavigationActionPolicyAllow) or decisionHandler(WKNavigationActionPolicyCancel)
You can access the NSURLRequest using navigationAction.request.
Source: https://developer.apple.com/documentation/webkit/wknavigationdelegate/1455641-webview?language=objc

UIWebView not getting next Url to Load

I am working on a native cum web iOS app. When i try to load Urls in UIWebView on any button press inside WebView i am unable to get the next URL to be loaded. Can anyone suggest anything for this? Thanks in advance.
Here is my Code:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSString *myString = [[request URL] absoluteString];
[myString lowercaseString];
NSLog(#"%#",myString)
NSDictionary *headers = [request allHTTPHeaderFields];
BOOL hasReferer = [headers objectForKey:#"X"]!=nil;
if (hasReferer)
{
return YES;
}
else
{
// relaunch with a modified request
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
dispatch_async(dispatch_get_main_queue(), ^{
NSURL *url = [request URL];
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
[request setValue:X forHTTPHeaderField:#"X"];
[request setValue:Y forHTTPHeaderField:#"Y"];
[loadingWebView loadRequest:request];
});
});
return NO;
}
return 0;
}
set delegate
- (void)webViewDidStartLoad:(UIWebView *)webView;
- (void)webViewDidFinishLoad:(UIWebView *)webView;
- (void)webView:(UIWebView *)webView didFailLoadWithError:(nullable NSError *)error;
see in this method what it returns
and make sure you are passing correct url ie stringByAddingPercentEscapesUsingEncoding

LaunchServices: ERROR: There is no registered handler for URL scheme applewebdata

I have a code that opens a link inside a webview in Safari, which looks like this.
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
if (navigationType == UIWebViewNavigationTypeLinkClicked){
NSLog(#"%#", request);
NSURL *url = request.URL;
[[UIApplication sharedApplication] openURL:url];
}
return YES;
}
However, when I click the link, it shows the error
LaunchServices: ERROR: There is no registered handler for URL scheme applewebdata
The environment is iOS 9. Is there some setting in the plist I need to change?
Here's how I fixed the issue:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
if (navigationType == UIWebViewNavigationTypeLinkClicked){
NSString *requestURLString = request.URL.absoluteString;
NSString *trimmedRequestURLString = [requestURLString stringByReplacingOccurrencesOfString:#"^(?:applewebdata://[0-9A-Z-]*/?)" withString:#"" options:NSRegularExpressionSearch range:NSMakeRange(0, requestURLString.length)];
trimmedRequestURLString = [trimmedRequestURLString stringByReplacingOccurrencesOfString:#"%22" withString:#""];
NSLog(#"%#", trimmedRequestURLString);
NSURL *url = [NSURL URLWithString:trimmedRequestURLString];
NSLog(#"%#", url);
[[UIApplication sharedApplication] openURL:url];
}
return YES;
}
Just manipulating the URL string to have no applewebdata and just plain URL format worked.

Objective-C getting mime type of url file before webview loads it

I'm facing a problem building a web browser with download functionality, here's my code:
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
{
NSURL *url = request.URL;
NSURLRequest *req = [NSURLRequest requestWithURL:url];
NSURLConnection *conn = [NSURLConnection connectionWithRequest:req delegate:self];
[conn start];
return YES;
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
if ([[response MIMEType] rangeOfString:#"video"].location != NSNotFound) {
// Do something with that video
}
}
This is currently working as intended, video files will be handled correctly but the webview will also load it, what i need to do is capture the mime type of the file before returning YES in shouldStartLoadWithRequest and return NO if it's a video.
I tried the sendSynchronousRequest method but it slows the app, I also tried:
#import <MobileCoreServices/MobileCoreServices.h>
NSString *fileExtension = [myFileURL pathExtension];
NSString *UTI = (__bridge_transfer NSString *)UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, (__bridge CFStringRef)fileExtension, NULL);
NSString *contentType = (__bridge_transfer NSString *)UTTypeCopyPreferredTagWithClass((__bridge CFStringRef)UTI, kUTTagClassMIMEType);
But I often get wrong myme types, last thing, I dont want to detect type by the file extension since urls can be formatted as aliases.
Thank you for your help.
NSURLConnection is dead so stop using it. Switch to NSURLSession. NSURLSession gives you a data task delegate method that lets you examine the response header and bow out.

UIWebView Detect mp3 file loading

In my application i have UIWebView and i want to detect if a MP3 file is load(download).
So i use this UIWebView Delegate method:
- (BOOL)webView:(UIWebView*)webview shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
NSURL *url = [request URL];
if ([[[url pathExtension] lowercaseString] isEqualToString:#"mp3"]) {
[self userDidClickUrl:url];
return NO;
}
return YES;
}
The problem is that sometimes the URL is without mp3 string inside,and the UIWebView open the Native player. It's possible to detect it? I want to detect when a mp3 file is start loading.
Try to detect MIMEType based on your request - check if MIMEType from response is either one of those kind:
audio/mp3 || audio/mpeg3 || audio/x-mp3 || audio/x-mpeg3
Update: check this already answered:
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
NSURL *url = request.URL;
NSURLRequest *req = [NSURLRequest requestWithURL:url];
NSURLConnection *conn = [NSURLConnection connectionWithRequest:req delegate:self];
[conn start];
return YES; }
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
NSString *mime = [response MIMEType];
NSLog(#"%#",mime); }
Full link bellow:
UIWebView Delegate get MIME Type

Resources