Skip to main content

How to intercept click on link in UITextView?



Is it possible to perform custom action when user touch autodetected phone link in UITextView. Please do not advice to use UIWebView instead.





And please don't just repeat text from apple classes reference - certainly I've already read it.





Thanks.


Comments

  1. A nice way to do this is to by subclassing UIApplication and ovewriting the -(BOOL)openURL:(NSURL *)url

    @interface MyApplication : UIApplication {

    }

    @end

    @implementation MyApplication


    -(BOOL)openURL:(NSURL *)url{
    if ([self.delegate openURL:url])
    return YES;
    else
    return [super openURL:url];
    }
    @end


    You will need to implement openURL: in your delegate.

    Now, to have the application start with your new subclass of UIApplication, locate the file main.m in your project. In this small file that bootstraps your app, there is usually this line:

    int retVal = UIApplicationMain(argc, argv, nil, nil);


    The third parameter is the class name for your application. So, replacing this line for:

    int retVal = UIApplicationMain(argc, argv, @"MyApplication", nil);


    This did the trick for me.

    ReplyDelete
  2. I haven't tried that myself but you can try to implement application:handleOpenURL: method in your application delegate - it looks like all openURL request pass through this callback.

    ReplyDelete
  3. Not sure how you would intercept the detected data link, or what type of function you need to run. But you may be able to utilize the didBeginEditing TextField method to run a test/scan through the textfield if you know what your looking for..such as comparing text strings that meet ###-###-#### format, or begin with "www." to grab those fields, but you would need to write a little code to sniff through the textfields string, reconize what you need, and then extract it for your function's use. I don't think this would be that difficult, once you narrowed down exactly what it is that you wanted and then focussed your if() statement filters down to very specific matching pattern of what you needed.

    Of couse this implies that the user is going to touch the textbox in order to activate the didBeginEditing(). If that is not the type of user interaction you were looking for you could just use a trigger Timer, that starts on ViewDidAppear() or other based on need and runs through the textfields string, then at the end of you run through the textfield string methods that you built, you just turn the Timer back off.

    ReplyDelete
  4. application:handleOpenURL: is called when another app opens your app by opening a URL with a scheme your app supports. It's not called when your app begins opening a URL.

    I think the only way to do what Vladimir wants is to use a UIWebView instead of a UITextView. Make your view controller implement UIWebViewDelegate, set the UIWebView's delegate to the view controller, and in the view controller implement webView:shouldStartLoadWithRequest:navigationType: to open [request URL] in a view instead of quitting your app and opening it in Mobile Safari.

    ReplyDelete
  5. Instead of extending UIApplication you can just block the userInteraction of the UITextView

    This is working for me.

    ReplyDelete

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.