Friday, April 26, 2013

How to open a UITextView URL in UI Web View http://stackoverflow.com/questions/1555294/how-to-open-a-uitextview-url-in-ui-web-view

http://stackoverflow.com/questions/1555294/how-to-open-a-uitextview-url-in-ui-web-view

How to open a UITextView URL in UI Web View

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

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];

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.

No comments:

Post a Comment