xcode 在 iOS 应用程序中加载网页
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12737237/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Loading webpage inside iOS application
提问by Baig
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.
如果我们加载一个网页,我们可以将其转发到 safari,但这会导致用户离开我们的应用程序。有什么方法可以让用户访问任何网页,然后返回到我们的应用程序。
回答by Baig
If you want some browser type functionality for devices earlier then iOS7, you can use this inline browser
如果你想要一些早于 iOS7 的设备的浏览器类型功能,你可以使用这个内嵌浏览器
iOS 9 Update:Apple has introduced a visible standard interface for browsing the web i.e. SFSafariViewControllerfor iOS 9+ devices.
iOS 9 更新:Apple 引入了用于浏览网络的可见标准界面,即 适用于 iOS 9+ 设备的SFSafariViewController。
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)
}
}
回答by rid
回答by danner.tech
I wanted to add a more detailed answer for others in the future (key coding!) :
我想在未来为其他人添加更详细的答案(关键编码!):
In the object library, search for WebKit View and add it to your ViewController
Place "import WebKit" under "import UIKit"
Create an outlet from the WebKit View you placed in your ViewController in your code directly under the opening "{"
Under "superViewDidLoad()", all you need is this: let url = URL(string: "your_url_here"")
在对象库中,搜索 WebKit View 并将其添加到您的 ViewController
将“import WebKit”放在“import UIKit”下
从您放置在您的代码中的 ViewController 中的 WebKit 视图直接在开头的“{”下创建一个插座
在“superViewDidLoad()”下,你只需要: let url = URL(string: "your_url_here"")
and
和
webview.load(URLRequest(url:url!))
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.
}
}
回答by Jarvis The Avenger
@user1706456 UIWebView is deprecated by Apple, You can use WKWebView for it.
@user1706456 UIWebView 已被 Apple 弃用,您可以使用 WKWebView。
Here's code to do that :
这是执行此操作的代码:
step1: Import WebKit
step1:导入WebKit
Step2:
第2步:
//ViewController's LifeCycle.
override func loadView() {
let webConfiguration = WKWebViewConfiguration()
webView = WKWebView(frame: .zero, configuration: webConfiguration)
webView.uiDelegate = self
webView.navigationDelegate = self
view = webView
}
Step3 :
步骤3:
In ViewDidLoad:
在 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.
第 4 步:WKWebView 委托方法来处理导航和加载等。
//MARK: - WKNavigationDelegate
//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) {
}
}
回答by Coder404
Use a UIWebView Here's how to program one. Let me know if you need more help or examples
使用 UIWebView下面是如何编程一个. 如果您需要更多帮助或示例,请告诉我