How to get the word tapped on in a UILabel - ios

In an app I'm writing I think it would be best if the user can tap on a UILabel and I can see what word they tapped on. Is there anyway I can do this?

you can do it use a UIWebview
use
- (void)loadHTMLString:(NSString *)string baseURL:(NSURL *)baseURL;
and code the whole message then wrap the link to
[webView.loadHTMLString:#"Google" baseURL:nil];
then in the delegate method
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
you will know which word you tapped
if ( [request.URL.scheme compare:FeverProtocolIdentifier] == NSOrderedSame )
{
NSString *word = request.URL.resourceSpecifier;
}

I don't think this is very easy. I would probably use an OHAttributedLabel rather than a UILabel and add a method to the OHAttributedLabel code to return the information you need. If you look at the linkAtPoint method in OHAttributedLabel.m, you can see what you'll need to do. I would duplicate that method, naming the new one characterOffsetAtPoint perhaps, and then return the index that the existing code calculates at line 340.

Related

how do i call objective c code from javascript

i've some javascript code on the webpage in the uiwebview that i want to use to call one of my objective c methods.
i found some code online which i decided to use. but it still doesn't seem to be working. can anyone see where the problem is?
javascript code:
function someMethod() {
window.location = "ios:webToNativeCall";
}
objective c code:
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
if ([[[request URL] absoluteString] hasPrefix:#"ios:"]) {
// Call the given selector
[self performSelector:#selector(webToNativeCall)];
// Cancel the location change
return NO;
}
return YES;
}
-(void)webToNativeCall
{
//my code here
}
i'm not sure, how to use this method so it might be that i have implemented it incorrectly.
does anyone have any ideas about what could be causing this?
Thanks in advanced.
This code looks ok, please check whether delegate for UIWebView is set or not.
Otherwise you can use EasyJSWebView download it from Github, it is easy to use.
You must have missed to link the delegate.
Either connect the delegate of the webView to the file owner in the .xib file
or
Use Following code
webView = [[UIWebView alloc] init];
webView.delegate = self;
in your viewDidLoad also write below code
[webView stringByEvaluatingJavaScriptFromString:#"yourJavascriptFunction()"];
Hope it helps you...

Use UIWebViewNavigationTypeLinkClicked Filter

I want to store the clicked URL by UIWebViewNavigationTypeLinkClicked. However I just want to store the clicked URL not a series of URLs after clicking in some URLs with ads. Anyway to deal with this?
When you clicked inside webview below delegate will called you need to implement below delegate method then on the basis of this you can check that url and store in an array accordingly-
- (BOOL)webView:(UIWebView *)webView
shouldStartLoadWithRequest:(NSURLRequest
*)request navigationType:
(UIWebViewNavigationType)navigationType
{
//here you can check the condition on the basis
 of navigation type
if (navigationType==
UIWebViewNavigationTypeLinkClicked)
{ //Store the link in array below
// initialized you array somewhere first and then
use below
[mutableArray addObject:yourUrl];
}
}

iOS: Block Other Domains in UIWebView

So I'm writing my first app, and have a quick question about blocking other domains in UIWebView. I would like to restrict navigation to my website, and my partner website only. So, if there happens to be a link on my site that links to Google, I'd like for a popup message to occur and say "You can't go there!" or something. I'm not sure how to go about this. I've never played with UIWebView before and am pretty new to programming in general.
Any help is greately appreciated, thanks!
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSString *host = [request.URL host];
if ([host isEqualToString:"oralb.com"] || [host isEqualToString:"other.com"]) {
// Add any of your own domains in the above line
return YES;
}
return NO;
}
You need to make your controller class the delegate of the web view. Then implement this method in the controller:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
In this method you can examine the request to see if its URL is acceptable. If it is, return YES, if not, show your alert and return NO.
To test the domain you can request the host from the URL of the request.

Disable UIWebView hyperlinks

My goal is to allow UIWebView load initial link, but disallow any further navigation.
I saw question:
Disable hyperlinks in UIWebView
So, I wired referenced webView property from interface builder.
I specified that my UIViewController uses UIWebViewDelegate:
#interface LegalViewController : UIViewController<UIWebViewDelegate>
In the code, on viewDidLoad, I do following:
mFirstLoad = TRUE;
webView.delegate = self;
And I have following code:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
if (!mFirstLoad)
return FALSE;
mFirstLoad = FALSE;
return TRUE;
}
shouldStartLoadWithRequest is called both initial call and when I click on the link. However, even if shouldStartLoadWithRequest returns FALSE, UIWebView still proceeds and loads a new page.
I see this problem on iOS Simulator 6.0
How can I fix this behavior?
return NO based on type to exclude links
return !(navigationTpe==UIWebViewNavigationTypeLinkClicked);
Is it possible your boolean is getting reset somehow? What happens if you just return NO for (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType?
Here is the same thing which I wrote in comments:
I think I found the reason for the problem. I am not sure how to properly call it (not a big expert on HTML and Javascript). However the link has following code : "<a href="/something" data-transition="flip"</a> As result, callback is called, but it looks like results of this callback is ignored.
As result most likely solution is here: Remove hyperlinks in uiwebview

How to retrieve a clicked link in iOS

For security reasons, I used to use an anonymizer on mobile safari. However It was a bit annoying. If I searched for something and clicked on a link, then google returns an error message. You can try.. http://proxy2974.my-addr.org/myaddrproxy.php/http/www.google.com.au/ (search for something and click on a link -- it works fine if you test it on desktop browsers.. however it will return http://proxy2974.my-addr.org/myaddrproxy.php/http/url if you try it on iPhone or iPhone simulator)
So I decided to make my own. What I am doing is to get the URL from the text field and pass it through the anonymizer's link like urlString = [NSString stringWithFormat:#"http://proxy2974.my-addr.org/myaddrproxy.php/http/%#", urlString];
However I am still facing the same problem.. When I click on a link on google, it returns an error.. So what I want to do is to get the clicked link, stop loading the page (before it returns an error), then pass it through the anonymizer.. How can I do that? Thanks..
- (BOOL)webView:(UIWebView *) sender shouldStartLoadWithRequest:(NSURLRequest *) request navigationType:(UIWebViewNavigationType) navigationType {
NSLog(#"req: %#",request.URL.absoluteString);
return YES;
}
req: http://proxy2974.my-addr.org/url?sa=t&source=web&cd=1&ved=0CFMQFjAA&url=%2Fmyaddrproxy.php%2Fhttp%2Fwww.perthnow.com.au%2Ffun-games%2Fleft-brain-vs-right-brain%2Fstory-e6frg46u-1111114517613&ei=GXAsUMaUA7H44QTys4HYDA&usg=AFQjCNGB_zOrrEZC0SKx813XGHB1xi_AlA
req: http://proxy2974.my-addr.org/myaddrproxy.php?proxy_url_sjla67z78f8viz4=url&sa=t&source=web&cd=1&ved=0CFMQFjAA&url=%2Fmyaddrproxy.php%2Fhttp%2Fwww.perthnow.com.au%2Ffun-games%2Fleft-brain-vs-right-brain%2Fstory-e6frg46u-1111114517613&ei=GXAsUMaUA7H44QTys4HYDA&usg=AFQjCNGB_zOrrEZC0SKx813XGHB1xi_AlA
req: http://proxy2974.my-addr.org/myaddrproxy.php/http/url
Implement this delegate method to retrieve a clicked link in UIWebView:
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
{
if(navigationType == UIWebViewNavigationTypeLinkClicked)
{
NSLog(#"req: %#",request.URL.absoluteString);
}
}
implement
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType;
in your uiwebview delegate

Resources