2017-08-15 42 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 

在没有网络连接,其被调用。你可能想要实现它。

相关问题