All,
I have rich text, with links, in my UITableView's detail View:
When a link is pressed, the URL is loaded into the UIWebView where the rich text is being displayed. I'd like the URLs to open in Safari instead.
Can someone tell me how to accomplish this correctly? Is there a UIWebView delegate method suited for this purpose?
Thanks
// where url is an NSURL
[[UIApplication sharedApplication] openURL:url];
This will open the link in the default OS browser.
Documentation here.
Related
I just wanted to implement a feature like safari for new tab. I don't worry about session right now. As I am using UIWebView, need detect the clicked link/button/url from the website needs to opened in new tab.
I have tried following questions, but none of these working as expected. Everything is opening in the same WebView.
UIWebView detect when javascript is loading a new page
Opening popup links in UIWebView, possible?
Any help, much appreciated.
May use UIWebView's delegate to detect events
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
The thing is that I'm rather new to Objective-C and have been reading about it now for a few days online. I started to create a simple iOS application but I didn't find anything about this when I Googled it.
Now I want to create a link that is not plain text, but an image.
If this is incomprehensible, then this is how I would do it in HTML:
<img src="IMAGE.png"/>
So far in my ViewController.h, I have:
-(IBAction)link;
And in my ViewController.m, I have:
-(IBAction)link {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"http://www.google.com/"]];
}
How would I link this URL to an image?
Cheers.
There's a simple way to do that. You can add a button on top of the image and make the button send actions to the view controller, or you can link it to -(IBAction)link; directly.
Add a UIButton in IB/Storyboard to your view.
Make that button of the same size that your image is.
Assing the image as the buttons image or background image.
Connect the touch up inside event with the IBAction link.
Or do you want to do that programmatically?
I am having an issue similiar to this SO question. I would like to have a webview in an NSSheet in which I am doing some authentication to retrieve an API token.
I created a new NSWindowController subclass with a corresponding xib file. This is how I am starting the NSSheet:
- (IBAction)startAuthentication:(NSButton *)sender {
self.authController = [[AuthenticationWindowController alloc] initWithWindowNibName:#"AuthenticationWindow"];
[[NSApplication sharedApplication] beginSheet:self.authController.window
modalForWindow:[self.exportManager window]
modalDelegate:self
didEndSelector:#selector(sheetDidEnd:returnCode:contextInfo:)
contextInfo:nil];
}
In the windowDidLoad callback of this authenticationWindowController I am directing the webview to the URL:
- (void)windowDidLoad
{
[super windowDidLoad];
[[self.webView mainFrame]loadRequest:[NSURLRequest requestWithURL:authURL]];
}
It looks great at first sight: if I press the button, the startAuthentication action method is called and the new window is animated into the parent window and the authURL is loaded. The website is displayed correctly with its HTML form containing two input fields (username and password).
The problem is, that I can click on the page and it works, however if I am trying to click into one textfield, so that this field gets focus it does not work. No cursor appears in the text field of the website's form and after each keystroke I hear the NSBeep() sound.
I did some research on this topic and I found two references:
How do I use a WebView in a modal dialog?
Cocoa topics: the case of the modal WebView
What kind of puzzles me is that there is the Facebook Exporter for Aperture Plugin, which shows exactly what I want: a webview in a modal sheet. However I cannot find out what they are making differently . In the Facebook Exporter I have not found any code interacting with the run loops directly.
My questions
Is this a known problem with webviews in modal sheets?
Are the problems explained in the two references still there?
How can I get this working? I do not understand when to switch the runloop mode for example.
there is no general problem with this and I just tried it out again. must be some code besides this? can you narrow it down to the sheet? btw: my sample:
https://dl.dropbox.com/u/3753090/test2.zip
BUT all that said, modal runmode and webviews / timers / networking isnt very waterproof :)
because: when you go modal via one of the convenience methods in NSApp, the runloop is only run in a very limited way. :/
Is it possible intercept when a user clicks a link in a UIWebView and retrieve the link?
I wish to block the UIWebView from going to the clicked link and do something else.
Thanks in advance.
Yes, look at the UIWebViewDelegate Protocol Reference, specifically webView:shouldStartLoadWithRequest:navigationType:
The NSURLRequest will let you know where the link they clicked is going to direct them.
You would simply return NO on this method if you want to prevent the request from loading.
I want to restrict the users from using the links in a UIWebView. Hence, showing only single a HTML page with no navigation.
I know I can do that using the "User Interaction" option of UIWebView but I can't use that since it disables scrolling as well, and the user cannot view the complete page.
Any ideas?
My situation demanded that a user really can ONLY look at the page, but not interact at all (specifically with respect to copying content--it was a security issue). Here's what I did:
for (UIView *thisView in webView.scrollView.subviews)
{
thisView.userInteractionEnabled = NO;
}
If you don't mind them seeing the links, you could use a UIWebViewDelegate to detect when a page begins loading and cancel it.
If you don't want them to see the links at all, you could modify the HTML before rendering it. Using libxml is pretty easy's and it's htmlparser.h can probably do what you need. If you don't like that, HTML tidy works well too.
You can wrap the WKWebView in a UIView and then set webViewWrapper.isUserInteractionEnabled = false
to disable navigation with links
func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
webView.stopLoading()
}