我无法打开网址:
https://www.osmhydrant.org/beta/#{"map":{"rotation":0,"visibleBaseLayer":"osm","visibleOverlays":\["FireHydrant","WaterTank","SuctionPoint","DryRiserInlet"\],"markers":\[\],"lat":48.2095786151576,"lon":16.473358494913136,"zoom":17.356666666666655},"activeFeature":{"lat":52.6178998,"lon":13.306985,"id":null,"edit":{"isActive":false,"isPanelVisible":false},"isPopupVisible":false},"setting":{"labelProperty":null,"language":"de","doClustering":true,"doOgdVienna":false,"doPhotoIntegration":false},"ui":{"sidebar":null}}
使用Xcode 11.5中的WKWebview。
var webView: WKWebView!
webView.navigationDelegate = self
if let encodedURL = myurl.addingPercentEncoding(withAllowedCharacters: .urlFragmentAllowed),
let url = URL(string: encodedURL)
{
webView.configuration.preferences.javaScriptEnabled = true
let request = URLRequest(url: url, cachePolicy: .reloadIgnoringCacheData, timeoutInterval: 50)
webView.load(request)
}
我尝试了其他组合:.urlUserAllowed
.urlPasswordAllowed
.urlHostAllowed
.urlPathAllowed
.urlQueryAllowed
.urlFragmentAllowed
但没有积极的结果。
看来,#
之后的第二部分被否定。
您的片段字符串看起来是JSON格式,并且转义了错误的大小写,例如“ [”。因此,我首先删除了\字符并验证了JSON格式,如下所示:
{
"map":{
"rotation":0,
"visibleBaseLayer":"osm",
"visibleOverlays":[
"FireHydrant",
"WaterTank",
"SuctionPoint",
"DryRiserInlet"
],
"markers":[
],
"lat":48.2095786151576,
"lon":16.473358494913136,
"zoom":17.356666666666655
},
"activeFeature":{
"lat":52.6178998,
"lon":13.306985,
"id":null,
"edit":{
"isActive":false,
"isPanelVisible":false
},
"isPopupVisible":false
},
"setting":{
"labelProperty":null,
"language":"de",
"doClustering":true,
"doOgdVienna":false,
"doPhotoIntegration":false
},
"ui":{
"sidebar":null
}
}
然后,您应该这样避免使用双引号。
let fragment = "{\"map\":{\"rotation\":0,\"visibleBaseLayer\":\"osm\",\"visibleOverlays\":[\"FireHydrant\",\"WaterTank\",\"SuctionPoint\",\"DryRiserInlet\"],\"markers\":[],\"lat\":48.2095786151576,\"lon\":16.473358494913136,\"zoom\":17.356666666666655},\"activeFeature\":{\"lat\":52.6178998,\"lon\":13.306985,\"id\":null,\"edit\":{\"isActive\":false,\"isPanelVisible\":false},\"isPopupVisible\":false},\"setting\":{\"labelProperty\":null,\"language\":\"de\",\"doClustering\":true,\"doOgdVienna\":false,\"doPhotoIntegration\":false},\"ui\":{\"sidebar\":null}}"
最后,您可以获取所需的URL。
guard let encoded = frament.addingPercentEncoding(withAllowedCharacters: .urlFragmentAllowed) else { return }
guard let url = URL(string: "https://www.osmhydrant.org/beta/#\(encoded)") else { return }
// now you can load this request.
let request = URLRequest(url: url)
webView.load(request)