在我的iPhone应用程序的UITextView的是包含URL。 我想开在UIWebView的这个网址,而不是打开它在Safari的? 我的UITextView包含了一些数据与URL一起。 在某些情况下,没有。 的URL可以是多个。
由于桑迪
在我的iPhone应用程序的UITextView的是包含URL。 我想开在UIWebView的这个网址,而不是打开它在Safari的? 我的UITextView包含了一些数据与URL一起。 在某些情况下,没有。 的URL可以是多个。
由于桑迪
假设你有以下情况,这也被添加到您的UIView:
UITextView *textView;
UIWebView *webView;
和TextView中包含URL字符串,您可以加载URL到web视图中的内容,具体如下:
NSURL *url = [NSURL URLWithString:textView.text];
NSURLRequest *req = [NSURLRequest requestWithURL:url];
[webView loadRequest:req];
您可以按以下步骤操作:
UITextView
从XIB或故事板截取。 或写这些对于动态拍摄的TextView。
textview.delegate=self;
textview.selectable=YES;
textView.dataDetectorTypes = UIDataDetectorTypeLink;
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; }
我认为你正在寻找这一点。
该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类的bonified委托。 打开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;
}
希望这应该为你工作。