Get the URL from webview component - xcode

I am using the WebView component and sometimes I reload this component. But sometimes, before reloading I would like to get the URL where the WebView is still pointing.
Is it possible to get this information??
Thank you in advance.
Alex

UIWebView *myWebView = // ... well, we'll just say it exists.
NSLog(#"last loaded or currently loading URL: %#", myWebView.request.URL);
The request is a NSURLRequest object which was used to request the page so you can pretty much get anything might need out of it.

Related

Reload vue2-bootstrap-table data using Ajax

Using vue2-bootstrap-table component, I want to force the component to call the url and refresh the data programmatically.
This is the component usage:
<vue-bootstrap-table ref="table"
:columns="columns"
:ajax="tableAjax"
:sortable="true"
:paginated="true"
:multi-column-sortable=true
:show-filter="true"
>
</vue-bootstrap-table>
Now in a method I change the url as follows:
this.tableAjax.url = NEW_URL
My question is how tell the vue2-bootstrap-table to reload the data using this new url
Testing all methods listed by Chrome browser console, I found that the processFilter() method will do that:
this.$refs['table'].processFilter()

How can I decrease in app website load time using WKWebView?

I am trying to decrease to load time of in app websites. Our application is currently using WKWebView and right now we have this code to load the webpage
var webView : WKWebView
if let urlStr = self.webUrl, url = NSURL (string: urlStr) {
let requestObj = NSURLRequest(URL: url)
self.webView.loadRequest(requestObj)
}
Is there a way to improve the performance? Would using a dispatch method help? I attempted to use dispatch_async and didn't notice any changes. Also is there a way to track how long it took the webpage to load?
WKWebView wasn't the source of the problem. It is now apparent that the lag came from other sources in the application and WKWebView has little impact on the actual performance of loading pages. SafariWebView, although easier to implement lacks customization and seems to be slower than WKWebView.

How do I change the WKWebview's user agent in OS X Yosemite?

How do you change the user agent used by WKWebview?
With the older WebView, I could write the following to change the user agent:
[myWebView setCustomUserAgent:#"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_4)
AppleWebKit/537.77.4 (KHTML,like Gecko) Version/7.0.5 Safari/537.77.4"];
Very simple in Swift. Just place the following into your App DelegatedidFinishLaunchingWithOptions.
NSUserDefaults.standardUserDefaults().registerDefaults(["UserAgent" : "Custom Agent"])
If you want to append to the existing agent string then:
let userAgent = UIWebView().stringByEvaluatingJavaScriptFromString("navigator.userAgent")! + " Custom Agent"
NSUserDefaults.standardUserDefaults().registerDefaults(["UserAgent" : userAgent])
Note: You will need to uninstall and reinstall the App to avoid appending to the existing agent string.
I don't have an answer for this. However, some pointers from my research so far:
In iOS, it's possible to set a custom user agent for a UIWebView like this:
NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:agent, #"UserAgent", nil];
[[NSUserDefaults standardUserDefaults] registerDefaults:dictionary];
In OSX, there was a setCustomUserAgent method for WebView elements that did the trick.
However, this doesn't work for WKWebView (at least, in OSX). I couldn't find any documentation about it from Apple, either.
Hope somebody can help!
I ran into the same issue, but managed to work around it using a combination of loadHTMLString on the WKWebView and a NSMutableURLRequest to do the heavy lifting.
My search on how to call some method on the WKWebView itself lead me to http://trac.webkit.org/changeset/165594, which implies there is a private method _setCustomUserAgent to do this. I'm not proficient enough in cocoa/swift to figure this one out.
I ended up using the code below, as I really only need to fetch the contents of a single URL and display it, but it may be helpful in some way.
What it does is simply loading the contents of an URL into the WKWebView as string, I suspect you may lose back/forward navigation and such, and it will only work for the initial page display, as the WKWebView will take over clicks and asset loading.
(please note, this example is written in Swift and not Objective-C)
self.webView = WKWebView(frame: webViewRect, configuration: webViewConfig)
// create the request
let url = NSURL(string: "https://example.com/")
let request = NSMutableURLRequest(URL: url!)
request.setValue("YourUserAgent/1.0", forHTTPHeaderField: "User-Agent")
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()) {(response, data, error) in
let content = NSString(data: data, encoding: NSUTF8StringEncoding)
self.webView!.loadHTMLString(content!, baseURL: url)
}
It wouldn't be ideal but you could likely implement a custom NSURLProtocol handler to intercept HTTP requests and modify them with your custom user-agent header. I don't think this would work on iOS since WKWebView makes requests out-of-process and bypasses any registered NSURLProtocols. But it might work on OS X?
With a user entered URL in a text field, you can completely control the NSURLRequest using an NSMutableURLRequest object, and set the header field for it.
However, with things the user actually clicks on within the web view, you're kind of not in control from obvious and clearly documented Objective-C land.
I do not see any documented way beyond what WKWebView seems to push things toward, JavaScript. So, that means you can do things like posted here:
Mocking a useragent in javascript?
Using the script injection APIs.
This is why I do not like WKWebView. I want to like it, but I do not want to learn to do half of everything in JavaScript.
So, you can create a WKUserScript object to do this, and set its injection time to WKUserScriptInjectionTimeAtDocumentStart. That will enable you to handle things requested from page elements within the document, as long as the page itself is not loading other scripts that conflict.
First Quit Safari. Then open Terminal and paste this command, and press enter:
defaults write com.apple.Safari CustomUserAgent "\"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_0) AppleWebKit/538.46 (KHTML, like Gecko) Version/7.1 Safari/537.85.7\""
Open Safari and you're done.
To undo this change, close Safari and paste this command into terminal:
defaults delete com.apple.Safari CustomUserAgent
Sometimes a restart may be required to get these changes to stick, not sure why, could be a cache thing.

how to get the current tab url in popover for safari extension

Hi There best collegue,
Currently im busy with developing a bookmarker extension for safari and running up against a problem. How can i acces the current tab url in the popover?
Tnx in advance:)
The popover doesn't have access to safari.application so can't get the current URL directly, but it can call functions in the global page which can.
In your global page:
function currentUrl() {
return safari.application.activeBrowserWindow.activeTab.url;
}
Then in the popover:
alert(safari.extension.globalPage.contentWindow.currentUrl());
Ensure that the Access Level is set to 'All' in the Extension builder and secure pages are included otherwise the URL will sometimes be undefined.

Cocoa WebView On Navigate?

I am working on a Cocoa WebView based browser application.
Basically, I would like to have it so when the user navigates to a url, for example:
http://a/b.php, by clicking on a link on the previous page of a website, my application intercepts the "on navigate" event and changes the URL to http://a/b.php?enableapi=1
Any ideas??
Thanks in advance.
You need to assign an object as the web view's WebPolicyDelegate and implement the webView:decidePolicyForNavigationAction:request:frame:decisionListener: method.
In that method, you must call one of the WebPolicyDecisionListener protocol methods on the object that is passed as the decisionlistener parameter to the method. The three WebPolicyDecisionListener protocol methods are ignore, use or download.
You can then pass ignore to the listener and handle the link some other way for those links you're interested in intercepting.

Resources