ios Xcode 9 GM - WKWebView NSCoding 支持在以前的版本中被破坏
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46221577/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Xcode 9 GM - WKWebView NSCoding support was broken in previous versions
提问by Mehdi Chennoufi
Does anyone know how to fix this error with Xcode 9 GM? I'm working on an app made with Xcode 8.3, the deployment target is for iOS 9.3 and I never had this problem before. I don't find any information here or on Apple forums yet :(
有谁知道如何使用 Xcode 9 GM 修复此错误?我正在开发一个用 Xcode 8.3 制作的应用程序,部署目标是针对 iOS 9.3 的,我以前从未遇到过这个问题。我还没有在这里或在 Apple 论坛上找到任何信息:(
Edit: This error came when I put a WKWebView into interface builder, not if I use it programmatically.
编辑:当我将 WKWebView 放入界面构建器时出现此错误,而不是如果我以编程方式使用它。
Edit 2: Well, it's finally not a bug, see Quinn's answer below to have more information about this behavior. Thanks to him for the explanation.
编辑 2:嗯,这终于不是错误了,请参阅下面的 Quinn 回答以了解有关此行为的更多信息。谢谢他的解释。
回答by Quinn Taylor
The error is correct behavior, and not a bug in Xcode 9. Although WKWebView was introduced in iOS 8, there was a bug in -[WKWebView initWithCoder:]
that was only fixed in iOS 11, which always crashed at runtime and thus prevented configuring one within Interface Builder.
错误是正确的行为,而不是 Xcode 9 中的错误。虽然 WKWebView 是在 iOS 8 中引入的,但有一个错误-[WKWebView initWithCoder:]
只在 iOS 11 中修复,它总是在运行时崩溃,从而阻止在 Interface Builder 中配置一个。
https://bugs.webkit.org/show_bug.cgi?id=137160
https://bugs.webkit.org/show_bug.cgi?id=137160
Rather than allow developers to build something in IB that would be broken at runtime, it is a build error. It's an inconvenient limitation since iOS 11 was only recently released, but there's really no other good option.
它不是允许开发人员在 IB 中构建在运行时会被破坏的东西,而是一个构建错误。这是一个不方便的限制,因为 iOS 11 最近才发布,但确实没有其他好的选择。
The workaround for older deployment targets is to continue to add the WKWebView in code, as @fahad-ashraf already described in his answer:
旧部署目标的解决方法是继续在代码中添加 WKWebView,正如@fahad-ashraf 在他的回答中已经描述的那样:
回答by Fahad Ashraf
This seems to be a bug in Xcode 9 and was also present in the betas. You will only get the build error if you are creating a WKWebView through the storyboard. If you progmatically create the WKWebView in the corresponding ViewController class file, you should be able to build on iOS versions below iOS 11. Here is the approach given on Apple's website for how to accomplish this:
这似乎是 Xcode 9 中的一个错误,并且在测试版中也存在。如果您通过故事板创建 WKWebView,您只会收到构建错误。如果您在相应的 ViewController 类文件中以编程方式创建 WKWebView,您应该能够在 iOS 11 以下的 iOS 版本上进行构建。 以下是 Apple 网站上提供的如何完成此操作的方法:
import UIKit
import WebKit
class ViewController: UIViewController, WKUIDelegate {
var webView: WKWebView!
override func loadView() {
super.loadView()
let webConfiguration = WKWebViewConfiguration()
webView = WKWebView(frame: .zero, configuration: webConfiguration)
webView.uiDelegate = self
view = webView
}
override func viewDidLoad() {
super.viewDidLoad()
let myURL = URL(string: "https://www.apple.com")
let myRequest = URLRequest(url: myURL!)
webView.load(myRequest)
}}
You should then be able to implement WKWebView functionality as you normally would.
然后,您应该能够像往常一样实现 WKWebView 功能。
Source: https://developer.apple.com/documentation/webkit/wkwebview
来源:https: //developer.apple.com/documentation/webkit/wkwebview
回答by Alessandro Ornano
If you want to realize a custom UIViewController
with other components in addition you can make a "container" through the storyboard called for example webViewContainer
:
如果您还想实现与其他组件的自定义UIViewController
,您可以通过故事板制作一个“容器”,例如调用webViewContainer
:
import UIKit
import WebKit
class ViewController: UIViewController, WKUIDelegate {
@IBOutlet weak var webViewContainer: UIView!
var webView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
let webConfiguration = WKWebViewConfiguration()
let customFrame = CGRect.init(origin: CGPoint.zero, size: CGSize.init(width: 0.0, height: self.webViewContainer.frame.size.height))
self.webView = WKWebView (frame: customFrame , configuration: webConfiguration)
webView.translatesAutoresizingMaskIntoConstraints = false
self.webViewContainer.addSubview(webView)
webView.topAnchor.constraint(equalTo: webViewContainer.topAnchor).isActive = true
webView.rightAnchor.constraint(equalTo: webViewContainer.rightAnchor).isActive = true
webView.leftAnchor.constraint(equalTo: webViewContainer.leftAnchor).isActive = true
webView.bottomAnchor.constraint(equalTo: webViewContainer.bottomAnchor).isActive = true
webView.heightAnchor.constraint(equalTo: webViewContainer.heightAnchor).isActive = true
webView.uiDelegate = self
let myURL = URL(string: "https://www.apple.com")
let myRequest = URLRequest(url: myURL!)
webView.load(myRequest)
}
回答by Kampai
If you are moved from older target to iOS 11.0 and still you are getting this error, then use below solution.
如果您从较旧的目标移动到 iOS 11.0 并且仍然收到此错误,请使用以下解决方案。
- Go to Storyboard (Main.storyboard), click on any Scene.
- Select 'File Inspector' which is the right side property window of Xcode
- Change 'Builds for' value to 'iOS 11.0 and Later'
- Compile and Build
- 转到 Storyboard (Main.storyboard),单击任何场景。
- 选择“文件检查器”,这是 Xcode 的右侧属性窗口
- 将“ Builds for”值更改为“ iOS 11.0 及更高版本”
- 编译和构建
回答by Najam
I have faced the same issue but it can be tackle if we add WKWebViewprogrammatically.
我遇到了同样的问题,但如果我们以编程方式添加WKWebView可以解决它。
Do whatever design you want to make in storyboard but leave the room for WKWebView, in that area drag & drop a view and name it as webViewContainerand declare these two properties,
@property (weak, nonatomic) IBOutlet UIView *webViewContainer; @property(nonatomic, strong)WKWebView *webView;
Now create and add webView like this:
-(instancetype)initWithCoder:(NSCoder *)aDecoder { self.webView = [self createWebView]; self = [super initWithCoder:aDecoder]; return self; }
createWebView function is declared as,
-(WKWebView *)createWebView { WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init]; return [[WKWebView alloc] initWithFrame:CGRectZero configuration:configuration]; }
Now add this newly created webView to your containerView, like this, :
-(void)addWebView:(UIView *)view { [view addSubview:self.webView]; [self.webView setTranslatesAutoresizingMaskIntoConstraints:false]; self.webView.frame = view.frame; }
Finally, just load your Url like this,
-(void)webViewLoadUrl:(NSString *)stringUrl { NSURL *url = [NSURL URLWithString:stringUrl]; NSURLRequest *request = [NSURLRequest requestWithURL:url]; [self.webView loadRequest:request]; }
做任何你想在故事板中进行的设计,但为WKWebView留出空间,在该区域拖放一个视图并将其命名为webViewContainer并声明这两个属性,
@property (weak, nonatomic) IBOutlet UIView *webViewContainer; @property(nonatomic, strong)WKWebView *webView;
现在像这样创建和添加 webView:
-(instancetype)initWithCoder:(NSCoder *)aDecoder { self.webView = [self createWebView]; self = [super initWithCoder:aDecoder]; return self; }
createWebView 函数声明为,
-(WKWebView *)createWebView { WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init]; return [[WKWebView alloc] initWithFrame:CGRectZero configuration:configuration]; }
现在将这个新创建的 webView 添加到您的 containerView,如下所示:
-(void)addWebView:(UIView *)view { [view addSubview:self.webView]; [self.webView setTranslatesAutoresizingMaskIntoConstraints:false]; self.webView.frame = view.frame; }
最后,只需像这样加载您的网址,
-(void)webViewLoadUrl:(NSString *)stringUrl { NSURL *url = [NSURL URLWithString:stringUrl]; NSURLRequest *request = [NSURLRequest requestWithURL:url]; [self.webView loadRequest:request]; }
Thanks for reading this.
感谢您阅读本文。
回答by Mr.Javed Multani
WebKit was introduced in iOS 8but it was released with an error which caused in a runtime crash, If you are using Xcode 9/10, your project configuration support less than iOS 11and if you are trying to add WKWebView using interface builder. Xcode immediately shows a compile-time error.
WebKit 是在iOS 8中引入的,但它发布时出现错误,导致运行时崩溃,如果您使用的是Xcode 9/10,您的项目配置支持低于iOS 11,并且您尝试使用界面构建器添加 WKWebView。Xcode 立即显示编译时错误。
WKWebView before iOS 11.0 (NSCoding Support was broken in the previous version)
iOS 11.0 之前的 WKWebView(NSCoding Support 在之前的版本中被破坏了)
Although WKWebView was introduced in iOS 8, there was a bug in –[WKWebView initWithCoder:] that was only fixed in iOS 11.
尽管 WKWebView 是在iOS 8中引入的,但在 –[WKWebView initWithCoder:] 中存在一个错误,该错误仅在iOS 11 中修复。
Resolution is you must add WKWebView through code (Only if you are supporting below iOS 11). That actually sounds strange.
解决方案是您必须通过代码添加 WKWebView(仅当您支持低于 iOS 11 时)。这实际上听起来很奇怪。
Another solutionis to change the Interface Builder Document Builds for the option to iOS 11 and later (If you're migrating from older target to iOS 11 and still getting the same error).
另一种解决方案是将 Interface Builder Document Builds 的选项更改为 iOS 11 及更高版本(如果您从旧目标迁移到 iOS 11 并且仍然遇到相同的错误)。
回答by ThomasB
This should be a comment on Kampai's answer, but I don't have enough reputation points to comment.
这应该是对 Kampai 回答的评论,但我没有足够的声望点来评论。
Besides explicit iOS versions the 'Builds for' dropdown in the 'File Inspector' also contains the Deployment Target (..)option. I prefer managing versions at a single place if possible and wanted to change the build target anyway. I was fully confused it still said Deployment Target (9.3)although I changed the corresponding entry in the general settings to 11.0.
除了显式的 iOS 版本,“文件检查器”中的“构建对象”下拉菜单还包含部署目标 (..)选项。如果可能的话,我更喜欢在一个地方管理版本,并且无论如何都想更改构建目标。我很困惑它仍然说Deployment Target (9.3)尽管我将常规设置中的相应条目更改为11.0。
Restartof XCode (11.3.1) was necessary to make everything work as expected, the option 'Deployment Target (11.0)' was available (and 9.3 was gone). After selection everything worked as expeected.
必须重新启动XCode (11.3.1) 才能使一切按预期工作,选项“部署目标 (11.0)”可用(9.3 已消失)。选择后,一切都按预期进行。
回答by pawel221
UIWebView is deprecated in iOS11 and WKWebView works only in iOS11 - this sounds like a bug in Xcode GM.
UIWebView 在 iOS11 中已被弃用,WKWebView 仅在 iOS11 中有效 - 这听起来像是 Xcode GM 中的错误。