I am showing an URL in my uiwebview and it is working properly.
So the web page actually contains some Javascript functions (fun1, fun2, fun3...) which is getting executed once I select some portion over webview.
So my requirement is to detect the Javascript method which is being executed on selection over webview.
So I have implemented UITapGestureRecognizer over webview.
[myWebView addGestureRecognizer:tapGestureRecognizer];
Now once I select on the webview my following method is called:
-(void)singleTap:(UIGestureRecognizer *)gestureRecognizer
{
[myWebView stringByEvaluatingJavaScriptFromString:#"function getItem(){ var functionName =arguments.callee.name; alert(functionName);}getItem()"];
}
But I am not getting the Javascript method which is executed, how can I get this?
Note : Somewhere I have read that arguments.callee.name; has been deprecated, so is that the problem?
You need to modify the JavaScript on the remote webpage to set a global variable with the currently running function's name, like so:
var CURRENT_FUNCTION = '';
function someFunction() {
CURRENT_FUNCTION = 'someFunction()';
}
Then from the iOS side you can accomplish this by changing your code to say:
NSString *currentFunction = [myWebView stringByEvaluatingJavaScriptFromString:#"return CURRENT_FUNCTION"];
Does that make sense?
Related
I am working on developing iOS application using Xamarin. I have a
requirement to call c# method from JavaScript inside UIWebView. How could we achieve that?
The following is html content is loading into UIWebView
const string html = #"
<html>
<body onload=""setBarcodeInTextField()"">
<p>Demo calling C# from JavaScript</p>
<button type=""button""
onClick=""CSharp.ScanBarcode('txtBarcode', 'setBarcodeInTextField')"">Scan Barcode
</button>
<input type=""text"" value="""" id=""txtBarcode""/>
<script type=""text/javascript"">
function setBarcodeInTextField() {
alert(""JS"");
}
</script>
</body>
</html>";
Also, i am getting about://(null) in alert message (onload specified on body tag for displaying alert) when UIWebView loads the html content.
One solution to trigger C# method from website shown in WebView compontent is to:
1) Initialize a navigation to a website in your web code, for example
http://scancode/providedBarCode
2) Then you can override a webview method which is called before navigation actually happens. You can intercept a call there with parameters and ignore the actual navigation
Xamarin Forms (Navigating method of WebView)
webview.Navigating += (s, e) =>
{
if (e.Url.StartsWith("http://scancode/"))
{
var parameter = e.Url.Split(new[] { "scancode/" }, StringSplitOptions.None)[1];
// parameter will be providedBarCode
// perform your logic here
// cancel the actual navigation
e.Cancel = true;
}
};
Xamarin iOS (ShouldStartLoad method of UIWebView)
webViewControl.ShouldStartLoad = (webView, request, navType) =>
{
var path = request.Url.AbsoluteString;
if (path.StartsWith("http://scancode/"))
{
var parameter = path.Split(new[] { "scancode/" }, StringSplitOptions.None)[1];
// parameter will be providedBarCode
// perform your logic here
// cancel the actual navigation
return false;
}
return true;
}
I have a edit photo php script that i want to load in my iOS app. I am calling it through WebView "www.example/com/myTool". When the edit task is complete the webView is moving to homepage of the website. I want when the Edit task is complete then instead of redirecting url to www.example.com/home the webView closes and it moves back to View Controller.
What i can think of solution is - when the URL of webView changes then the webView should close and move back to view controller .. But i don't know how to apply it in code.
import UIKit
class AddPostVC: UIViewController {
#IBOutlet weak var addPost: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
addPost.loadRequest(URLRequest(url: URL(string: "https://new.example.com/pentool")!))
}
}
You need to implement UIWebViewDelegate methods.
Before loading a URL, UIWebView will call, (webView: shouldStartLoadWith request) delegate method.
Compare the URL in request property with your home page URL. If yes, close the web view.
Hope this helps :)
You could do this
NSString *currentURL = [webView stringByEvaluatingJavaScriptFromString:#"window.location"];
This will get you the url of the current page. Or this
currentURL = currentWebView.request.URL.absoluteString
Credit to https://stackoverflow.com/questions/2491410/get-current-url-of-uiwebview/3654403#3654403
I have a UIView with two buttons on it. In the MyView class I have this code:
-(BOOL) canBecomeFocused {
return YES;
}
-(NSArray<id<UIFocusEnvironment>> *)preferredFocusEnvironments {
return #[_editButton, _addButton];
}
-(IBAction) editTapped:(id) sender {
BOOL editing = !tableViewController.editing;
[_editButton setTitle:editing ? #"Done" : #"Edit" forState:UIControlStateNormal];
_addButton.hidden = !editing;
[tableViewController setEditing:editing animated:YES];
}
The basic idea is that the user can move the focus to the edit button, which can then make the Add button appear.
The problem started because every time I tapped the edit button, focus would shift to the table view. I would actually like it to move to the Add button. I also want it so that when editing it deactivated, the edit button keeps the focus. but again it's shifting down to the table view.
So I tried the above code. This works in that focus can move to the view and on to the button. But once it's there, I cannot get it to move anywhere else.
Everything I've read says just override preferredFocusEnvironments but so far I've not been able to get this to work. Focus keeps going to a button then refusing to move anywhere else.
Any ideas?
If anybody is facing this issue, Just check if you are getting the following debug message printed in the console.
WARNING: Calling updateFocusIfNeeded while a focus update is in progress. This call will be ignored.
I had the following code :
// MARK: - Focus Environment
var viewToBeFocused: UIView?
func updateFocus() {
setNeedsFocusUpdate()
updateFocusIfNeeded()
}
override var preferredFocusEnvironments: [UIFocusEnvironment] {
if let viewToBeFocused = self.viewToBeFocused {
self.viewToBeFocused = nil
return [viewToBeFocused]
}
return super.preferredFocusEnvironments
}
I was calling the updateFocus() method multiple times while viewToBeFocused was either nil or some other view. Debugging the focus issues mainly between transition is really difficult. You should have patience.
Important to note: This depends on your use case, but if you want to
update the focus right after a viewcontroller transition (backward
navigation), You might have to set the following in viewDidLoad:
restoresFocusAfterTransition = false // default is true
If this is true, the view controller will have the tendancy to focus the last focused view even if we force the focus update by calling updateFocusIfNeeded(). In this case , since a focus update is already in process, you will get the warning as mentioned before at the top of this answer.
Debug focus issue
Use the following link to debug the focus issues: https://developer.apple.com/documentation/uikit/focus_interactions/debugging_focus_issues_in_your_app
Enable the focus debugger first under Edit scheme > Arguments passed on launch:
-UIFocusLoggingEnabled YES
This will log all the attempts made by the focus engine to update the focus. This is really helpful.
You can override the preferredFocusEnviromnets with the following logic:
-(NSArray<id<UIFocusEnvironment>> *)preferredFocusEnvironments {
if (condition) {
return #[_editButton];
}
else {
return #[_addButton];
}
}
After setting it, you can call
[_myView setNeedsFocusUpdate];
[_myView updateFocusIfNeeded];
The condition could be BOOL condition = tableViewController.editing; or sg like that.
If that now works, you can call it with a delay (0.1 sec or so).
I got an application that serves widgets inside iframes of other websites. So far so good but how can I allow these widgets views only to be loaded inside an iframe and not directly?
This should work
<iframe src="http://www.example.com/widgets/example">
But typing in http://www.example.com/widgets/example directly into a browser shouldn't be allowed.
What is or is there a best way to achieve this in rails?
You need first to check if your page window is the same as parent window if not then your page inside an iframe:
function inIframe () {
try {
return window.self !== window.top;
} catch (e) {
return true;
}
}
Then if not you can remove everything in DOM:
// Pure JS something like
var myNode = document.getElementById("foo");
myNode.innerHTML = '';
// jQuery
$('html').empty();
OR you can redirect to an empty page that say its not allowed to be viewed outside of iframe:
window.location = "http://www.yourul.com/empty_page";
Referring to
Using a RTLabel, I show eficently this html string:
Example Street, 1
as: Example Street, 1. Its clickable, but no action is done. Any RTLabel user Knows how active an action?
Or if there is some altrnative to open in safari that link (in this case to show a map).
Thanks in advance
Set the RTLabel delegate as:
[rtlabel setDelegate:self];
On tapping the link, the below function gets called. Handle accordingly:
-(void)rtLabel:(id)rtLabel didSelectLinkWithURL:(NSURL*)url
{
RTLabel * label = (RTLabel*)rtLabel;
[self openBrowserViewForURL:[url absoluteString] pageTitle:label.text];
}