2012-09-19 32 views
11

我试图嵌入在以同样的方式在iOS应用程序,Facebook并链接预览:预览嵌入式链接Facebook一样

enter image description here

我试图找出一种方法来抓取最合适的图像(并将网址返回给它),页面标题,甚至是元描述,然后将其传递回应用程序,但我不确定最佳方式。

有API可以做到这一点,大部分是为了价格,但它似乎不应该是这么难。有什么想法吗?

+4

你试过_anything_? – Abizern

+0

嗯,我在PHP中构建了一个非常基础的链接预览,它从给定的网页中抽取某些数据,但这很粗糙 – d2burke

+1

你有什么发现? – Esqarrouth

回答

3

你可以做这个服务器端或客户端。

服务器端,您可以使用脚本(如您创建的脚本)来获取HTML页面的<head>标记。

客户端可以使用NSURLConnection或类似AFNetworking的库以HTML格式(例如Mashable为〜180KB)下载整个页面,并使用XML解析器解析它以查找<head>标签。

我建议您创建一个服务器脚本,以便您可以在其他项目或其他平台中重复使用它。

+0

有没有人有任何与此链接预览相关的示例,我想使用它的一个我的聊天应用程序。提前致谢。 –

+0

我想做同样的事情... –

2

我去同一个目标,我做到了在客户端

我用这些吊舱

pod 'HTMLReader' 
pod 'AFNetworking' 

然后我从AFHTTPResponseSerializer继承并返回一个包含链接的详细信息的对象

#import <UIKit/UIKit.h> 

@interface LinkDetails : NSObject 

@property (nonatomic,strong) NSString *linkURL; 

@property (nonatomic,strong) NSString *linkHOST; 

@property (nonatomic,strong) NSString *linkTitle; 

@property (nonatomic,strong) NSString *linkDescription; 

@property (nonatomic,strong) NSString *linkWebSiteName; 

@property (nonatomic,strong) NSString *linkImageUrl; 

@property (nonatomic,strong) UIImage *linkImage; 

@end 

这是我的回复标题序列化器

#import <AFNetworking/AFNetworking.h> 

@interface HTMLResponseSerializer : AFHTTPResponseSerializer 

@end 

,这是实现我responseSerializer

#import "HTMLResponseSerializer.h" 
#import <HTMLReader/HTMLReader.h> 
#import "LinkDetails.h" 

@implementation HTMLResponseSerializer 

-(id)responseObjectForResponse:(NSURLResponse *)response data:(NSData *)data error:(NSError *__autoreleasing _Nullable *)error{ 

    NSString *responseStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; 

    LinkDetails *details = [[LinkDetails alloc] init]; 

    HTMLDocument *document = [HTMLDocument documentWithString:responseStr]; 

    NSArray *metaTags = [document nodesMatchingSelector:@"meta"]; 

    for (HTMLElement *metaTag in metaTags) { 

     if ([[[metaTag attributes] objectForKey:@"property"] isEqualToString:@"og:url"] || [[[metaTag attributes] objectForKey:@"property"] isEqualToString:@"twitter:url"]) { 
      NSLog(@"%@",[[metaTag attributes] objectForKey:@"content"]); 
      details.linkURL = [[metaTag attributes] objectForKey:@"content"]; 
     } 

     if ([[[metaTag attributes] objectForKey:@"property"] isEqualToString:@"og:title"] || [[[metaTag attributes] objectForKey:@"property"] isEqualToString:@"twitter:title"]) { 
      NSLog(@"%@",[[metaTag attributes] objectForKey:@"content"]); 
      details.linkTitle = [[metaTag attributes] objectForKey:@"content"]; 
     } 

     if ([[[metaTag attributes] objectForKey:@"property"] isEqualToString:@"og:description"] || [[[metaTag attributes] objectForKey:@"property"] isEqualToString:@"twitter:description"]) { 
      NSLog(@"%@",[[metaTag attributes] objectForKey:@"content"]); 
      details.linkDescription = [[metaTag attributes] objectForKey:@"content"]; 
     } 

     if ([[[metaTag attributes] objectForKey:@"property"] isEqualToString:@"og:image"] || [[[metaTag attributes] objectForKey:@"property"] isEqualToString:@"twitter:image"]) { 
      NSLog(@"%@",[[metaTag attributes] objectForKey:@"content"]); 
      details.linkImageUrl = [[metaTag attributes] objectForKey:@"content"]; 
     } 

     if ([[[metaTag attributes] objectForKey:@"property"] isEqualToString:@"og:site_name"] || [[[metaTag attributes] objectForKey:@"property"] isEqualToString:@"twitter:site_name"]) { 
      NSLog(@"%@",[[metaTag attributes] objectForKey:@"content"]); 
      details.linkWebSiteName = [[metaTag attributes] objectForKey:@"content"]; 
     } 
    } 

    if(!details.linkTitle){ 
     details.linkTitle = [document firstNodeMatchingSelector:@"title"].textContent; 
    } 

    if(!details.linkDescription){ 
     details.linkTitle = [document firstNodeMatchingSelector:@"description"].textContent; 
    } 

    if (!details.linkHOST) { 
     details.linkHOST = [response.URL host]; 
    } 

    if (!details.linkURL) { 
     details.linkURL = [response.URL absoluteString]; 
    } 

    return details; 
} 

@end 

不要忘记responseSerlializer分配给您的自定义一个

这个工作对我非常好