尝试使用UIWebView加载10个网页后应用崩溃

时间:2016-10-27 02:23:28

标签: ios swift uiwebview

我正在制作一个可以订购食物的应用程序。我在paypal的UIWebView页面上结帐了。问题是他们一次加载9或10个不同的网页。我想知道他们是否只是在需要时加载它。

import UIKit

class ViewController: UIViewController {
    @IBOutlet weak var webView: UIWebView!
    @IBOutlet weak var webView1: UIWebView!
    @IBOutlet weak var webView2: UIWebView!
    @IBOutlet weak var webView3: UIWebView!
    @IBOutlet weak var webView4: UIWebView!
    @IBOutlet weak var webView5: UIWebView!
    @IBOutlet weak var webView6: UIWebView!
    @IBOutlet weak var webView7: UIWebView!
    @IBOutlet weak var webView8: UIWebView!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        let url = NSURL(string: "https://mms-lunch-order.000webhostapp.com/12count/pay.html")
        let request = NSURLRequest(URL: url!)
        webView.loadRequest(request)

        let url1 = NSURL(string: "https://mms-lunch-order.000webhostapp.com/spicychicken6th/pay2.html")
        let request1 = NSURLRequest(URL: url1!)
        webView1.loadRequest(request1)

        let url2 = NSURL(string: "https://mms-lunch-order.000webhostapp.com/chickensandwhich6th/pay3.html")
        let request2 = NSURLRequest(URL: url2!)
        webView2.loadRequest(request2)

        let url3 = NSURL(string: "https://mms-lunch-order.000webhostapp.com/12count7/pay4.html")
        let request3 = NSURLRequest(URL: url3!)
        webView3.loadRequest(request3)

        let url4 = NSURL(string: "https://mms-lunch-order.000webhostapp.com/spicychicken7/pay9.html")
        let request4 = NSURLRequest(URL: url4!)
        webView4.loadRequest(request4)

        let url5 = NSURL(string: "https://mms-lunch-order.000webhostapp.com/chickensandwhich7/pay7.html")
        let request5 = NSURLRequest(URL: url5!)
        webView5.loadRequest(request5)

        let url6 = NSURL(string: "https://mms-lunch-order.000webhostapp.com/12count8/pay5.html")
        let request6 = NSURLRequest(URL: url6!)
        webView6.loadRequest(request6)

        let url7 = NSURL(string: "https://mms-lunch-order.000webhostapp.com/spicychicken8/pay10.html")
        let request7 = NSURLRequest(URL: url7!)
        webView7.loadRequest(request7)

        let url8 = NSURL(string: "https://mms-lunch-order.000webhostapp.com/chickensandwhich8/pay8.html")
        let request8 = NSURLRequest(URL: url8!)
        webView8.loadRequest(request8)
    }

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

2 个答案:

答案 0 :(得分:0)

这是评论,因为我没有足够的声誉。

我想可能是@IBOutlet没有正确连接,请你检查一下

答案 1 :(得分:0)

我假设您正在从上一页导航到此Web视图,并且您只打算检查您选择的项目。如果不是这样,请告诉我,我会调整这个答案。

在这种情况下,删除除一个webview之外的所有内容。我会在这个名为foodUrlString的viewcontroller上设置一个变量。

class ViewController: UIViewController {
@IBOutlet weak var webView: UIWebView!

var foodUrlString = String()

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    if let url = NSURL(string: foodUrlString) {
      let request = NSURLRequest(URL: url)
      webView.loadRequest(request)
    } else {
    //if URL does not work for some reason, do something here like show error loading view or something 
    }
}

当您即将导航到此viewcontroller且您知道要购买的内容时,请为该foodUrlString属性设置URL字符串。

例如,如果您要呈现此viewcontroller,请在从前一个viewcontroller呈现它之前进行设置

viewController.foodUrlString = "http://food.com"

只要您的网络视图为零,只留下一个网络视图。在故事板中,控件单击层次结构中最顶层的视图并删除所有未链接的插座。