3

I'm using UIWebViews in an iOS 7 app to display content. Unfortunally it happens that users have enabled the system wide restrictions for websites that were introduced with iOS 7. Although I have nothing of the category 'adult' in my webview content the filter seems to dislike what I want to display in some cases.

So I searched for a way to detect if the system wide restrictions for websites are enabled by the user (or a settings profile that is installed on the device). I'd like to at least tell the user why some elements of the view behave not as expected and give the advice to allow 'localhost' to fully use my app and still have the restrictions enabled.

Sadly I could not find a way to detect if those restrictions are enabled. I could of course observe the content of every UIWebView and react in case the content is blocked... this just seems a bit of an overhead to do with every webview all the time and I would like to limit that to those users that really have the restrictions enabled.

This page describes how to setup website restrictions and how they appear to the user, there is also an image how a site in Safari then appears. The same I get in my apps webview: http://www.ifans.com/forums/threads/inside-ios-7-how-to-block-websites.399846/

So any hint how I could detect enabled website restrictions would be nice. Thank you!

Funkybit
  • 359
  • 1
  • 2
  • 15
  • What kind of restrictions are you seeing exactly? – Andreas Ley Nov 08 '13 at 14:50
  • Good question, I added a reference to instructions how one can setup website restrictions on iOS 7. There you also find images how they appear to the user when a site is blocked. – Funkybit Nov 08 '13 at 15:01

1 Answers1

2

When trying to load a restricted page, the UIWebView will replace its content with a placeholder page. If you look at the source of that page, there's the following snippet:

<!-- NO LOC: Applications (such as Dictionary.app) can recognize that a webpage has been restricted by looking for the following comment:
-->
<!-- com.apple.webcontentfilter.accessrestricted -->

So you can do this:

NSString *htmlSourceCode = [webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.outerHTML"];
BOOL accessIsRestricted = ([htmlSourceCode rangeOfString:@"<!-- com.apple.webcontentfilter.accessrestricted -->"].location != NSNotFound);

Additionally, the UIWebView in iOS 7.0.3 doesn't seem to restrict content loaded by loadHTMLString:baseURL:, so you probably could use that to work around the restrictions.

Finally, I'm not sure why you're using localhost. If you want to load local files, you can just request a NSURL that is pointing to a file (by using NSURL's fileURLWithPath:).

Andreas Ley
  • 9,109
  • 1
  • 47
  • 57
  • You are right, my case is just not that simple. In the mean time I found the comment in the source code. As stated in my question I think it's a lot of overhead if I ask this on every webview all the time. I just wanted to find a way to learn in front of doing that if the restrictions are set at all and if querying for the source code is neccessary. And then there is the problem that I really have a local webservice running on my device that I ask for files via localhost/127.0.0.1. The other methods to load content are not sufficient enough and I had to go the hard way with a local server. – Funkybit Nov 08 '13 at 17:47
  • 1
    @Funkybit You could just load a known restricted page once in an invisible web view to run the check; no need to do this for every webview/load. Also, I can't imagine any scenario that requires a local webserver, because directly setting a `UIWebView`'s HTML has the same effect. Care to enlighten me? :) – Andreas Ley Nov 08 '13 at 18:03
  • Well, I have a system which loads a JavaScript library that then loads further content, from local or remote sources, asyncronously into some html containers. Since the same library is hybridly used on iOS, Android, ... the goal was to have one code for all (no the app itself is not really a hybrid or PhoneGap app, but this part should be hybrid (customer wishes)). And since JavaScript loads content the way it does and files, encrypted on disk, need to be decrypted somewhere on the way from disk to local delivery... the methods you mentioned could not do the job. – Funkybit Nov 08 '13 at 18:21
  • I hoped for maybe an information dictionary about system restrictions on UIDevice or something like that. But well, your idea to check once with something really bad to get the information will be the alternative. A +1 on your original answer anyway for pointing to the alternatives :) – Funkybit Nov 08 '13 at 18:24