Saqib2235
Saqib2235

Reputation: 51

WKUserScript init Crash on iOS 14: Use of unimplemented initializer init(source:injectionTime:forMainFrameOnly:in:)

Crash while executing this init method in ios 14

Use of unimplemented initializer init(source:injectionTime:forMainFrameOnly:in:)

class MyScript: WKUserScript {

init(source: String) {
    super.init(source: source,
               injectionTime: .atDocumentEnd,
               forMainFrameOnly: true)
}

}

let source = "var style = document.createElement('style');\nstyle.type = 'text/css'\nstyle.innerHTML = '/** * Style.css * FolioReaderKit * * Created by Heberti Almeida on 06/05/15. * Copyright (c) 2015 Folio Reader. All rights reserved. // CSS Reset /html, body, div, span, applet, object, iframe,h1, h2, h3, h4, h5, h6, p, blockquote, pre,a, abbr, acronym, address, big, cite, code,del, dfn, em, img, ins, kbd, q, s, samp,small, strike, strong, sub, sup, tt, var,b, u, i, center,dl, dt, dd, li,fieldset, form, label, legend,table, caption, tbody, tfoot, thead, tr, th, td,article, aside, canvas, details, embed,figure, figcaption, footer, header, hgroup,menu, nav, output, ruby, section, summary,time, mark, audio, video { margin: 0; vertical-align: baseline;}/ HTML5 display-role reset for older browsers /article, aside, details, figcaption, figure,footer, header, hgroup, menu, nav, section { display: block;}/ ePUB /html { -webkit-text-size-adjust: none; / Never autoresize text / padding: 0 0 !important; overflow: hidden;}body { padding: 40px 20px !important; overflow: hidden !important;} { page-break-before: initial !important; /* Reset page breaks to default /}/ Custom padding for tablets /@media only screen and (min-device-width: 768px){ body { padding: 60px 80px !important; }}/ Table /table { border-collapse: collapse; border-spacing: 0; word-break: normal;}tbody, tfoot, thead { vertical-align: middle !important;}td, th, tr { vertical-align: inherit !important;}/ List /dd, dir, menu, ol, ul { margin-left: 30px !important; }ol { list-style-type: decimal !important; }li { display: list-item !important; }ol ol, ol ul, ul ol, ul ul { margin-bottom: 0 !important; margin-top: 0 !important;}/ Links /a { -webkit-touch-callout: none; } / Disable link callback / { -webkit-user-select: text; }img { -webkit-user-select: none; }p { margin: 1.3em 0 1.5em 0; line-height: 1.40em !important; text-indent: 1.25em;}b, strong, th {font-weight: bolder !important;}/* Forced font overrides /code, kbd, pre, samp, tt { font-family: monospace, monospace !important; font-size: 1em;}button, input, select, textarea { display: inline-block !important; }/h1, h2, h3, h4, h5, h6 { font-weight: 400!important; }/del, s, strike { text-decoration: line-through!important; }hr { background-color: rgba(0,0,0,.1) !important; border: none !important; height: 1px !important;}/ Sub and Super /big { font-size: 1.15em !important; }small, sub, sup { font-size: .65em !important; }sub { vertical-align: sub !important; }sup { font-family: monospace !important; vertical-align: super !important;}/ iBooks like /a { text-decoration: none; }pre { white-space: pre-wrap; }@page { margin: 0 0 !important; }table, ol, il { text-align: -webkit-auto; }h1 ,h2 ,h3 ,h4 ,h5 ,h6 { text-align: -webkit-auto; text-rendering: optimizelegibility;}/ allow breaking of words on headers and anchors as they tend to be larger font size or contain longer words /a, h1, h2, h3, h4, h5, h6 { word-break: break-word !important; -webkit-hyphens: none !important; hyphens: none !important;}/ Begin Ted /img, svg, audio, video { max-height: 95% !important; max-width: 100% !important; box-sizing: border-box; object-fit: contain; page-break-inside: avoid;}/ End Ted // Divs are also used to size images so make sure the authors get what they intended // which is for the images boxed in them to be completely visible on screen /div { max-width: 100%; }aside[epub|type~="footnote"] { display: none !important; }ruby > rt, ruby > rp { -webkit-user-select: none; } { -webkit-font-smoothing: subpixel-antialiased }/* * * Highlight classes * /highlight { -webkit-touch-callout: none; -webkit-user-select: none;}/ Remove tap highlight /input, textarea, button, highlight, select, a { -webkit-tap-highlight-color: rgba(0,0,0,0);}/ Highlight styles /html .highlight-yellow {background:rgb(255, 235, 107)}html .highlight-green {background:#C0ED72}html .highlight-blue {background:#ADD8FF}html .highlight-pink {background:#FFB0CA}html .highlight-underline { text-decoration: none; border-bottom: 2px solid #F02814;}html .highlight-yellow, html .highlight-green, html .highlight-blue, html .highlight-pink, span.epub-media-overlay-playing { border-radius: 3px; padding: 0 2px; margin: 0 -2px;}/ default media overlay style /.mediaOverlayStyle0 span.epub-media-overlay-playing { background: #ccc}.mediaOverlayStyle1 .epub-media-overlay-playing { border-bottom: dotted 2px transparent; border-radius: 0;}/ * * Night mode * /html, body { -webkit-transition: all 0.6s ease;}html { background-color: #FFFFFF !important;}body { background-color: transparent !important;}html.nightMode, html.nightMode body { background-color: #131313 !important;}.nightMode p, .nightMode div, .nightMode span:not(.epub-media-overlay-playing) { color: #767676 !important; background-color: transparent !important;}.nightMode h1, .nightMode h2, .nightMode h3, .nightMode h4, .nightMode h5, .nightMode h6 { color: #848484 !important;}html.nightMode .highlight-yellow {background:rgba(255, 235, 107, 0.9)}html.nightMode .highlight-green {background:rgba(192, 237, 114, 0.9)}html.nightMode .highlight-blue {background:rgba(173, 216, 255, 0.9)}html.nightMode .highlight-pink {background:rgba(255, 176, 202, 0.9)}html.nightMode .highlight-underline {border-bottom: 2px solid rgba(240, 40, 20, 0.6)}/ * * Font classes * */.andada, .andada p, .andada span, .andada div { font-family: "Andada", sans-serif !important; }.lato, .lato p, .lato span, .lato div { font-family: "Lato", serif !important; }.lora, .lora p, .lora span, .lora div { font-family: "Lora", serif !important; }.raleway, .raleway p, .raleway span, .raleway div { font-family: "Raleway", sans-serif !important; }html.textSizeOne, .textSizeOne body { font-size: 13px !important; }html.textSizeTwo, .textSizeTwo body { font-size: 15px !important; }html.textSizeThree, .textSizeThree body { font-size: 17px !important; }html.textSizeFour, .textSizeFour body { font-size: 19px !important; }html.textSizeFive, .textSizeFive body { font-size: 21px !important; }h1 { font-size: 2em; line-height: 1.2;}h2 { font-size: 1.5em; line-height: 1.2;}h3 { font-size: 1.17em; line-height: 1.2;}h4 { font-size: 1em; line-height: 1.2;}h5 { font-size: 0.83em; line-height: 1.2;}h6 { font-size: 0.67em; line-height: 1.2;}body { word-break: break-word !important; -webkit-hyphens: auto !important; hyphens: auto !important;}p, span, div { font-size: 1em; line-height: 1.5 !important;}@media only screen and (min-device-width: 600px) { div { font-size: 1em; line-height: 1.438em !important; } body { -webkit-hyphens: none !important; hyphens: none !important; }}';\ndocument.head.appendChild(style);"

Upvotes: -1

Views: 514

Answers (1)

Saqib2235
Saqib2235

Reputation: 51

The problem with WKUserScript initilization on iOS 14 and latter.

For example:

  • If you have complex scripting logic to bridge your web content to your application but your web content also has complex scripting libraries of its own, you avoid possible conflicts by using a client WKContentWorld.
  • If you are writing a general purpose web browser that supports JavaScript extensions, you would use a different client WKContentWorld for each extension.

Since a WKContentWorld object is a namespace it does not contain any data itself. For example:

  • If you store a variable in JavaScript in the scope of a particular WKContentWorld while viewing a particular web page document, after navigating to a new document that variable will be gone.
  • If you store a variable in JavaScript in the scope of a particular WKContentWorld in one WKWebView, that variable will not exist in the same world in another WKWebView.

Slove after adding this line

class MyScript: WKUserScript {

init(source: String) {
    if #available(iOS 14.0, *) {
        super.init(source: source, injectionTime: .atDocumentEnd, forMainFrameOnly: true, in: .defaultClient)
    } else {
        // Fallback on earlier versions
        super.init(source: source,
                   injectionTime: .atDocumentEnd,
                   forMainFrameOnly: true)
    }
}

}

Upvotes: 2

Related Questions