上記の記事を参考に実装したのだが、webview側で値が取れない・・・
いろいろ調べたら、戻すときのheaderに
- Access-Control-Allow-Origin
- Access-Control-Allow-Headers
が必要だったようです。(多分)
sample.swift
private func sendBackPlainText(str: String, status_code:Int) {
if let data: NSData = str.dataUsingEncoding(NSUTF8StringEncoding) {
let headers = [
"Content-Type": "text/plain",
"Content-Length": "\(data.length)",
"Content-Encoding": "UTF-8",
"Access-Control-Allow-Origin" : "*", //ここ
"Access-Control-Allow-Headers" : "*" //ここ
]
let response = NSHTTPURLResponse(URL: self.request.URL!,
statusCode: status_code,
HTTPVersion: "1.1",
headerFields: headers)
self.client?.URLProtocol(self,
didReceiveResponse: response!,
cacheStoragePolicy: NSURLCacheStoragePolicy.AllowedInMemoryOnly)
self.client?.URLProtocol(self, didLoadData: data)
self.client?.URLProtocolDidFinishLoading(self)
}
}