我正在尝试在iPhone中实现类似下图的功能。
我的要求是,如果用户在textview
中输入任何网站网址链接或youtube视频网址链接,那么我想从链接获取(获取)该网址标题,说明,基本网址,以及默认图片。然后我会将其显示为下图。我成功使用以下代码来获取任何网址标题..但我无法获取描述,链接的基本网址和默认图片。
NSError *error = nil;
NSString *html = [NSString stringWithContentsOfURL:_textView.text encoding:NSASCIIStringEncoding error:&error];
if(html)
{
NSLog(@"HTML %@", html);
NSRange r = [html rangeOfString:@"<title>"];
if (r.location != NSNotFound)
{
NSRange r1 = [html rangeOfString:@"</title>"];
if (r1.location != NSNotFound)
{
if (r1.location > r.location)
{
NSString *title = [html substringWithRange:NSMakeRange(NSMaxRange(r), r1.location - NSMaxRange(r))];
NSLog(@"title %@", title);
lable = [[UILabel alloc]init];
lable.frame = CGRectMake(10, 40, 280, 30);
lable.backgroundColor = [UIColor clearColor];
lable.text = title;
lable.numberOfLines=2;
lable.textColor=[UIColor redColor];
lable.font=[UIFont fontWithName:@"Times New Roman" size:14];
[youCell addSubview:lable];
}
}
}
}
如果任何有任何想法的人请与我分享...提前感谢...
答案 0 :(得分:0)
您需要将inputString覆盖到URL中。
NSError *error = nil;
NSURL *inputURL = [NSURL URLWithString:_textView.text];
NSString *pageSource = [NSString stringWithContentsOfURL:inputURL
encoding:NSASCIIStringEncoding
error:&error];
if(pageSource)
{
NSRange startTitle = [pageSource rangeOfString:@"<title>"];
if (startTitle.location != NSNotFound)
{
NSRange endTitle = [pageSource rangeOfString:@"</title>"];
if (endTitle.location != NSNotFound)
{
if (endTitle.location > startTitle.location)
{
NSString *pageTitle = [pageSource substringWithRange:NSMakeRange(NSMaxRange(startTitle), endTitle.location - NSMaxRange(startTitle))];
NSLog(@"Title of Page := %@", pageTitle);
}
}
}
}