I have the following code
NSURL *nsurl = [NSURL urlWithString:#"http://url.that.does.not.exist.com"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:nsurl];
WKWebViewConfiguration *config = [WKWebViewConfiguration new];
WKWebView *wv = [[WKWebView alloc] initWithFrame:[[UIScreen mainScreen] bounds] configuration:config];
wv.navigationDelegate = self;
And the delegate:
- (void)webView:(WKWebView *)webView
decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse
decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler {
...
}
Works fine and I can catch errors when the could not be loaded (404, etc). But how do I catch the error when the nsurl was something WKWebView cannot even handle?
For example:
NSURL *nsurl = [NSURL urlWithString:#"nacho"];
will lead to a blank page with no delegate method being called. How can I handle detect this error? (I have checked at this iPhone SDK: Check validity of URLs with NSURL not working? but I wonder if there is anything better than regex?)
You can try and see if the url can be requested like this:
NSURL *nsurl = [NSURL urlWithString:#"nacho"];
if([NSURLConnection canHandleRequest:[NSURLRequest requestWithURL: nsurl]]){
NSLog(#"URL can be loaded");
}else{
NSLog(#"URL cannot be loaded");
}
Related
Same url in run in lower then iOS10 (ex. iOS8,iOS9) work properly.
-(void) loadUrl
{
NSString *urlAddress = #"http://myurl.com";
//Create a URL object.
NSURL *url = [NSURL URLWithString:urlAddress];
//URL Requst Object
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
//Load the request in the UIWebView.
[detailWebView loadRequest:requestObj];
}
I tried your code and it works fine in ios 10 and xCode 8.
I used this,
-(void) loadUrl
{
NSString *urlAddress = #"http://google.com";
//Create a URL object.
NSURL *url = [NSURL URLWithString:urlAddress];
//URL Requst Object
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
//Load the request in the UIWebView.
[self.webView loadRequest:requestObj];
}
And call this method where you want using [self loadUrl];
Add URLSchemes in your plist file for your url.
I am getting webview like,
Your code is not wrong.
Hope this will help you.
In apps that run in iOS 8 and later, use the WKWebView class instead of using UIWebView.import webkit.h and use this code
#import <WebKit/WebKit.h>
WKWebViewConfiguration *theConfiguration = [[WKWebViewConfiguration alloc] init];
WKWebView *webView = [[WKWebView alloc] initWithFrame:self.view.frame configuration:theConfiguration];
NSURL *nsurl=[NSURL URLWithString:#"http://www.apple.com"];
NSURLRequest *nsrequest=[NSURLRequest requestWithURL:nsurl];
[webView loadRequest:nsrequest];
[self.view addSubview:webView];
I am implementing instagram login, with client id and all credential. From browser it's returning access token but in my code it is showing 403 error with message "You are not sandbox user of this client".from manage client in developer account I have unchecked the disable implicit option but still from my app getting this error.
any help ??
my code is like
- (void)viewDidLoad
{
[super viewDidLoad];
_webview = [[UIWebView alloc]init];
_webview.frame = self.view.frame;
_webview.delegate = self;
[self.view addSubview:_webview];
NSString *scopeStr = #"scope=comments+relationships";
if ([_typeOfAuthentication isEqualToString:#"UNSIGNED"])
{
NSString *url = [NSString stringWithFormat: #"%#?client_id=%#&redirect_uri=%#&response_type=token&scope=%#&DEBUG=True",
KAUTHURL,
KCLIENTID,
kREDIRECTURI,
INSTAGRAM_SCOPE];
[_webview loadRequest: [NSURLRequest requestWithURL: [NSURL URLWithString: url]]];
[_webview setDelegate:self];
} else {
NSString *url = [NSString stringWithFormat: #"%#?client_id=%#&redirect_uri=%#&response_type=code&scope=%#&DEBUG=True",
KAUTHURL,
KCLIENTID,
kREDIRECTURI,
INSTAGRAM_SCOPE];
[_webview loadRequest: [NSURLRequest requestWithURL: [NSURL URLWithString: url]]];
[_webview setDelegate:self];
}
}
I have created an app which is a web browser. Whenever I enter any URL in text field it goes to the required web page. I have included an UIWebView to display webpage. By default google website is opened. But when I select any website from google I want to display that URL in the textfield. How do I do it?
- (void)viewDidLoad
{
[super viewDidLoad];
self.myTextField.text = #"http://www.google.com";
NSURL *myURL = [NSURL URLWithString:#"http://www.google.com"];
NSURLRequest *myRequest = [NSURLRequest requestWithURL:myURL];
[_myWebView loadRequest:myRequest];
self.myTextField.delegate = self;
}
You can do like this
- (void)viewDidLoad
{
[super viewDidLoad];
self.myTextField.text = #"http://www.google.com";
NSURL *myURL = [NSURL URLWithString:#"http://www.google.com"];
NSURLRequest *myRequest = [NSURLRequest requestWithURL:myURL];
[_myWebView loadRequest:myRequest];
_myWebView.delegate = self;
self.myTextField.delegate = self;
}
- (void)webViewDidFinishLoad:(UIWebView *)webView;
{
NSURL *requestURL = [webView.request URL];
txtView.text = [requestURL absoluteString];
}
i triad to click on URL which is in PDF file. and PDF file opened in UIWebView, but i cant able to click on URL.
any one tell me how to open URL which is in PDF file.
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 20, 320, 460)];
NSURL *targetURL = [NSURL URLWithString:#"http://partners.adobe.com/public/developer/en/acrobat/PDFOpenParameters.pdf"];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[webView loadRequest:request];
[self.view addSubview:webView];
In you webView deleget method, first of detect the url from PDF and then open in webview or etc. I have used following method to do this, may be its helping you :
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSURL *requestURL = [[ request URL] retain];
if (([[requestURL scheme] isEqualToString: #"http"] || [[requestURL scheme] isEqualToString: #"https"]) && (navigationType == UIWebViewNavigationTypeLinkClicked))
{
// Your method when tap on url found in PDF
}
}
If you have a PDF file in Local storage
NSString *html = [NSString stringWithContentsOfFile:path1 encoding:NSUTF8StringEncoding error:nil];
[self.webView loadHTMLString:html baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle]bundlePath]]];
If you have Access to file From the Internet
- (void) loadRemotePdf
{
CGRect rect = [[UIScreen mainScreen] bounds];
CGSize screenSize = rect.size;
UIWebView *myWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0,0,screenSize.width,screenSize.height)];
webView.autoresizesSubviews = YES;
webView.autoresizingMask=(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth);
NSURL *myUrl = [NSURL URLWithString:#"http://www.mysite.com/test.pdf"];
NSURLRequest *myRequest = [NSURLRequest requestWithURL:myUrl];
[webView loadRequest:myRequest];
[window addSubview: myWebView];
[myWebView release];
}
I am loading different file types like PDF, Excel, Doc etc in UIWebview. Some files requires authorization and passed the value in header.
This works fine in ios 6. Not working in ios 7.
Below is the code and error message.
NSURL *url =[NSURL URLWithString:regularURL];
self.webView.scalesPageToFit=YES;
self.request = [NSMutableURLRequest requestWithURL:url];
[self.request setValue:#"multipart/form-data" forHTTPHeaderField:#"Accept"];
NSString *auth = [NSString stringWithFormat:#"Bearer %#",userToken];
[self.request setValue:auth forHTTPHeaderField:#"Authorization"];
Error Message:
Error Domain=WebKitErrorDomain Code=102 "Frame load interrupted" UserInfo=0xd4b5310 {
Is there any additional header field to be passed for ios 7 web view?
I have tried to solve the problem with NSURLCache solution but that didn't work for me.
You have to try next:
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
NSString *strUrl = [strUrl stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
NSURL *targetURL = [NSURL URLWithString:strUrl];
NSData *dataFromUrl = [NSData dataWithContentsOfURL:[NSURL URLWithString: strUrl]];
[webView loadData:dataFromUrl MIMEType:#"application/pdf" textEncodingName:nil baseURL:nil];
[self.view addSubview:webView];
It work for the all files that have not worked before (pdf, doc, etc).
I have kind of found a solution. Not perfect but it works!
Set a shared URL cache before loading the request, then intercept the error and load the cached data with the correct MIME type into the webView manually.
NSURLCache *URLCache = [[NSURLCache alloc] initWithMemoryCapacity:4 * 1024 * 1024 diskCapacity:256 * 1024 * 1024 diskPath:nil];
[NSURLCache setSharedURLCache:URLCache];
And then
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
NSCachedURLResponse* cachedResponse = [[NSURLCache sharedURLCache] cachedResponseForRequest:self.originalRequest];
if (cachedResponse) {
CFStringRef UTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, (__bridge CFStringRef)(cachedResponse.response.URL.pathExtension), NULL);
CFStringRef MIMEType = UTTypeCopyPreferredTagWithClass(UTI, kUTTagClassMIMEType);
CFRelease(UTI);
NSString* MIMETypeString = (__bridge_transfer NSString *)MIMEType;
[self.webView loadData:cachedResponse.data MIMEType:MIMETypeString textEncodingName:nil baseURL:nil];
}
}
Naturally, you must set your WebViews delegate to somewhere you put the above delegate method.
This code works just fine in iOS 7 , screenshot attached. Hope it helps...
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(10, 10, 320, 480)];
NSURL *targetURL = [NSURL URLWithString:#"http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UIWebView_Class/UIWebView_Class.pdf"];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[webView loadRequest:request];
[self.view addSubview:webView];