简体   繁体   English

NSInvalidArgumentException',原因:从 UIWebView 迁移到 WKWebView 时,'Configuration cannot be nil'

[英]NSInvalidArgumentException', reason: 'Configuration cannot be nil' while migrating from UIWebView to WKWebView

I can't seem to find out what is nil.我似乎无法找出什么是零。 The controller calling this WKWebView is not nil.调用这个WKWebView的controller不为nil。 There must be some type of configuration that I'm not setting.一定有某种我没有设置的配置类型。 Any Ideas?有任何想法吗?

HelpViewViewController.h HelpViewViewController.h



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

@interface HelpViewViewController : UIViewController
< WKUIDelegate, WKNavigationDelegate>{}



@property (nonatomic, strong) IBOutlet WKWebView *webView;
@property (weak, nonatomic) IBOutlet UIBarButtonItem *sidebarButton;

@property (assign) NSString *urlString;

@end

HelpViewViewController.m HelpViewViewController.m

#import "HelpViewViewController.h"
#import "SWRevealViewController.h"
#import "Functions.h"

@interface HelpViewViewController ()

@end

@implementation HelpViewViewController

@synthesize webView,urlString;

NSURL *urlToSave2 = nil;


- (void)viewDidLoad
{
    [super viewDidLoad];
    NSLog(@" start %@",urlString);

This NSLog never gets called.这个 NSLog 永远不会被调用。 It is crashing before viewDidLoad is called.在调用 viewDidLoad 之前它正在崩溃。


    WKWebViewConfiguration *theConfiguration = [[WKWebViewConfiguration alloc] init];
    WKWebView *webView = [[WKWebView alloc] initWithFrame:self.view.frame configuration:theConfiguration];
    webView.navigationDelegate = self;
    NSURL *nsurl=[NSURL URLWithString:@"http://www.apple.com"];
    NSURLRequest *nsrequest=[NSURLRequest requestWithURL:nsurl];
    [webView loadRequest:nsrequest];
    [self.view addSubview:webView];

    
    NSLog(@" end %@",urlString);
}

I couldn't reproduce the error on simulator by adding a WKWebView IBOutlet.我无法通过添加WKWebView IBOutlet 在模拟器上重现错误。

BUT , I did reproduce it if I put a UIWebView object, and change its class in Storyboard to WKWebView .但是,如果我放入UIWebView object,并将其 Storyboard 中的 class 更改为WKWebView ,我确实重现了它。
If that's what you've done to "update your code", please don't do that.如果那是您为“更新代码”所做的,请不要那样做。
Instead, remove the outlet in Storyboard, and add a new one which is a WKWebView .相反,删除 Storyboard 中的插座,并添加一个新的WKWebView WKWebView & UIWebView are two different classes even if their "goal" is the same. WKWebViewUIWebView是两个不同的类,即使它们的“目标”是相同的。 But they are different, and need different initialization/parameters, and when reading it from Storyboard, when the loading occurs, it breaks everything in that case:但它们是不同的,需要不同的初始化/参数,当从 Storyboard 读取它时,当加载发生时,在这种情况下它会破坏一切:

If you open the Storyboard (as source code, ie XML) of a newly added WKWebView outlet, you'll have:如果您打开新添加的 WKWebView 插座的 Storyboard(作为源代码,即 XML),您将拥有:

<wkWebView contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="R9K-yX-E6A">
    <rect key="frame" x="0.0" y="0.0" width="320" height="284"/>
    <color key="backgroundColor" red="0.36078431370000003" green="0.38823529410000002" blue="0.4039215686" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
    <wkWebViewConfiguration key="configuration">
        <audiovisualMediaTypes key="mediaTypesRequiringUserActionForPlayback" none="YES"/>
        <wkPreferences key="preferences"/>
    </wkWebViewConfiguration>
</wkWebView>

If you change the class manually of a UIWebView into a WKWebView , you'll have:如果您手动将 class 的UIWebView更改为WKWebView ,您将拥有:

<webView contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="JoT-Lq-Nq2" customClass="WKWebView">
    <rect key="frame" x="40" y="341" width="240" height="128"/>
    <color key="backgroundColor" red="0.36078431370000003" green="0.38823529410000002" blue="0.4039215686" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
</webView>

You see that the real class if webView (ie UIWebView ), set to custom WKWebView .你看真正的 class if webView (即UIWebView ),设置为自定义WKWebView BUT , and there is a BUT, if you check carefully the proper one (previous one), you'll see it as a wkWebViewConfiguration tag. BUT ,并且有一个 BUT,如果您仔细检查正确的那个(前一个),您会看到它是一个wkWebViewConfiguration标签。 It's set in the Storyboard. It's implicitly added when adding the outlet.设置在Storyboard。添加outlet时隐式添加。

Although Apple documentation says that you can create a WKWebView as an outlet, I have not seen an example of that working.尽管 Apple 文档说您可以创建一个 WKWebView 作为 outlet,但我还没有看到这种工作的示例。 I suggest removing it from your xib/storyboard, making it a regular instance variable, and using your in-code initialization.我建议将它从你的 xib/storyboard 中移除,使它成为一个常规实例变量,并使用你的代码内初始化。

(In my case, this also required the addition of constraints when it was added as a subview.) (在我的例子中,这也需要在将其添加为子视图时添加约束。)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从 UIWebView 迁移到 WKWebView - Migrating from UIWebView to WKWebView 将 iOS 混合应用程序从 UIWebView 迁移到 WKWebview - Migrating iOS Hybrid App from UIWebView to WKWebview &#39;NSInvalidArgumentException&#39;,原因:&#39;*** setObjectForKey:对象不能为零(键:device_uid) - 'NSInvalidArgumentException', reason: '*** setObjectForKey: object cannot be nil (key: device_uid)' “ NSInvalidArgumentException”,原因:“ ***-[__ NSArrayM insertObject:atIndex:]:对象不能为零” - 'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil' UICollectionView insertSections 错误 - NSInvalidArgumentException 原因 setObjectForKey 对象不能为零 UICollectionViewItemKey - UICollectionView insertSections error - NSInvalidArgumentException reason setObjectForKey object cannot be nil UICollectionViewItemKey NSInvalidArgumentException,原因:“数据参数为nil” - NSInvalidArgumentException, reason: 'Data parameter is nil' 'NSInvalidArgumentException',原因:'data parameter is nil' - 'NSInvalidArgumentException', reason: 'data parameter is nil' &#39;NSInvalidArgumentException&#39;,原因:&#39;***-[NSURL initFileURLWithPath:]:无字符串参数 - 'NSInvalidArgumentException', reason: '*** -[NSURL initFileURLWithPath:]: nil string parameter 未捕获的异常&#39;NSInvalidArgumentException&#39;问题,原因:&#39;***-[__ NSArrayM insertObject:atIndex:]:对象不能为nil - Issues with an uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil 由于未捕获的异常“ NSInvalidArgumentException”而终止应用程序,原因:“ *** setObjectForKey:键不能为零” - Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** setObjectForKey: key cannot be nil'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM