2017-08-15 40 views
0

這裏是我的文件。當我點擊重新加載按鈕時,它會進入我所瞭解的OBJC中的段錯誤。我不認爲這是網站,但主要是沒有得到滿足/返回的東西。它在main.m文件中提供了SIGABRT。我試圖讓殭屍無濟於事,如果這是要走的路,請讓我知道。當我重新加載UIWebView我得到一個信號SIGABRT在我的main.h文件

// 
// main.m 
// webKitExp 
// 
// Created by J.Doe on 8/14/17. 
// Copyright © 2017 J.Doe. All rights reserved. 
// 

#import <UIKit/UIKit.h> 
#import "AppDelegate.h" 

int main(int argc, char * argv[]) { 
    @autoreleasepool { 
    return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); 
    } 
} 

// 
// ViewController.h 
// webKitExp 
// 
// Created by J.Doe 
// Copyright © 2017 J.Doe. All rights reserved. 
// 

#import <UIKit/UIKit.h> 
#import <WebKit/WebKit.h> 

@interface ViewController : UIViewController 

@property (nonatomic, retain) IBOutlet UIWebView *webView; 
@end 

// 
// ViewController.m 
// webKitExp 
// 
// Created by J.Doe on 8/14/17. 
// Copyright © 2017 J.Doe. All rights reserved. 
// 

#import "ViewController.h" 

@interface ViewController() 

@end 

@implementation ViewController 
@synthesize webView; 

- (void)viewDidLoad { 
[super viewDidLoad]; 
[UIView setAnimationsEnabled:NO]; 
NSString *localURL = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"]; 
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:[NSURL fileURLWithPath:localURL]]; 
[webView loadRequest:urlRequest]; 
} 
- (IBAction)buttonClicked:(UIButton *)sender { 
[webView reload]; 
} 


- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
} 


@end 
+0

index.html文件可能未找到。嘗試加載網頁而不是本地文件,看看你是否得到相同的錯誤 –

+0

試試這個呢? [webView loadRequest:urlRequest]; –

+0

我也試過了webView的loadRequest,但這並沒有工作,只要index.html文件,它在同一個文件中,並加載第一次罰款,它只是不重新加載和崩潰,而不是 –

回答

0

由於ios8 +你應該使用WKWebview而不是UIWebview。 @Henly ans在Swift3中,這裏是Objective-C版本。

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    _wkWebview = [[WKWebView alloc] initWithFrame:self.view.frame]; 
    _wkWebview.navigationDelegate = self ; 
    [self.view addSubview:_wkWebview ]; 
    NSURL *url = [NSURL URLWithString:@"https://www.google.com"]; 
    NSURLRequest *request = [NSURLRequest requestWithURL:url]; 
    [_wkWebview loadRequest:request]; 

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 
    [button addTarget:self action:@selector(reload) forControlEvents:UIControlEventTouchUpInside]; 
    [button setTitle:@"Reload" forState:UIControlStateNormal]; 
    button.frame = CGRectMake(50.0, 200.0, 50.0, 50.0); 
    [self.view addSubview:button]; 
} 

-(void)reload{ 
    [self.wkWebview reload]; 
} 

不要忘了進口的WebKit,使其工作#import <WebKit/WebKit.h> 做讓我知道,如果你在評論任何疑問。我會盡力幫助。

+0

奇怪,現在我的按鈕不顯示。我把我的版本取出來,並用程序創建的代碼替換它,現在它根本沒有顯示出來 –

+0

-digured out,它默認爲白色,並沒有出現在它顯示的背景上,但有些東西是關於重新加載 –

0

對於運行iOS 8+的應用程序,您應該使用WKWebview而不是UIWebview。

這是一個在UIViewController中使用WKWebview的例子。點擊重新加載按鈕(使用或使用我們的網絡連接)不會使應用程序崩潰。

import UIKit 
import WebKit 

class WebviewController: UIViewController, WKNavigationDelegate { 

    var wkWebview : WKWebView? 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     wkWebview = WKWebView.init(frame: self.view.bounds) 
     wkWebview?.navigationDelegate = self 
     self.view.addSubview(wkWebview!) 

     let url = URL.init(string:"https://www.google.com") 
     let urlRequest: URLRequest = URLRequest.init(url: url!) 

     wkWebview?.load(urlRequest) 
     // Do any additional setup after loading the view. 

     let button = UIButton.init(frame: CGRect.init(x: 50, y: 200, width: 50, height: 50)) 
     button.setTitle("Reload", for: .normal) 
     button.backgroundColor = UIColor.black 
     self.view.addSubview(button) 
     button.addTarget(self, action: #selector(reload), for: .touchUpInside) 
     button.layer.zPosition = 101 
    } 

    func reload() { 
     self.wkWebview?.reload() 
    } 


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

    public func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) { 
     print("\(error.localizedDescription)") 
    } 

} 

不應該太難將它轉換爲Objective-C。

還有一個WKNavigationDelegate方法

- (void)webView:(WKWebView *)webView 
     didFailProvisionalNavigation:(WKNavigation *)navigation 
     withError:(NSError *)error 

在沒有網絡連接,其被調用。你可能想要實現它。

相關問題