5

In my iPhone app an UITextView is containing an URL. I want to open this URL in an UIWebView instead of opening it into safari? My UITextView contains some data along with an URL. In some cases the no. of URLs can be more than one.

Thanks Sandy

sandy
  • 2,127
  • 4
  • 28
  • 50

3 Answers3

11

You can follow these steps:

  1. Tick on the following properties in UITextView taken from Xib or Storyboard.

Check these properties of UITextView

OR write these for textview taken dynamically.

textview.delegate=self;
textview.selectable=YES;
textView.dataDetectorTypes = UIDataDetectorTypeLink;
  1. Now write the below delegate method :
-(BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange
{
 NSLog(@"URL: %@", URL);
//You can do anything with the URL here (like open in other web view).
    return NO;
}

I think you are searching for that.

Community
  • 1
  • 1
Manab Kumar Mal
  • 20,788
  • 5
  • 31
  • 43
9

The UITextView has the capability to detect URLs and embed hyperlinks accordingly. You can turn that option on in:

myTextView.dataDetectorTypes = UIDataDetectorTypeLink;

Then you need to configure your app to trap this URL request and let your application handle it. I published a boilerplate class on github that does this, which might be the easiest route: http://github.com/nbuggia/Browser-View-Controller--iPhone-.

The first step is to sub-class UIApplication so you can override who gets to take action on the 'openUrl' request. Here's what that class might look like:

#import <UIKit/UIKit.h>
#import "MyAppDelegate.h"

@interface MyApplication : UIApplication

-(BOOL)openURL:(NSURL *)url;

@end


@implementation MyApplication

-(BOOL)openURL:(NSURL *)url 
{
    BOOL couldWeOpenUrl = NO;

    NSString* scheme = [url.scheme lowercaseString];
    if([scheme compare:@"http"] == NSOrderedSame 
        || [scheme compare:@"https"] == NSOrderedSame)
    {
        // TODO - Update the cast below with the name of your AppDelegate
        couldWeOpenUrl = [(MyAppDelegate*)self.delegate openURL:url];
    }

    if(!couldWeOpenUrl)
    {
        return [super openURL:url];
    }
    else
    {
        return YES;
    }
}


@end

Next, you need to update main.m to specify MyApplication.h as being the bonified delegate for your UIApplication class. Open main.m and change this line:

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

to this

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

Finally, you need to implement the [(MyAppDelegate*) openURL:url] method to have it do what ever you would like with the URL. Like maybe open up a new view controller with a UIWebView in it, and show the URL. You could do something like this:

- (BOOL)openURL:(NSURL*)url
{
    BrowserViewController *bvc = [[BrowserViewController alloc] initWithUrls:url];
    [self.navigationController pushViewController:bvc animated:YES];
    [bvc release];

    return YES;
}

Hopefully that should work for you.

Nathan Buggia
  • 924
  • 9
  • 11
  • very detail. Going to try. Thanks a lot – Cullen SUN Nov 16 '11 at 14:14
  • Really useful, thanks. But how can you manage different views implementing hyperlinks? I mean, in your example, in the AppDelegate you implement: `BrowserViewController *bvc = [[BrowserViewController alloc] initWithUrls:url]; [self.navigationController pushViewController:bvc animated:YES];` because there is only one view. But what if you call the `openURL` method from different views in the app??? – yassassin Jan 08 '12 at 13:38
  • @yassassin - the scope of the AppDelegate is for the whole application, so the override will be available everywhere. There is an overload to the method that allows you to override this behavior if you want to still be able to open some links in safari: 'forceOpenInSafari:(BOOL)forceOpenInSafari'. – Nathan Buggia Dec 23 '12 at 22:47
3

Assuming you have the following instances, that are also added to your UIView:

UITextView *textView;
UIWebView *webView;

and textView contains the URL string, you can load the contents of the URL into webView, as follows:

NSURL *url = [NSURL URLWithString:textView.text];
NSURLRequest *req = [NSURLRequest requestWithURL:url];
[webView loadRequest:req];
pythonquick
  • 10,789
  • 6
  • 33
  • 28
  • Thanks for the response pythonquick but I am facing a different situation in my case UITextView does contains some text data along with an URL in some cases the number of URLs can be more than one. – sandy Oct 13 '09 at 09:11
  • OK, so if you have more than one URL within the UITextView's text, what should happen to the UIWebView? In which circumstances should the UIWebView load which URL? – pythonquick Oct 13 '09 at 16:30