如何在 UI Web 视图中打开 UITextView URL?

发布于 2024-08-08 08:35:20 字数 149 浏览 2 评论 0原文

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

谢谢 沙

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

相思故 2024-08-15 08:35:20

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

  1. 在来自 Xib 或 Storyboard 的 UITextView 中勾选以下属性。

检查 UITextView 的这些属性

或者为动态获取的文本视图编写这些属性。

textview.delegate=self;
textview.selectable=YES;
textView.dataDetectorTypes = UIDataDetectorTypeLink;
  1. 现在编写以下delegate方法:
-(BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange
{
 NSLog(@"网址:%@",网址);
//您可以使用此处的 URL 执行任何操作(例如在其他 Web 视图中打开)。
    返回否;
}

我想你正在寻找那个。

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.

多像笑话 2024-08-15 08:35:20

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

myTextView.dataDetectorTypes = UIDataDetectorTypeLink;

然后您需要配置您的应用程序以捕获此 URL 请求并让您的应用程序处理它。我在 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);

:最后,您需要实现 [(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;
}

希望这对你有用。

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.

御弟哥哥 2024-08-15 08:35:20

假设您有以下实例,也添加到您的 UIView:

UITextView *textView;
UIWebView *webView;

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

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

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];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文