How to open a UITextView URL in a web UI view?

In my iPhone application, the UITextView contains the url. I want to open this url in a UIWebView and not open it in safari? My UITextView contains some data along with the url. In some cases, no. There can be more than one URL.

Thanks Sandy

+5
source share
3 answers

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 the webView, as shown below:

NSURL *url = [NSURL URLWithString:textView.text];
NSURLRequest *req = [NSURLRequest requestWithURL:url];
[webView loadRequest:req];
+3
source

You can do the following:

  • UITextView, Xib Storyboard.

Check these properties of UITextView

, .

textview.delegate=self;
textview.selectable=YES;
textView.dataDetectorTypes = UIDataDetectorTypeLink;
  1. delegate:
-(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;
}

, .

+10

UITextView URL- . :

myTextView.dataDetectorTypes = UIDataDetectorTypeLink;

, URL-, . catplate github, , : http://github.com/nbuggia/Browser-View-Controller--iPhone-.

UIApplication, , openUrl. :

#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

main.m, MyApplication.h UIApplication. main.m :

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

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

Finally, you need to implement the open (open) method: [url] [myAppDelegate *) so that it does what you would like with the URL. For example, it is possible to open a new view controller in it with a UIWebView and show the URL. You can do something like this:

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

    return YES;
}

Hope this works for you.

+9
source

All Articles