swift2 初学一问,UIWebView 无法使用

陳尹
  • 348

书籍是1.X多,里面有说到 UIWebView

@IBAction func buttonClick(sender: UIButton) {
    var str=textURL.text
    if str!.hasPrefix("http://"){
        str="http://" + str!
    }
    var url=NSURL(string: str!)
    var request=NSURLRequest(URL: url!)
    webView!.loadRequest(request)
    
}
override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    var url=NSURL(string:"https://www.google.com.tw")
    print(url)
    var request=NSURLRequest(URL:url!)
    
    webView!.loadRequest(request)
    
    // Dispose of any resources that can be recreated.
}

上网查了一下,好像是安全性的问题,说在 info.plist 加上

<key>NSAppTransportSecurity</key>  
 <dict>  
      <key>NSAllowsArbitraryLoads</key><true/>  
 </dict>  

但我加了也没用,请问这是怎么回事 求大大解

回复
阅读 2.9k
3 个回答
✓ 已被采纳

终于解决了 ,感谢大大

方法移到viewdidload中就好了。didreceivememorywarning是低内存警告时调用。用于清理。

等等等等等等
  • 1
新手上路,请多包涵
  override func viewDidLoad() {       
        super.viewDidLoad()
          let myWebView:UIWebView = UIWebView(frame: CGRectMake(0, 0, UIScreen.mainScreen().bounds.width, UIScreen.mainScreen().bounds.height))
        myWebView.loadRequest(NSURLRequest(URL: NSURL(string: "http://www.sourcefreeze.com")!))
        self.view.addSubview(myWebView)
    }
    
    http://sourcefreeze.com/uiwebview-example-using-swift-in-ios/
宣传栏