Skip to content

Commit c03eb56

Browse files
committed
updated web view chapter examples
1 parent 4a5c40f commit c03eb56

File tree

8 files changed

+115
-62
lines changed

8 files changed

+115
-62
lines changed

bk2ch11p552webkit/ch24p825webkit.xcodeproj/project.pbxproj

+12-6
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
archiveVersion = 1;
44
classes = {
55
};
6-
objectVersion = 46;
6+
objectVersion = 50;
77
objects = {
88

99
/* Begin PBXBuildFile section */
@@ -140,7 +140,7 @@
140140
};
141141
};
142142
buildConfigurationList = C9EC6B451819AB3F00606A3A /* Build configuration list for PBXProject "ch24p825webkit" */;
143-
compatibilityVersion = "Xcode 3.2";
143+
compatibilityVersion = "Xcode 9.3";
144144
developmentRegion = English;
145145
hasScannedForEncodings = 0;
146146
knownRegions = (
@@ -317,8 +317,11 @@
317317
GCC_PRECOMPILE_PREFIX_HEADER = YES;
318318
GCC_PREFIX_HEADER = "ch24p825webview/ch24p825webkit-Prefix.pch";
319319
INFOPLIST_FILE = "ch24p825webview/ch24p825webkit-Info.plist";
320-
IPHONEOS_DEPLOYMENT_TARGET = 13.0;
321-
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
320+
IPHONEOS_DEPLOYMENT_TARGET = 14.0;
321+
LD_RUNPATH_SEARCH_PATHS = (
322+
"$(inherited)",
323+
"@executable_path/Frameworks",
324+
);
322325
PRODUCT_BUNDLE_IDENTIFIER = "com.neuburg.matt.${PRODUCT_NAME:rfc1034identifier}";
323326
PRODUCT_NAME = ch24p825webkit;
324327
SWIFT_OBJC_BRIDGING_HEADER = "";
@@ -338,8 +341,11 @@
338341
GCC_PRECOMPILE_PREFIX_HEADER = YES;
339342
GCC_PREFIX_HEADER = "ch24p825webview/ch24p825webkit-Prefix.pch";
340343
INFOPLIST_FILE = "ch24p825webview/ch24p825webkit-Info.plist";
341-
IPHONEOS_DEPLOYMENT_TARGET = 13.0;
342-
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
344+
IPHONEOS_DEPLOYMENT_TARGET = 14.0;
345+
LD_RUNPATH_SEARCH_PATHS = (
346+
"$(inherited)",
347+
"@executable_path/Frameworks",
348+
);
343349
PRODUCT_BUNDLE_IDENTIFIER = "com.neuburg.matt.${PRODUCT_NAME:rfc1034identifier}";
344350
PRODUCT_NAME = ch24p825webkit;
345351
SWIFT_OBJC_BRIDGING_HEADER = "";

bk2ch11p552webkit/ch24p825webview/Base.lproj/Main.storyboard

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="15400" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="3uX-z4-z57">
2+
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="17147" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="3uX-z4-z57">
33
<device id="retina4_7" orientation="portrait" appearance="light"/>
44
<dependencies>
5-
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="15404"/>
5+
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="17120"/>
66
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
77
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
88
</dependencies>
@@ -14,8 +14,8 @@
1414
<view key="view" contentMode="scaleToFill" id="kh9-bI-dsS">
1515
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
1616
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
17-
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
1817
<viewLayoutGuide key="safeArea" id="9j4-ul-Ujr"/>
18+
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
1919
</view>
2020
<navigationItem key="navigationItem" id="yhr-XM-kMI">
2121
<barButtonItem key="rightBarButtonItem" title="Push" id="RgB-EV-YE2">
@@ -40,20 +40,20 @@
4040
<wkWebView contentMode="scaleToFill" restorationIdentifier="wv" customUserAgent="" translatesAutoresizingMaskIntoConstraints="NO" id="s19-Cm-bas">
4141
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
4242
<color key="backgroundColor" red="0.36078431370000003" green="0.38823529410000002" blue="0.4039215686" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
43-
<wkWebViewConfiguration key="configuration" applicationNameForUserAgent="Version/13.0.1 Safari/605.1.15">
43+
<wkWebViewConfiguration key="configuration" applicationNameForUserAgent="Version/13.1.2 Safari/605.1.15">
4444
<audiovisualMediaTypes key="mediaTypesRequiringUserActionForPlayback" none="YES"/>
4545
<wkPreferences key="preferences"/>
4646
</wkWebViewConfiguration>
4747
</wkWebView>
4848
</subviews>
49+
<viewLayoutGuide key="safeArea" id="PPW-r3-6rE"/>
4950
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
5051
<constraints>
5152
<constraint firstItem="PPW-r3-6rE" firstAttribute="bottom" secondItem="s19-Cm-bas" secondAttribute="bottom" id="Eh2-8T-gxr"/>
5253
<constraint firstItem="PPW-r3-6rE" firstAttribute="trailing" secondItem="s19-Cm-bas" secondAttribute="trailing" id="OcI-Xx-iqZ"/>
5354
<constraint firstItem="s19-Cm-bas" firstAttribute="top" secondItem="PPW-r3-6rE" secondAttribute="top" id="WNM-Hm-mjd"/>
5455
<constraint firstItem="s19-Cm-bas" firstAttribute="leading" secondItem="PPW-r3-6rE" secondAttribute="leading" id="lSp-VY-Kme"/>
5556
</constraints>
56-
<viewLayoutGuide key="safeArea" id="PPW-r3-6rE"/>
5757
</view>
5858
<connections>
5959
<outlet property="wv" destination="s19-Cm-bas" id="7AU-ZJ-DsY"/>

bk2ch11p552webkit/ch24p825webview/WebViewController.swift

+34-20
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ final class WebViewController: UIViewController, WKNavigationDelegate, WKScriptM
4545
document.documentElement.appendChild(s);
4646
"""
4747
}
48+
var cssrule2 : String {
49+
return """
50+
var s = document.createElement('style');
51+
s.textContent = `body { font-size: ${thefontsize}px; }`;
52+
document.documentElement.appendChild(s);
53+
"""
54+
}
4855
@IBOutlet weak var wv : WKWebView!
4956

5057
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
@@ -136,15 +143,15 @@ final class WebViewController: UIViewController, WKNavigationDelegate, WKScriptM
136143
switch leak {
137144
case true:
138145
let config = self.wv.configuration
139-
config.userContentController.add(self, name: "playbutton")
146+
config.userContentController.add(self, contentWorld: .defaultClient, name: "playbutton")
140147
case false:
141148
let config = self.wv.configuration
142149
let handler = MyMessageHandler(delegate:self)
143-
config.userContentController.add(handler, name: "playbutton")
150+
config.userContentController.add(handler, contentWorld: .defaultClient, name: "playbutton")
144151
}
145152

146-
wv.configuration.applicationNameForUserAgent = "Version/1.0 MyShinyBrowser/1.0" // not working, too late?
147-
153+
wv.configuration.applicationNameForUserAgent = "Version/13.1.2 Safari/605.1.15" // not working, too late?
154+
// so presumably if the web view comes from the storyboard, you must set this in the storyboard
148155

149156
wv.restorationIdentifier = "wv"
150157
wv.scrollView.backgroundColor = .black // web view alone, ineffective
@@ -342,8 +349,9 @@ final class WebViewController: UIViewController, WKNavigationDelegate, WKScriptM
342349
if self.fontsize < 10 {
343350
self.fontsize = 20
344351
}
345-
let s = self.cssrule
346-
self.wv.evaluateJavaScript(s)
352+
// let s = self.cssrule
353+
// self.wv.evaluateJavaScript(s, in: nil, in: .defaultClient)
354+
self.wv.callAsyncJavaScript(self.cssrule2, arguments: ["thefontsize": self.fontsize], in: nil, in: .defaultClient)
347355
}
348356

349357
deinit {
@@ -375,44 +383,47 @@ final class WebViewController: UIViewController, WKNavigationDelegate, WKScriptM
375383
print(navigation.effectiveContentMode.rawValue) // 2, but I'm still not seeing the desktop version
376384
}
377385

378-
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Swift.Void) {
386+
func webView(_ webView: WKWebView,
387+
decidePolicyFor navigationAction: WKNavigationAction,
388+
preferences: WKWebpagePreferences, decisionHandler: @escaping (WKNavigationActionPolicy, WKWebpagePreferences) -> Void) {
379389
print("old here")
390+
preferences.preferredContentMode = .desktop
391+
print("asking for desktop version")
392+
380393
if navigationAction.navigationType == .linkActivated {
381394
if let url = navigationAction.request.url {
382395
if url.scheme == "file" { // we do not scroll to anchor; bug in iOS 11?
383-
decisionHandler(.allow)
396+
decisionHandler(.allow, preferences)
384397
return
385398
}
386399
print("user would like to navigate to \(url)")
387400
var whichNav : Int { return 1 }
388401
switch whichNav {
389402
case 0:
390403
// this is how you would open in Mobile Safari
391-
if #available(iOS 10.0, *) {
392-
UIApplication.shared.open(url)
393-
} else {
394-
UIApplication.shared.openURL(url)
395-
}
396-
decisionHandler(.cancel)
404+
UIApplication.shared.open(url)
405+
decisionHandler(.cancel, preferences)
397406
return
398407
case 1:
399408
// this is how to use the new Safari view controller
400409
let svc = SFSafariViewController(url: url)
401410
self.safariurl = url
402411
svc.restorationIdentifier = "sf" // doesn't help
403412
svc.restorationClass = type(of:self)
404-
// svc.delegate = self
413+
svc.delegate = self
405414
self.present(svc, animated: true)
406-
decisionHandler(.cancel)
415+
decisionHandler(.cancel, preferences)
416+
return
417+
default:
418+
decisionHandler(.allow, preferences)
407419
return
408-
default:break
409420
}
410421
}
411422
}
412423
// must always call _something_
413-
decisionHandler(.allow)
424+
decisionHandler(.allow, preferences)
414425
}
415-
426+
416427
func webViewWebContentProcessDidTerminate(_ webView: WKWebView) { // new in iOS 9
417428
print("process did terminate") // but I do not know under what circumstances this will actually be called
418429
}
@@ -425,7 +436,10 @@ final class WebViewController: UIViewController, WKNavigationDelegate, WKScriptM
425436
func safariViewController(_ controller: SFSafariViewController, didCompleteInitialLoad didLoadSuccessfully: Bool) {
426437
print("loaded svc")
427438
}
428-
439+
440+
func safariViewControllerWillOpenInBrowser(_ controller: SFSafariViewController) { // new in iOS 14
441+
print("will open in browser")
442+
}
429443

430444
func userContentController(_ userContentController: WKUserContentController,
431445
didReceive message: WKScriptMessage) {

bk2ch11p553webkit2/ch24p825webkit2.xcodeproj/project.pbxproj

+12-6
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
archiveVersion = 1;
44
classes = {
55
};
6-
objectVersion = 46;
6+
objectVersion = 50;
77
objects = {
88

99
/* Begin PBXBuildFile section */
@@ -112,7 +112,7 @@
112112
};
113113
};
114114
buildConfigurationList = C9EC6B451819AB3F00606A3A /* Build configuration list for PBXProject "ch24p825webkit2" */;
115-
compatibilityVersion = "Xcode 3.2";
115+
compatibilityVersion = "Xcode 9.3";
116116
developmentRegion = English;
117117
hasScannedForEncodings = 0;
118118
knownRegions = (
@@ -279,8 +279,11 @@
279279
GCC_PRECOMPILE_PREFIX_HEADER = YES;
280280
GCC_PREFIX_HEADER = "ch24p825webview/ch24p825webkit2-Prefix.pch";
281281
INFOPLIST_FILE = "ch24p825webview/ch24p825webkit2-Info.plist";
282-
IPHONEOS_DEPLOYMENT_TARGET = 13.0;
283-
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
282+
IPHONEOS_DEPLOYMENT_TARGET = 14.0;
283+
LD_RUNPATH_SEARCH_PATHS = (
284+
"$(inherited)",
285+
"@executable_path/Frameworks",
286+
);
284287
PRODUCT_BUNDLE_IDENTIFIER = "com.neuburg.matt.${PRODUCT_NAME:rfc1034identifier}";
285288
PRODUCT_NAME = ch24p825webkit2;
286289
SWIFT_OBJC_BRIDGING_HEADER = "";
@@ -299,8 +302,11 @@
299302
GCC_PRECOMPILE_PREFIX_HEADER = YES;
300303
GCC_PREFIX_HEADER = "ch24p825webview/ch24p825webkit2-Prefix.pch";
301304
INFOPLIST_FILE = "ch24p825webview/ch24p825webkit2-Info.plist";
302-
IPHONEOS_DEPLOYMENT_TARGET = 13.0;
303-
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
305+
IPHONEOS_DEPLOYMENT_TARGET = 14.0;
306+
LD_RUNPATH_SEARCH_PATHS = (
307+
"$(inherited)",
308+
"@executable_path/Frameworks",
309+
);
304310
PRODUCT_BUNDLE_IDENTIFIER = "com.neuburg.matt.${PRODUCT_NAME:rfc1034identifier}";
305311
PRODUCT_NAME = ch24p825webkit2;
306312
SWIFT_OBJC_BRIDGING_HEADER = "";

bk2ch11p554AnchorScrollFail/AnchorScrollFail.xcodeproj/project.pbxproj

+14-7
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
archiveVersion = 1;
44
classes = {
55
};
6-
objectVersion = 48;
6+
objectVersion = 50;
77
objects = {
88

99
/* Begin PBXBuildFile section */
@@ -105,7 +105,7 @@
105105
};
106106
};
107107
buildConfigurationList = 810EEFED1F8542E0002509B2 /* Build configuration list for PBXProject "AnchorScrollFail" */;
108-
compatibilityVersion = "Xcode 8.0";
108+
compatibilityVersion = "Xcode 9.3";
109109
developmentRegion = en;
110110
hasScannedForEncodings = 0;
111111
knownRegions = (
@@ -270,7 +270,8 @@
270270
IPHONEOS_DEPLOYMENT_TARGET = 11.1;
271271
MTL_ENABLE_DEBUG_INFO = NO;
272272
SDKROOT = iphoneos;
273-
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
273+
SWIFT_COMPILATION_MODE = wholemodule;
274+
SWIFT_OPTIMIZATION_LEVEL = "-O";
274275
VALIDATE_PRODUCT = YES;
275276
};
276277
name = Release;
@@ -282,8 +283,11 @@
282283
CODE_SIGN_STYLE = Automatic;
283284
DEVELOPMENT_TEAM = W3LHX5RGV2;
284285
INFOPLIST_FILE = AnchorScrollFail/Info.plist;
285-
IPHONEOS_DEPLOYMENT_TARGET = 13.0;
286-
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
286+
IPHONEOS_DEPLOYMENT_TARGET = 14.0;
287+
LD_RUNPATH_SEARCH_PATHS = (
288+
"$(inherited)",
289+
"@executable_path/Frameworks",
290+
);
287291
PRODUCT_BUNDLE_IDENTIFIER = com.neuburg.matt.AnchorScrollFail;
288292
PRODUCT_NAME = "$(TARGET_NAME)";
289293
SWIFT_VERSION = 5.0;
@@ -298,8 +302,11 @@
298302
CODE_SIGN_STYLE = Automatic;
299303
DEVELOPMENT_TEAM = W3LHX5RGV2;
300304
INFOPLIST_FILE = AnchorScrollFail/Info.plist;
301-
IPHONEOS_DEPLOYMENT_TARGET = 13.0;
302-
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
305+
IPHONEOS_DEPLOYMENT_TARGET = 14.0;
306+
LD_RUNPATH_SEARCH_PATHS = (
307+
"$(inherited)",
308+
"@executable_path/Frameworks",
309+
);
303310
PRODUCT_BUNDLE_IDENTIFIER = com.neuburg.matt.AnchorScrollFail;
304311
PRODUCT_NAME = "$(TARGET_NAME)";
305312
SWIFT_VERSION = 5.0;

bk2ch11p555JSAlertTest/JSAlertTest.xcodeproj/project.pbxproj

+14-7
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
archiveVersion = 1;
44
classes = {
55
};
6-
objectVersion = 48;
6+
objectVersion = 50;
77
objects = {
88

99
/* Begin PBXBuildFile section */
@@ -102,7 +102,7 @@
102102
};
103103
};
104104
buildConfigurationList = 810EF00D1F856168002509B2 /* Build configuration list for PBXProject "JSAlertTest" */;
105-
compatibilityVersion = "Xcode 8.0";
105+
compatibilityVersion = "Xcode 9.3";
106106
developmentRegion = en;
107107
hasScannedForEncodings = 0;
108108
knownRegions = (
@@ -266,7 +266,8 @@
266266
IPHONEOS_DEPLOYMENT_TARGET = 11.1;
267267
MTL_ENABLE_DEBUG_INFO = NO;
268268
SDKROOT = iphoneos;
269-
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
269+
SWIFT_COMPILATION_MODE = wholemodule;
270+
SWIFT_OPTIMIZATION_LEVEL = "-O";
270271
VALIDATE_PRODUCT = YES;
271272
};
272273
name = Release;
@@ -278,8 +279,11 @@
278279
CODE_SIGN_STYLE = Automatic;
279280
DEVELOPMENT_TEAM = W3LHX5RGV2;
280281
INFOPLIST_FILE = JSAlertTest/Info.plist;
281-
IPHONEOS_DEPLOYMENT_TARGET = 13.0;
282-
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
282+
IPHONEOS_DEPLOYMENT_TARGET = 14.0;
283+
LD_RUNPATH_SEARCH_PATHS = (
284+
"$(inherited)",
285+
"@executable_path/Frameworks",
286+
);
283287
PRODUCT_BUNDLE_IDENTIFIER = com.neuburg.matt.JSAlertTest;
284288
PRODUCT_NAME = "$(TARGET_NAME)";
285289
SWIFT_VERSION = 5.0;
@@ -294,8 +298,11 @@
294298
CODE_SIGN_STYLE = Automatic;
295299
DEVELOPMENT_TEAM = W3LHX5RGV2;
296300
INFOPLIST_FILE = JSAlertTest/Info.plist;
297-
IPHONEOS_DEPLOYMENT_TARGET = 13.0;
298-
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
301+
IPHONEOS_DEPLOYMENT_TARGET = 14.0;
302+
LD_RUNPATH_SEARCH_PATHS = (
303+
"$(inherited)",
304+
"@executable_path/Frameworks",
305+
);
299306
PRODUCT_BUNDLE_IDENTIFIER = com.neuburg.matt.JSAlertTest;
300307
PRODUCT_NAME = "$(TARGET_NAME)";
301308
SWIFT_VERSION = 5.0;

0 commit comments

Comments
 (0)