I use UIWebView in my app and it all worked fine in Xcode4,5,6 simulator. but not for Xcode 7 simulator, I don't know why, there is no warning or error in simulator, and the screen is just showing blank page. Please help me. Thanks.
#import "IndexViewController.h"
#interface IndexViewController ()
#end
#implementation IndexViewController
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *urlString = nil;
NSString *languageCode = [[NSLocale preferredLanguages] objectAtIndex:0];
if ([languageCode isEqualToString:#"zh-Hans"]) {
urlString = #"http://www.originoftime.net/index-cn";
}else if ([languageCode isEqualToString:#"zh-Hant"]) {
urlString = #"http://www.originoftime.net/index-cn";
}else{
urlString = #"http://www.originoftime.net/index-en";
}
NSURL *url = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];
NSURLRequest *urlrequest = [NSURLRequest requestWithURL:url];
[_Index loadRequest:urlrequest];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
Xcode 7 with iOS9 now force you to not use HTTP call, but HTTPS ones.
this is a security point enhanced in AppTransportSecurity.
Try this:
Go to your info.plist
Add a dictionary called NSAppTransportSecurity
Add a boolean attribute to this one, called NSAllowsArbitraryLoads
Pass it to TRUE
Reload your app.
I advice you that if Apple want to block HTTP (unsecured) calls is for a good reason. http://www.originoftime.net/index-cn has a HTTPS one but the server certificate seems to be self-signed.
Let me know if this workaround work for you
Cheers from france
Do you implement the web view delegate methods? In particular:
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
If a load fails, that will tell you what the issue is.
It could well be the error related to the new security model which is being enforced for network access. You can override this new behavior by adding the following into your Info.plist file. Just edit the XML and paste this in:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key><true/>
</dict>
The changes are summarized here: https://developer.apple.com/library/prerelease/ios/technotes/App-Transport-Security-Technote/index.html
Related
The local code below loads into a WKWebView a local HTML file (from the app container, not the app bundle).
- (void)loadIndexConteneurWithHash:(NSString *)hash
{
NSString *fileName = #"index.html";
NSString *subpath = hash ? [NSString stringWithFormat:#"%##%#", fileName, hash] : fileName;
NSString *rootContainerDirectoryPath = [NSFileUtility pathRelativeToContentDirectoryForSubpath:#"/www/v2"];
NSURL *URL = [NSURL URLWithString:subpath relativeToURL:[NSURL fileURLWithPath:rootContainerDirectoryPath]];
[[NSURLCache sharedURLCache] removeAllCachedResponses];
[self.webView loadFileURL:URL allowingReadAccessToURL:URL];
// This below has the same effect (works on simulator and not on device)
// NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:10.0f];
// [self.webView performSelectorOnMainThread:#selector(loadRequest:) withObject:request waitUntilDone:true];
}
The file url on device is : file:///var/mobile/Containers/Data/Application/3CF58895-866D-400A-B0E0-4CA98BF143F7/Library/Application%20Support/<my.app.id>/LocalContents/www/v2/indexConteneur.html
And on simulator : file:///Users/<me>/Library/Developer/CoreSimulator/Devices/4A5B9713-6589-4DCA-ABBD-F4FF1246AE43/data/Containers/Data/Application/717B54FD-1344-41D3-8B53-FC0767FFE892/Library/Application%20Support/<my.app.id>/LocalContents/www/v2/indexConteneur.html
This code works fine on any simulator I've tried (ios 12 / ios 13).
It however does not load the HTML file on the device. When I use the Safari debugger, I see that the webview is on "about:blank".
I have no error at runtime, it just seem to stop executing the loadFileURL silently.
What could I do to solve / investigate on this issue?
EDIT 1
Neither the didFailNavigation nor the didFinishNavigation are called on device...
EDIT 2
Adding this code:
NSFileManager *fileManager = [NSFileManager defaultManager];
NSLog(#"%# exists? %d",URL.absoluteString,[fileManager fileExistsAtPath:URL.absoluteString]);
gives this on device:
2019-10-17 18:42:48.955588+0200 gall-app[881:230671] /var/mobile/Containers/Data/Application/3CF58895-866D-400A-B0E0-4CA98BF143F7/Library/Application%20Support/<my.app.id>/LocalContents/www/v2/indexConteneur.html exists? 1
So it exists at the expected location. I've also downloaded the app container from the device with XCode and I see the file where it should be.
EDIT 3
After further testing, it seems that [self.webView loadFileURL:... is not working on ios 12 (simulator included) but works on ios 13...
There might be some security rule difference between them?
In my info.plist, I have:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
<key>NSAllowsArbitraryLoadsInWebContent</key>
<true/>
</dict>
And my webview is initialized with this code:
- (void)viewDidLoad
{
[super viewDidLoad];
self.webView.UIDelegate = self;
self.webView.navigationDelegate = self;
self.webView.configuration.preferences.javaScriptEnabled = YES;
self.webView.configuration.preferences.javaScriptCanOpenWindowsAutomatically = YES;
[self.webView.configuration.preferences setValue:#YES forKey:#"allowFileAccessFromFileURLs"];
[self.webView.configuration setValue:#YES forKey:#"allowUniversalAccessFromFileURLs"];
[self addUserScriptToUserContentController:self.webView.configuration.userContentController];
OK I have found "why" it was not working. I changed this:
[self.webView loadFileURL:URL allowingReadAccessToURL:URL];
to this:
[self.webView loadFileURL:URL.absoluteURL allowingReadAccessToURL:rootAppURL];
with rootAppURL being:
NSString *rootappPath = [NSFileUtility pathRelativeToContentDirectoryForSubpath:#"/www"];
NSURL *rootAppURL = [NSURL fileURLWithPath:rootappPath isDirectory:YES];
And it now works on ios < 13
I am getting such error in my iOS application when I am going to open some web pages using buil-in web view engine (based on Safari but Safari works fine):
This page contains the following errors: error on line 2 at column 59:
SystemLiteral " or ' expected. Below is a rendering of the page up to
the first error.
(nothing is rendered).
The funny thing is that these websites can be opened on iPhone's version of my app but not on iPad.
Looking into the Internet I found hundreds of thousands websites which cannot be handled by google robots - when I search in google:
"59: SystemLiteral " or ' expected"
it lists search results but all found websites' descriptions shows my error. However when I open any of them they are rendering fine on desktop browser and iOS Safari.
Anybody knows what may be the reason of that? Non of websites opened from this google search can be rendered in my app on iPad - all shows the same error.
I also tried debug it using Mac's Safari Developer mode but it does not give any hint.
Update: I prepared a minimal project, added UIWebView controller and it shows the same error.
ViewController.h:
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
#property (weak, nonatomic) IBOutlet UIWebView *webView;
#end
ViewController.m:
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *fullURL = #"http://catalog.llc.lib.ms.us/polaris/";
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[_webView loadRequest:requestObj];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
#end
I also tried few web pages with this code (all from google search I mentioned above), they show also different erros, like "error on line 1077 at column 9: Opening and editing tag mismatch: META line 0 and head." - this comes from http://www.lucindalayton.com/home/?ID=15449 but still they cannot be rendered.
Looking at the response from that web site, it has a blank line at the start of the response, which seemed a little curious. In my first attempt, I trimmed that whitespace:
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *fullURL = #"http://catalog.llc.lib.ms.us/polaris/";
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
if (!data) {
NSLog(#"%s: sendAsynchronousRequest error: %#", __PRETTY_FUNCTION__, connectionError);
return;
}
NSString *html = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
html = [html stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
[_webView loadHTMLString:html baseURL:url];
}];
}
But I later noticed that you don't have to trim the whitespace, but rather merely using loadHTMLString seemed to be enough.
I'm unclear as to why Safari handles this properly, as does the iPhone web view, but not the iPad web view. But this seems to get around it.
Rob, thanks from hint but in my case it was something else. I found breakthrough analysing communication between browser and server using some test web page. I will explain it in details:
The problem is related to user agent and mime type.
My application was containing in user agent string, among other things, "Mobile/[somenumber] Safari Version/[someversionnumber]"Some servers are very strict what comes in user agent and for mobile Safari-based browsers are expecting some Safari webkit version so the proper notation should be "Mobile/[somenumber] Safari/[someverionnumber] Version/[someversionnumber]"
Notation affects behavior of the server - it sends different mime type (Content-Type). For websites I mentioned in the qestion when user agent is improper "Content-Type" is set by server to "application/xhtml+xml", but when is proper it is "text/html" which is rendered without any problems. Html code sent in both cases is exactly the same.
Anyway, for me it is still hard to say if it is server site fault, which lets sending "application/xhtml+xml" content type to mobile device, or webkit which should handle such code anyway or very strict user agent notation.
It is continuation of open iBooks from my app I did try https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIDocumentInteractionController_class/Reference/Reference.html but it is hard to understand with out code sample.
I have iPhone app, that have Web View.
After starting the app Web View opens custom PDF, and this is working fine.
User wil not read that PDF from my app, so I wont to add button that will open that PDF in iBooks (because iBooks have search and lost of other features…).
I have Toolbar on bottom with button for opening my PDF in iBooks.
And this is my problem, I do not have code for opening my PDF in iBooks.
ViewControler.h
#import <UIKit/UIKit.h>
#interface BG_ViewController : UIViewController
{
IBOutlet UIWebView *myWebView;
}
- (IBAction)openInIBooks:(id)sender;
#end
ViewControler.m
#import "BG_ViewController.h"
#interface BG_ViewController ()
#end
#implementation BG_ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *path = [[NSBundle mainBundle] pathForResource:#"MyBOOK" ofType:#"pdf"];
NSURL *targetURL = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[myWebView loadRequest:request];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)openInIBooks:(id)sender
{
NSLog(#"Clicked\n");
// CODE ???
}
#end
What code need to be added to - (IBAction)openInIBooks:(id)sender so that it will open MyBOOK.pdf ???
Best that I have is:
NSString *stringURL = #"itms-books:";
NSURL *url = [NSURL URLWithString:stringURL];
[[UIApplication sharedApplication] openURL:url];
But this will just open iBooks app, but with out MyBOOK.pdf.
I tried just to add "itms-books:MyBOOK.pdf", but it is not working :-(
You can open iBooks app from your code by calling [NSURL URLWithString:#"ibooks://"]. But If you want to pass document you should have a look on UIDocumentInteractionController this is a way Apple wants you do do that.
I am trying to make an iOS app and I need to add a web view but the issue is I get an error:
Duplicate declaration of method 'viewDidLoad'
- (void)viewDidLoad {
[super viewDidLoad];
NSString *fullURL = #"http://google.com";
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[_viewWeb loadRequest:requestObj];
}
I am testing with google for now and when I develop the web site for the web view I will put the website in.
objective-C does not support method overloading, so you have to use different method names.I am sure you have two viewDidload methods implemented in your .m class so please try to remove one method
i have attached screen shot for your reference try to remove any one viewDidLoad method
I am writing an App where I pass a Link as a NSString from a TableView to my DetailView. The String gets passed correctly, as I can see it in the log. The problem is, that the WebView just stays blank; unless I hard code any URL in. Even the ones that I can see in the log by just copy pasting them. What am I missing? Any help is greatly appreciated. Here's a sample log output and my code. I also checked this thread, where the guy hab the exact same problem as me, but none of the solutions posted there where helpful. UIWebView not loading URL when URL is passed from UITableView
2013-06-17 13:19:52.147 Feuerwehr[1661:c07] http://oliverengelhardt.de/ffw_app/Einsaetze/GB0505/index.html
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *urlString = url;
NSURL *myURL = [NSURL URLWithString:urlString];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:myURL];
[webView loadRequest:requestObj];
NSLog(urlString);
}
I think you stringUrl is nil when it loads (so it doesn't get passed. Break point view did load and see the value of the urlstring
Based on what you've said, your url isn't formatted correctly
i think you are missing delegate
self.webView.delegate = self;