How would you detect any attempt to display an SSL URL that contains non-SSL assets in WebView? Just like a browser gives you a mixed-SSL warning.
I don't see any obvious property on UIWebView or UIWebViewDelegate. I could subclass NSURLProtocol, and somehow communicate the non-SSL connections back to the UIWebView. Is there an easier way to do this?
One solution is to create your custom URL cache and make it the default so you can catch all HTTP requests, and then you can check for mixed SSL content.
Here is an example:
#interface MonitoringURLCache : NSURLCache
#end
#implementation MonitoringURLCache
- (NSCachedURLResponse*)cachedResponseForRequest:(NSURLRequest *)request {
NSURL *requestURL = [request URL];
NSURL *pageURL = [request mainDocumentURL];
if ([[pageURL scheme] isEqualToString:#"https"] && [[requestURL scheme] isEqualToString:#"http"]) {
NSLog(#"Non safe resource: %# referenced from page: %#", requestURL, pageURL);
}
return [super cachedResponseForRequest:request];
}
#end
/// Register your custom cache
MonitoringURLCache *cache = [[MonitoringURLCache alloc] init];
[NSURLCache setSharedURLCache:cache];
/// Make a request to a website with mixed SSL content
NSURL *url = [NSURL URLWithString:#"https://some-website-with-mixed-ssl-content/"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webview loadRequest:request];
Related
I am working on WKWebView for loading some web pages. I need to pass some header inside WKWebView for change of language. I have passed successfully, however on server side, its showing other language. Please let me know whether the mechanism of passing is right or wrong.
- (void)viewDidLoad {
[super viewDidLoad];
WKWebViewConfiguration *theConfiguration = [[WKWebViewConfiguration alloc] init];
WKWebView *webView = [[WKWebView alloc] initWithFrame:self.view.frame configuration:theConfiguration];
webView.navigationDelegate = self;
NSURL *nsurl=[NSURL URLWithString:#""];
NSURLRequest *nsrequest=[NSURLRequest requestWithURL:nsurl];
[webView loadRequest:nsrequest];
[self.view addSubview:webView];
}
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler{
NSLog(#"%#",navigationAction.request.allHTTPHeaderFields);
NSMutableURLRequest *request = [navigationAction.request mutableCopy];
[request setValue:#"sv" forHTTPHeaderField:#"Accept-Language"];
decisionHandler(WKNavigationActionPolicyAllow);
}
There are two mistakes in your code:
1) You define the header fields too late (after the webview has already started to use the request to load the page)
2) You set the header on a mutable copy of the actual request (so not on the one that's used). This copy is then just dealloced once the method finishes.
Try this here in your viewDidLoad:
// ... start as you did
NSURL *nsurl=[NSURL URLWithString:#""]; // I assume you're using a correct URL in your actual code?
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:nsurl];
[request setValue:#"sv" forHTTPHeaderField:#"Accept-Language"];
[self.view addSubview:webView];
[webView loadRequest:nsrequest]; // I just prefer to add to the view hierarchy before I do anything with it, personal preference.
You do not need to do anything regarding the header fields in your webView:decidePolicyForNavigationAction:decisionHandler: delegate method.
I want to load a webpage with a huge javascript file.
<script src="js/sample.js"></script>
I cannot change the html file, but i can download the javascript file to my Application.
NSString* path = [NSString stringWithFormat:#"http://www.demo.url/index.html"];
NSURLRequest* request = [NSURLRequest requestWithURL:[NSURL URLWithString:path] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30.0];
[webView loadRequest:request];
Everytime I start my App the chached javascript file have to reload again. Maybe I have the wrong cache settings.
Two possible solution:
Cache the javascipt in the right way. Do not delete on app close.
Manual download javascript file and use it.
are you trying to inject javascript?
[webView stringByEvaluatingJavaScriptFromString:jsString];
if you are trying to load local html file for the 1st time, add it to the bundle and call it liek this:
NSURL *relativeURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
NSURL *u = [NSURL URLWithString:#"xxx.html" relativeToURL:relativeURL];
and then when you need to reload you do a normal uiwebview request method call!
I found the solution:
Implement an own NSURL Protocol:
NSURLProtocolCustom.h
#import <Foundation/Foundation.h>
#interface NSURLProtocolCustom : NSURLProtocol<NSURLConnectionDelegate> {
NSMutableData *responseData;
}
#property (nonatomic, strong) NSURLConnection *connection;
#end
NSURLProtocolCustom.m
#import "NSURLProtocolCustom.h"
#implementation NSURLProtocolCustom
+ (BOOL)canInitWithRequest:(NSURLRequest *)request {
if ([[request.URL lastPathComponent] isEqualToString:--Your precached file--]) {
return true;
} else {
return false;
}
}
+ (NSURLRequest *)canonicalRequestForRequest:(NSURLRequest *)request {
return request;
}
- (void)startLoading {
NSString* file = --Your local file--;
NSData *data = [NSData dataWithContentsOfFile:file];
NSURLResponse *response = [[NSURLResponse alloc] initWithURL:[self.request URL] MIMEType:#"text/javascript" expectedContentLength:-1 textEncodingName:nil];
[self.client URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed];
[self.client URLProtocol:self didLoadData:data];
[self.client URLProtocolDidFinishLoading:self];
}
- (void)stopLoading {
}
And your request
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:--your URL-- cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:50.0];
[NSURLProtocol registerClass:[NSURLProtocolCustom class]];
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
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.
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
}
}