如何在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.

希望這應該對你有用。

关注微信公众号

注意!

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



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