Spaces in UIWebView - ios

I am working on an app in which I am using UIWebView, when the webpage loads it adds space in the screen see below
Below is the code I have tried,
let requestURL = NSURL(string: "url")
let request = NSURLRequest(url: requestURL! as URL)
webView.loadRequest(request as URLRequest)
self.automaticallyAdjustsScrollViewInsets = false
//webView.scrollView.contentInset.right = UIEdgeInsets.zero.right
nothing works.

Related

How to refresh UIWebView images

I went through a few SO post and can't seem to fix my issues.
The codes works fine, but if i update my profile pics, the UIWebView, will still show the cached image instead of the updated image.
if i close my app and reopen, the webview will update to the new image.
any pointer are greatly appreciated.
func getLeaderBoard()
{
leaderWebView.loadRequest(getStaticWebView(urlStr: getLeaderBoardUrl) as URLRequest)
}
func getStaticWebView(urlStr:String) -> NSMutableURLRequest
{
let url = URL.init(string: urlStr)
let request = NSMutableURLRequest.init(url: url!)
request.httpMethod = "GET"
request.setValue("token \(kToken)", forHTTPHeaderField: "Authorization")
request.cachePolicy = .reloadIgnoringLocalAndRemoteCacheData
URLCache.shared.removeAllCachedResponses()
URLCache.shared.diskCapacity = 0
URLCache.shared.memoryCapacity = 0
return request
}

Unable to read data while making request

here i am getting strange issue like i can able to make request into simulator but in real device gave error like "unable to read data" also attached screen shot
My Code :
let urlPath: String = url_to_request
let url:NSURL = NSURL(string: urlPath)!
let session = URLSession.shared
let request = NSMutableURLRequest(url: url as URL)
request.httpMethod = "POST"
request.cachePolicy = NSURLRequest.CachePolicy.reloadIgnoringCacheData

iOS Swift WKWebView login with Session

I come from UIWebView where I had following code:
let url2 = NSURL(string: URL+"/auth?id="+self.username!+"&pw="+self.password!)
let task2 = NSURLSession.sharedSession().dataTaskWithURL(url2!) {(data, response, error) in
print(NSString(data: data!, encoding: NSUTF8StringEncoding))
}
task2.resume()
Doing this I was automatically logged in.
But now with the WKWebView this is not working anymore.
The Code is executed just before loading the request:
self.webView = WKWebView()
let request = NSURLRequest(URL: portalURL!)
self.webView!.loadRequest(request)
Username and Password are stored on the Device, when the User installs the Application.
How can I get a similar effect with WKWebView?
You'd better add user info to request header. Like this:
let url = NSURL(string: urlString)
let urlRequest = NSMutableURLRequest(URL: url!)
if needAddHttpHeader {
urlRequest.addValue("login", forHTTPHeaderField: "User-Login")
urlRequest.addValue("token", forHTTPHeaderField: "User-Token")
UBLog(urlRequest)
}
webView.loadRequest(urlRequest)

How to save UIWebView content for offline display?

I'm using Xcode 7.2 and Swift to create an iOS app, on this app I display the content of my website, however if I was offline the content will not be shown. So I want to cache the webpage and display for offline.
After I declared everything I'm using the following code :
var URLPATH="http://google.com"
let requestURL = NSURL(string: URLPATH)
let request = NSURLRequest(URL: requestURL!)
WB.loadRequest(request)
HTML to Data
if let url = URL(string: urlString) {
person.setValue(try? Data(contentsOf: url), forKey: "content_article")
}
Data to WebView
if let savedObject = fetchedObjects?.first,
let data = savedObject.content_article as? Data,
let baseStringUrl = savedObject.content_url,
let baseURL = URL(string: baseStringUrl) {
webView.load(
data, mimeType: "text/html",
textEncodingName: "",
baseURL: baseURL
)
}

UIWebView cache not working

I am trying to cache my UIWebView for when I have no network connection but it doesn't work. I tried looking into HanekeSwift but that only works with JSON requests as far as I could find.
This is what I currently have:
override func viewDidLoad() {
super.viewDidLoad()
let id = NSUserDefaults.standardUserDefaults().objectForKey("id") as! String
let url = NSURL (string: "http://****.php?id=\(id)");
let requestObj = NSURLRequest(URL: url!);
subtitleLabel.loadRequest(requestObj);
}
My question was answered here:
Getting error with cachePolicy
Thanks to KudoCC for pointing me in the right direction I got this as my final code:
var url = NSURL(string: "****.php?page=over_polen")
let requestObj = NSURLRequest(URL: url!, cachePolicy: NSURLRequestCachePolicy.ReturnCacheDataElseLoad, timeoutInterval: 5);
webViewLabel.loadRequest(requestObj);

Resources