当我重新加载 UIWebView 时,我在 main.h 文件中收到 Signal SIGABRT

When I reload UIWebView I get a Signal SIGABRT in my main.h file

这是我的文件。当我单击重新加载按钮时,它进入我所理解的 OBJC 中的段错误。我不认为它是网站,而是主要的东西不是 met/returned。它在 main.m 文件中给出了 SIGABRT。我试图启用僵尸无济于事,如果这是可行的方法,请告诉我。

//
//  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

您真的应该为应用程序使用 WKWebview 而不是 UIWebview 运行 iOS 8+。

这是在 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

在无法访问互联网时调用。您可能想要实现它。

因为 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> 如果您有任何疑问,请在评论中告诉我。我会尽力帮忙的。