如何在UI Web View中打开UITextView URL?

[英]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.

在我的iPhone应用程序中,UITextView包含一个URL。我想在UIWebView中打开此URL而不是将其打开到safari中?我的UITextView包含一些数据和URL。在某些情况下,没有。 URL可以不止一个。

Thanks Sandy

谢谢桑迪

3 个解决方案

#1


3  

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

假设您有以下实例,那么这些实例也会添加到您的UIView中:

UITextView *textView;
UIWebView *webView;

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

而textView包含URL字符串,可以将URL的内容加载到webView中,如下所示:

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

#2


10  

You can follow these steps:

您可以按照以下步骤操作:

  1. Tick on the following properties in UITextView taken from Xib or Storyboard.
  2. 勾选从Xib或Storyboard获取的UITextView中的以下属性。

Check these properties of UITextView

OR write these for textview taken dynamically.

或者为动态采取的textview写下这些。

textview.delegate=self;
textview.selectable=YES;
textView.dataDetectorTypes = UIDataDetectorTypeLink;
  1. Now write the below delegate method :
  2. 现在编写下面的委托方法:
-(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.

我想你正在寻找那个。

#3


9  

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

UITextView能够检测URL并相应地嵌入超链接。您可以打开该选项:

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-.

然后,您需要配置您的应用程序以捕获此URL请求并让您的应用程序处理它。我在github上发布了一个样板类来执行此操作,这可能是最简单的路线: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:

第一步是对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

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:

接下来,您需要更新main.m以将MyApplication.h指定为UIApplication类的bonified委托。打开main.m并更改此行:

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:

最后,您需要实现[(MyAppDelegate *)openURL:url]方法,让它按照您希望的URL进行操作。就像打开一个带有UIWebView的新视图控制器一样,并显示URL。你可以这样做:

- (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.

希望这应该对你有用。

智能推荐

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.itdaan.com/blog/2009/10/12/7200965a3cc8133f03a326ac5aae180.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告