user1706456
user1706456

Reputation:

Loading webpage inside iOS application

If we load a webpage, we can forward it to safari, but this causes users to leave our app. Is there any way so that a user visits any webpage and then come back to our application.

Upvotes: 3

Views: 9803

Answers (5)

Baig
Baig

Reputation: 4995

If you want some browser type functionality for devices earlier then iOS7, you can use this inline browser

iOS 9 Update: Apple has introduced a visible standard interface for browsing the web i.e. SFSafariViewController for iOS 9+ devices.

Example:

func showTutorial() {
    if let url = URL(string: "https://www.example.com") {
        let config = SFSafariViewController.Configuration()
        config.entersReaderIfAvailable = true

        let vc = SFSafariViewController(url: url, configuration: config)
        present(vc, animated: true)
    }
}

Upvotes: 4

Rahul Umap
Rahul Umap

Reputation: 2859

@user1706456 UIWebView is deprecated by Apple, You can use WKWebView for it.

Here's code to do that :

step1: Import WebKit

Step2:

 //ViewController's LifeCycle.
override func loadView() {
    let webConfiguration = WKWebViewConfiguration()
    webView = WKWebView(frame: .zero, configuration: webConfiguration)
    webView.uiDelegate = self
    webView.navigationDelegate = self
    view = webView
}

Step3 :

In ViewDidLoad:

   let myURL = URL(string: "www.google.com")
   let myRequest = URLRequest(url: myURL!)
   webView.load(myRequest)

Step 4: WKWebView Delegate Methods to handle navigation and loading etc.

//MARK: - WKNavigationDelegate

    extension GPWebViewController : WKNavigationDelegate {
  func webView(_ webView: WKWebView, didCommit navigation: WKNavigation!) {

}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
    // Refreshing the content in case of editing...
}



func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
}
 } 

Upvotes: 0

danner.tech
danner.tech

Reputation: 109

I wanted to add a more detailed answer for others in the future (key coding!) :

  1. In the object library, search for WebKit View and add it to your ViewController

  2. Place "import WebKit" under "import UIKit"

  3. Create an outlet from the WebKit View you placed in your ViewController in your code directly under the opening "{"

  4. Under "superViewDidLoad()", all you need is this: let url = URL(string: "your_url_here"")

and

webview.load(URLRequest(url:url!))

and dassit!

I'll attach a copy of my code just in case I didn't explain it very well:

import UIKit
import WebKit

class WebsiteViewController: UIViewController {

    @IBOutlet weak var webview: WKWebView!
    override func viewDidLoad() {
        super.viewDidLoad()

        let url = URL(string: "https://www.eventbrite.com/e/pretty-in-petals-tea-party-tickets-43361370025")
        webview.load(URLRequest(url: url!))
    }

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




}

Upvotes: 1

Coder404
Coder404

Reputation: 742

Use a UIWebView Here's how to program one. Let me know if you need more help or examples

Upvotes: 0

rid
rid

Reputation: 63442

You can use a UIWebView to display the page in your application.

Upvotes: 1

Related Questions