I need to display webpage using URL. From that page I want to remove one link control from that html page. Is it possible? I am opening it with this kind of code.
NSString *urlAddress = #"http://myurl.com";
NSURL *url = [NSURL URLWithString:urlAddress];
If you want to disable ALL links, see the answer from user3182143...
If you only want to disable a specific link, you can still use those methods - you will just need to evaluate the request part of shouldStartLoadWithRequest:(NSURLRequest *)request and decide whether to return YES or NO
If you want a specific link to look different, you'll need to modify the html. Couple ways to do so, but we really need more information about exactly what you are trying to do.
Are you displaying ONE specific web page? And all you need to do is change ONE link in that ONE page, and you're done? Are you displaying a series of pages? Do you have any control of the pages? And so on.
I'd suggest you start searching for and reading about uiwebview insert html or uiwebview insert javascript
We can do this.
First we should know about the UIWebViewDelegate methods
First create BOOL
ViewController.h
#property (nonatomic) BOOL ret;
ViewController.m
#synthesize ret;
Set BOOL to NO in below delagte method
- (void)webViewDidFinishLoad:(UIWebView*)webView {
// Sent after a web view finishes loading a frame.
ret = NO;
}
Then If we return NO in below delegate method, this will prevent the user from viewing link
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
return ret:
}
Dave DeLong's Disable Link in UIWebView answer
Removing hyper links from a URL shown in UIWebView
Related
I'm sort of new to Xcode, so forgive me in advance for any obvious wrong things that I might write.
I'm trying to write something a bit simple using the UIWebView. I've already made it load upon the app's loading, but I can't seem to do anything else. What I want to do next is to make a button appear/disappear depending on the current URL. This is what I used to (try to) get the current URL:
NSString *currentURL = viewWeb.request.URL.absoluteString;
(I'm using this code in the ViewController.h file)
When I made the outlet (Ctrl+dragging), I named it viewWeb and I also went and labeled it viewWeb. But it doesn't seem to work and I don't know why.
Also, please don't just give me some code without explaining, because this is a bit frustrating and I want to understand it.
EDIT: Thanks, but I'm not looking for help on the disappearing button just yet (by the way, viewWeb is my UIWebView, not the button). I need help to detect an URL change to make the button disappear. Is there a webViewDidLoad or something similar? viewDidLoad isn't for this.
In the ViewController.h File, you only declare the property, like this:
#property (strong,nonatomic) NSString *currentURL;
Then you can set the value in the ViewController.m file.
maybe in the -viewDidLoad method in the ViewController.m file like this:
_currentURL = viewWeb.request.URL.absoluteString;
You can set the property of hidden or not of the button to show it selectively based on the url.
// URLString contains the URL based on which you would like to show/hide the button
if ([currentURL isEqualToString:URLString]) {
viewWeb.hidden = NO;
}
else {
viewWeb.hidden = YES;
}
I am currently embedding a UIWebView inside a UICollectionViewCell to be able to view HTML formatted text residing in my local data structure.
I know it has been stated many times one should not embed an UIWebView inside a scrollView, but i currently see no other way to achieve displaying mixed content overviews (That is the current requirement, and so far UICollectionView does work very well with large data sets).
The Problem is, that i am not able to call the UIWebViews Delegate method:
- (BOOL)webView:(UIWebView *)webView
shouldStartLoadWithRequest:(NSURLRequest *)request
navigationType:(UIWebViewNavigationType)navigationType { ... }
I need the delegate method to be able to display external links or call local methods.
Is there a way to achieve this? Maybe by handing over the events programatically?
Thanks for the feedback!
I just found out what i did wrong:
I just needed to update the cells' contentView: [[cell contentView] setFrame:frameContainingWebViewSize]
That did the trick. The delegate method was not called because the contentView did not cover the webview (clipSubviews is set to NO) and thus the tap events where not fired.
First thanks in advance to have a look on my issue. My issue is my am developing webview based application. It's same like as browser but i have a issue related to reload web view. When i am getting a value form popover bookmarked and try to reload the current web view from this next bookmark url, my web view do noting. I am stuck in this issue. Although when i enter any url through it works perfectly. May i doing something wrong with web view ?
temp = appDeleg.strURLFromPopover;
NSURL *url = [NSURL URLWithString:temp];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[self.myWebView loadRequest:requestObj];
[self.myWebView setDelegate:self];
[self.view addSubview:myWebView];
Please have a look..
The last line looks like very suspicious. In my experience, [self.view addSubview:myWebView] should be placed in the "initialization block" to avoid some lazy init issues. Are you sure All objects are NOT nil? if so, you can try to set UIWebView's delegate to your class, and debug in each method to test if your object are properly initialized. I tried your code, work fine in my side. Below are delegate method you might consider about
– webView:shouldStartLoadWithRequest:navigationType:
– webViewDidStartLoad:
– webViewDidFinishLoad:
– webView:didFailLoadWithError:
I'm having this problem in my iPhone app: I have a webView which is first loaded with an HTML string. After the user clicks a link and it loads the requested page, the webView won't go back when I call the method [UIWebView goBack]; I suppose webView doesn't cache the HTML string. Is there any way I can make webView cache my HTML string without having to save it in a NSString myself?
You can use the canGoBack property and if you can't go back, reload the UIWebView with the original html. If the user has navigated forward using links then the canGoBack property will return YES and a goBack can be initiated on the UIWebView. The _htmlString is a member variable that is set when the UIWebView is initialized using an HTML string. -rrh
- (void)goBack
{
if (_htmlString && ![_browserWebView canGoBack]) {
[_browserWebView loadHTMLString:_htmlString baseURL:nil];
return;
}
[_browserWebView goBack];
}
Try to create a NSURLRequest from the file URL and use loadRequest instead of loadhtmlString
NSURL *htmlFileUrl = [[NSBundle mainBundle] URLForResource:#"index" withExtension:#"html"];
NSURLRequest *localRequest = [NSURLRequest requestWithURL:htmlFileUrl];
self.webView.delegate = self;
[self.webView loadRequest:localRequest];
This sounds like a your webview variable isn't properly linked up to the webview instance that you are using. Breakpoint at this call and check whether your webView variable is 'nil'.
If it is, make sure your webView in your XIB file is linked to an IBOutlet variable in Interface Builder. This is a common mistake and something I tend to forget when designing a new page for the first time.
This tutorial covers a LOT on how to build interfaces using Interface builder which i'm sure you're familliar with but for those that aren't it's also useful. It has some good screenshots which help illustrate what I mean by 'linking' better than me typing "click the little + icon and drag the little thingy on to the UI element" :)
http://www.icodeblog.com/2008/07/30/iphone-programming-tutorial-connecting-code-to-an-interface-builder-view/
EDIT
The only other thing i can think of is that you are overwriting your webView variable by re-initialising it somewhere (it is already initialised by the XIB) and therefore you're calling goBack on a webview that doesn't exist on the screen.
I am wondering if there is something already like this out there. But I basically want something like UITextView to display text with embedded links. However, I want to be able to handle the URL clicks as a delegate.
I've read a few posts like the following:
How to intercept click on link in UITextView?
However, I really don't want to override the openURL method. My app works with lots of webServer data, and I don't want to keep creating exceptions for different hosts in the openURL method.
I guess my questions is, is there another way to intercept the click on UITextView?
My alternative is to write my own, with a UIScrollView, and use a TTTAttributedLabel (https://github.com/mattt/TTTAttributedLabel) inside it. But am looking for suggestions, or alternatives.
Thanks.
You may use UIWebView + Local HTML instead of UITextView.
And use the -(BOOL )webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType )navigationType delegate to handle the URL clicks,like:
-(BOOL )webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType )navigationType
{
NSURL * clickedURL=[request URL];
//Do something here.
return NO;
}