Makapuu
Makapuu

Reputation: 21

How to link UIWebView to website

Really basic question here: I have a web view in my view controller, and can't figure out how to make it display a website. None of the solutions I've been able to find are working for me. Most of what I've seen online tells me to enter the code below (sample url), but I get errors telling me to delete '@' and add several ';' but doing so does not resolve the issues.

- (void)viewDidLoad {
[super viewDidLoad];
NSString *fullURL = @"http://conecode.com";
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[_viewWeb loadRequest:requestObj];

I'm not sure why I'm having so much difficulty with something that seems so simple...here is all I have in my code. How do I make the web view show a website? Thanks in advance! I've already learned a lot from this community.

import UIKit

class SecondViewController: UIViewController {

@IBOutlet var webView: UIWebView!

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

Upvotes: 1

Views: 92

Answers (2)

Kaloyan Dobrev
Kaloyan Dobrev

Reputation: 135

Here is how to load a page to UIWebView.

@IBOutlet var webView: UIWebView!

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        var url = NSURL(string: "https://www.google.com")
        var request = NSURLRequest(URL: url!)
        webView.loadRequest(request)

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

For the problem with http NSAppTransportSecurity key to your info.plist

Here is example:

<key>NSAppTransportSecurity</key>
    <dict>
        <key>NSAllowsArbitraryLoads</key>
        <true/>
        <key>NSExceptionDomains</key>
    <dict>
            <key>google.com</key>
            <dict>
                <key>NSIncludesSubdomains</key>
                <true/>
                <key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
                <true/>
                <key>NSTemporaryExceptionMinimumTLSVersion</key>
                <string>TLSv1.1</string>
            </dict>
        </dict>
</dict>

NSAllowsArbitraryLoads - it allow to connect to any http site.

or you could specifity the site with NSExceptionDomains - like I post in the code above. On more thing - Check WKWebView - it much better than UIWebView for displaying pages. - https://developer.apple.com/library/ios/documentation/WebKit/Reference/WKWebView_Ref/

Upvotes: 0

Makapuu
Makapuu

Reputation: 21

After a bit more digging here's what I've found that seems to work:

    var url = NSURL(string: "https://www.google.com")

    var request = NSURLRequest(URL: url!)

    webView.loadRequest(request)

I'm not sure how this relates to the earlier code snippets I had been finding, and it doesn't work for http (vs https) websites, so that's something I still need to figure out...

Upvotes: 1

Related Questions