-
Notifications
You must be signed in to change notification settings - Fork 472
Using WebKit View
Chi Hwa Michael Ting edited this page Jun 4, 2018
·
8 revisions
Loading a URL into your app is as easy! Just add and configure a WebKit View, which replaces UIWebView starting iOS 8.
Add WebKit to your project's "Linked Frameworks and Libraries"
Drag and drop a WebKit View from the Object Library to the main view of your ViewController. Drag the edges of the view to adjust it's size.
Choose: File -> New -> File...-> iOS -> Cocoa Touch Class
Create an outlet for the WebView by ctrl + dragging from the WebView in the Storyboard to your custom ViewController swift file.
//**Above** the `viewDidLoad` method, create a constant to hold your URL string. We will link to the DropBox mobile terms url.
let url = "https://www.dropbox.com/terms?mobile=1"
// As a property or local variable
NSString *urlString = @"https://www.dropbox.com/terms?mobile=1";
Within the viewDidLoad
method...
// Convert the url String to a NSURL object.
let requestURL = URL(string:url)
// Place the URL in a URL Request.
let request = URLRequest(url: requestURL!)
webkitView.load(request)
// Convert the url String to a NSURL object.
NSURL *url = [NSURL URLWithString:urlString];
// Place the URL in a URL Request.
NSURLRequest *request = [NSURLRequest requestWithURL:url
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval:10.0];
// Load Request into WebView.
[self.webkitView loadRequest:request];