I am working with WKWebView and it is loading page so slow. I have a link that I want to open in the app, so I searched for the solution and got that WKWebView is the right choice. but when I implemented it it takes 10 to 15 minutes with the loading.
however the time that is taken on android is about 20 seconds. I do not know what is the problem here. Is it the configurations problem or something else.
This is how I am doing it:
let url = URL(string: "https://www.google.com")!
webView = WKWebView()
webContainer.addSubview(webView)
webView.navigationDelegate = self
webView.load(URLRequest(url: url))
Here webContainer is a UIView that will be containing webView.
Please let me know what I am doing wrong and what could be problem here.
UPDATE 1:
THE LINK I AM USING IS OF WEBSITE THAT STREAMS A SECURITY CAMERA VIDEO
. I CANT SHARE IT SINCE THERE IS A SECURITY REASONS.
UPDATE 2:
However If I open www.google.com it also takes time.
Related
I am pretty new to Xcode and app development.
I have a platform that has a homepage with a login form and the designated Login/Join page(s).
My webview app is built to open the main URL where the homepage is located, e.g domain.com.
How can I set up to show the login page when the app is open instead of the main URL, e.g. domain.com/signin
I assume I should be able to set it up in the Xcode. Also, if this change is made in the Xcode will it sync with my App Dev Account?
Thank you in advance!!
Assuming you are using a WKWebView you can load a new page as so:
var webView = WKWebView()
if let loginUrl = URL(string: "domain.com/signin") {
webView.load(URLRequest(url: url))
}
A couple resources for you to learn more about WebKit:
https://medium.com/swift-productions/create-a-web-view-xcode-12-swift-5-3-9806d41cc9b
https://www.hackingwithswift.com/read/4/2/creating-a-simple-browser-with-wkwebview
Here is my code, which should load a webpage from a given URL:
let url = URL(string: urlString)
var urlRequest = URLRequest(url: url!)
urlRequest.cachePolicy = .returnCacheDataElseLoad
webView.loadRequest(urlRequest)
The problem is that I want to achieve the following:
Fully cache a website's HTML/CSS/JS
Fully load responses to clicks on hyperlinks from cache, unless an external web service needs to be accessed
Store website's JSON data (e.g. form data) in the device and make them available offline as cookies/DB records
I don't mean building a Progressive Web App, which can come with offline functionality (but not in iOS). What I mean is adding a Service Worker to a website container (WebView or iframe) in iOS. Same origin policy is not a problem, I am able to edit the website code.
Is it possible to achieve it in iOS and possibly Android?
I'm using some WKWebViews within my apps.
Basically, I have a first WKWebView and the user should be able to create another one WKWebView without any cookies so he/she wouldn't be logged in any account he logged in before on the first WKWebView.
It's like an incognito browsing mode.
But I can't see anything that would allow me to do that with WKWebView.
I think it's not possible as this bug suggests :
https://bugs.webkit.org/show_bug.cgi?id=140191
But maybe someone found a workaround ?
Any help would be really appreciated.
After reading Apple's documentation, I finally got it to work.
I just initialise the WKWebView with a non-persistent WKWebsiteDataStore like that :
let configuration = WKWebViewConfiguration()
configuration.websiteDataStore = WKWebsiteDataStore.nonPersistentDataStore()
let webView = WKWebView(frame: CGRectZero, configuration: configuration)
I'm building an app that uses Single Sign On for users to log in. After the user enters a successful ID and password, the web side of things returns headers which I grab and store in my app. The WKWebView also sets a cookie that the user successfully logged in. This is what I want to avoid or undo.
The undesired behavior that I'm seeing is that if I log in a user, everything goes well, and then I log them out and go to log back in again, the WKWebView thinks the user is still logged in and takes them to an undesired URL.
In iOS 9, mitigating this is fairly simple:
let config = WKWebViewConfiguration()
config.websiteDataStore = WKWebsiteDataStore.nonPersistentDataStore()
let webView = WKWebView(frame: .zero, configuration: config)
However in iOS 8.4, making making sure the cookies are clear each time the user goes in to load the Single Sign On URL is more complicated.
I've tried approaches where I loop through cookies in NSHTTPCookieStorage.sharedHTTPCookieStorage() and remove them. Unfortunately, the cookie count is 0.
I've also tried removing the /Cookies directory in NSFileManager.defaultManager(). This also does not work.
One thing that kind of worked was doing the following. Although this approach didn't allow me to get the headers because a redirect after login needed to happen and this interfered (in iOS 9+ and 8.4)
req = NSMutableURLRequest(URL: url)
req?.HTTPShouldHandleCookies = false
let webView = WKWebView()
webView.loadRequest(req ?? NSURLRequest(URL: url))
I'd prefer to clear cookies in the deinit of my view that holds my WKWebView if that's a possible solution here.
This might be a longshot, but what about overriding the cookie accept policy to reject all cookies at the time of sign in?
NSHTTPCookieStorage.sharedHTTPCookieStorage().cookieAcceptPolicy = .Never
Another idea would be to manually mange the cookies with a custom WKProcessPool subclass (which is not really documented). I believe that's how Firefox manages any cookie issues.
Even if you mention it, is this the approach you've already tried in your deinit?
let storage = NSHTTPCookieStorage.sharedHTTPCookieStorage()
for cookie in storage.cookies! {
storage.deleteCookie(cookie)
}
I'm quite new to swift & IOS development.
Is it possible to create an app in swift that will run a website inside of the app?
If so could someone point me in the right direction?
Thanks in advance.
You need to add UIWebView component or WKWebView component - tutorial can be found here: http://www.kinderas.com/technology/2014/6/7/getting-started-with-wkwebview-using-swift-in-ios-8
It is possible but it is going to be rejected by Apple in all probability unless you provide some more functionality, i.e., don't JUST wrap a website in an app and submit it.
Having said that, all you need to do is use an instance of UIWebView in your app and set the URL to load your website in viewDidLoad function
So your viewDidLoad function would look something like this:
override func viewDidLoad() {
super.viewDidLoad()
let url = NSURL(string: "http://www.apple.com")
var request = NSURLRequest(URL: url)
self.webView.loadRequest(request)
}