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
)
}
Related
I am using Swift 4 & WKWebView. The HTML pages are included in the app (not downloaded from server).
For configuration purpose I would like to add parameters to the URL (e.g. ...index.html?debug=true).
Currently I am using the following approach to load the page:
let indexHTMLPath = Bundle.main.path(forResource: "f7Shoma/index", ofType: "html")
let url = URL(fileURLWithPath: indexHTMLPath!)
let request = URLRequest(url: url)
...
appWebView!.load(request)
How can parameters be added/passed to the page?
You can use URLComponents to create a URL with a query component:
var components = URLComponents(string: indexHTMLPath)
components?.queryItems = [URLQueryItem(name: "debug", value: "true")]
if let result = components?.url {
let request = URLRequest(url: url)
appWebView!.load(request)
}
This question already has answers here:
Add http:// to NSURL if it's not there
(5 answers)
Closed 5 years ago.
I want show the URL in Web View
here is my code
let urlString:String = "https://www.apple.com"
let url:URL = URL(string: urlString)!
let urlRequest:URLRequest = URLRequest(url: url)
webView.load(urlRequest)
urlTextField.text = urlString
if user forget to write http or https the app crashed how can I resolve this error
Just use starts(with:) on string to detect if the url string starts with http/https, and if not, add the "http://" yourself (also, use safe if let instead of force unwrap):
var urlString: String = "www.apple.com"
if !urlString.starts(with: "http://") && !urlString.starts(with: "https://") {
urlString = "http://\(urlString)"
}
if let url: URL = URL(string: urlString) {
let urlRequest: URLRequest = URLRequest(url: url)
webView.load(urlRequest)
urlTextField.text = urlString
}
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.
I want to load GIF in my swift 3 application. I used this code to do that but the gif is not showed.
let filePath = Bundle.main.path(forResource: “myGif”, ofType: "gif")
let gif = NSData(contentsOfFile: filePath!)
self.webView.load(gif! as Data, mimeType: "image/gif", textEncodingName: String(), baseURL: NSURL() as URL)
self.webView.isUserInteractionEnabled = false
I don't know what is wrong with my code (I'm really new to swift and iOS development).
Any help please?
I found SwiftGif Lbrary from github, it doesn't use webview (if you want to use webview write me to comments to update the answer)
EDIT:
Webview:
let url = Bundle.main.url(forResource: "source", withExtension: "gif")!
let data = try! Data(contentsOf: url)
webview.load(data, mimeType: "image/gif", textEncodingName: "UTF-8", baseURL: NSURL() as URL)
webview.scalesPageToFit = true
webview.contentMode = UIViewContentMode.scaleAspectFit
Hope it helps.
Here is working example. I downloaded the gif image from web and added it to sample project.
let url = Bundle.main.url(forResource: "hsk", withExtension: "gif")!
let data = try! Data(contentsOf: url)
webView.load(data, mimeType: "image/gif", textEncodingName: "UTF-8", baseURL: NSURL() as URL)
With the code above, I could see the animated gif as below.
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)