在iOS App中预览嵌入式链接就像Facebook一样

时间:2012-09-19 14:13:46

标签: ios

我正在尝试以与Facebook相同的方式在iOS应用程序中嵌入链接预览:

enter image description here

我正试图找到一种方法来获取最合适的图像(并返回其中的URL),页面标题,甚至是元描述并将其传递回应用程序,但我不确定最好的方式。

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

2 个答案:

答案 0 :(得分:3)

您可以在服务器端或客户端执行此操作。

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

客户端您可以使用NSURLConnection或像AFNetworking这样的库将整个页面下载为HTML(例如Mashable为~180KB),并使用XML解析器解析它以查找<head>标记。

我建议你创建一个服务器脚本,这样你就可以在其他项目或其他平台上重用它。

答案 1 :(得分: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

这是我的responseSerializer的标题

#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分配给您的自定义

这对我很有用