0

My UIWebView will not load and I am extremely confused why.The webview was working a few weeks ago. The site that it linked to got updated. But for some reason now any link I send to it doesn't work. I tried clearing the cache. I am a little lost to why this wont appear. Internet on my phone works fine. Everything in the storyboard is connected properly.

The activity indicator keeps spinning. And my alert comes up from the didFailLoadWithError, which is supposed to happen, but it would be nice to figure out why it won't connect.. Any help would be great, thank you.

class CommunityViewController: UIViewController, UIWebViewDelegate {

@IBOutlet weak var activityIndicator: UIActivityIndicatorView!
@IBOutlet weak var communityWeb: UIWebView!
var refreshControl:UIRefreshControl!
let url = "http://www.google.com"


override func viewWillAppear(animated: Bool) {
    NSURLCache.sharedURLCache().removeAllCachedResponses()
    NSURLCache.sharedURLCache().diskCapacity = 0
    NSURLCache.sharedURLCache().memoryCapacity = 0
}
override func viewDidLoad() {
    super.viewDidLoad()

    self.communityWeb.delegate = self


    let requestURL = NSURL(string:url)
    let request = NSURLRequest(URL: requestURL!, cachePolicy: NSURLRequestCachePolicy.ReloadIgnoringLocalAndRemoteCacheData,
        timeoutInterval: 3.0)

    communityWeb.loadRequest(request)

    self.refreshControl = UIRefreshControl()
    self.refreshControl.attributedTitle = NSAttributedString(string: "")
    self.refreshControl.addTarget(self, action: "refresh:", forControlEvents: UIControlEvents.ValueChanged)

    self.communityWeb.scrollView.addSubview(refreshControl)    

}


func refresh(sender:AnyObject)
{
    let requestURL = NSURL(string:url)
    let request = NSURLRequest(URL: requestURL!)
    communityWeb.loadRequest(request)
    refreshControl.endRefreshing()
}

func webViewDidStartLoad(webView: UIWebView) // here show your indicator
{
    self.activityIndicator.startAnimating()
}

func webViewDidFinishLoad(webView: UIWebView) {

    self.activityIndicator.stopAnimating()
    self.activityIndicator.hidesWhenStopped = true
    self.communityWeb.scrollView.contentSize.width = self.communityWeb.frame.size.width

}

func webView(webView: UIWebView, didFailLoadWithError error: NSError?) {

        let alertView = SIAlertView(title: "Internet Connection", andMessage: "Connect to the internet to receive latest updates from the Community")
        alertView.addButtonWithTitle("OK", type: SIAlertViewButtonType.Default, handler:
            {alertView in
                NSLog("pressed")
        })
        alertView.transitionStyle = SIAlertViewTransitionStyle.Fade
        alertView.show()

}
MangoCode
  • 28
  • 7

1 Answers1

3

I think you forgot to update your info.plist file.

Add this key into your file:

The lazy option is:

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>

Or you can directly add that into your info.plist and it will look like:

enter image description here

And you can add a specific domain like:

<key>NSAppTransportSecurity</key>
<dict>
  <key>NSExceptionDomains</key>
  <dict>
    <key>yourserver.com</key>
    <dict>
      <!--Include to allow subdomains-->
      <key>NSIncludesSubdomains</key>
      <true/>
      <!--Include to allow HTTP requests-->
      <key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
      <true/>
      <!--Include to specify minimum TLS version-->
      <key>NSTemporaryExceptionMinimumTLSVersion</key>
      <string>TLSv1.1</string>
    </dict>
  </dict>
</dict>
Dharmesh Kheni
  • 71,228
  • 33
  • 160
  • 165
  • will apple except an that has NSAllowsArbitraryLoads inside of the PList? Reading on the forum I am getting mixed answers. – MangoCode Oct 17 '15 at 19:55