在Swift中使用WKWebview打开此URL

问题描述 投票:0回答:1

我无法打开网址:

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但没有积极的结果。

看来,#之后的第二部分被否定。

ios swift wkwebview
1个回答
0
投票

您的片段字符串看起来是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)
© www.soinside.com 2019 - 2024. All rights reserved.