Get Thumb image and title of particular Website URL in iOS?
Asked Answered
G

7

7

enter image description here

I have to display thumb image and get title of WEB URL same as LinkedIN app and Skype app in iOS?

Is there any Solution?

Gesellschaft answered 29/3, 2016 at 15:39 Comment(2)
This question is too broad and lacks and MCVE. The problem statement is also unclear. What is the "thumb image" you want?Can
See this demo : github.com/kiritmodi2702/PreViewInIOSSale
C
4

URLEmbeddedView written in Swift, but you can use in Objective-C.

This is Objective-C sample code. Please check it.

#import <URLEmbeddedView/URLEmbeddedView-Swift.h>
#import <MisterFusion/MisterFusion-Swift.h>

@interface ViewController ()

@property (weak, nonatomic) IBOutlet UITextView *textView;
@property (strong, nonatomic) URLEmbeddedView *embeddedView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.textView.text = @"https://github.com/";

    [OGDataProvider sharedInstance].updateInterval = [NSNumber days:10];

    self.embeddedView = [[URLEmbeddedView alloc] initWithUrl:@""];
    [self.containerView addLayoutSubview:self.embeddedView andConstraints:@[
        self.embeddedView.Top,
        self.embeddedView.Right,
        self.embeddedView.Left,
        self.embeddedView.Bottom
    ]];
}

- (IBAction)didTapFetchButton:(id)sender {
    [self.embeddedView loadURL:self.textView.text completion:nil];
}

@end
Cavorelievo answered 2/7, 2016 at 9:11 Comment(1)
can you check this please: github.com/marty-suzuki/URLEmbeddedView/issues/90Mankind
L
9

Here is the easiest way to find the favicon icon of URL

NSString *myURLString = @"http://www.google.com/s2/favicons?domain=www.facebook.com";
NSURL *myURL=[NSURL URLWithString: myURLString];
NSData *myData=[NSData dataWithContentsOfURL:myURL];

UIImage *myImage=[[UIImage alloc] initWithData:myData];

Swift Version:

let myURLString = "http://www.google.com/s2/favicons?domain=www.facebook.com"
let myURL = NSURL(string: myURLString)!
let myData = NSData.dataWithContentsOfURL(myURL)
let myImage = UIImage(data: myData)!

May this helps lot

UPDATE

Below is the swift library which helps to load images like you want.

URLEmbeddedView

Here You can check actual work of library.

Loafer answered 29/3, 2016 at 15:56 Comment(5)
This URL gives only fav small icon 12X12. I need big icon and it's description same as Linkedin app and Skype app.Gesellschaft
@Gesellschaft I update my answer please review that.Loafer
Thanks for giving this. It is exactly same which i need. But i need for Objective C and it is for Swift.Gesellschaft
@Gesellschaft same project also available into objective C..github.com/szk-atmosphere/URLEmbeddedViewLoafer
@Gesellschaft I edited the answer to show the swift version!Santalaceous
C
4

URLEmbeddedView written in Swift, but you can use in Objective-C.

This is Objective-C sample code. Please check it.

#import <URLEmbeddedView/URLEmbeddedView-Swift.h>
#import <MisterFusion/MisterFusion-Swift.h>

@interface ViewController ()

@property (weak, nonatomic) IBOutlet UITextView *textView;
@property (strong, nonatomic) URLEmbeddedView *embeddedView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.textView.text = @"https://github.com/";

    [OGDataProvider sharedInstance].updateInterval = [NSNumber days:10];

    self.embeddedView = [[URLEmbeddedView alloc] initWithUrl:@""];
    [self.containerView addLayoutSubview:self.embeddedView andConstraints:@[
        self.embeddedView.Top,
        self.embeddedView.Right,
        self.embeddedView.Left,
        self.embeddedView.Bottom
    ]];
}

- (IBAction)didTapFetchButton:(id)sender {
    [self.embeddedView loadURL:self.textView.text completion:nil];
}

@end
Cavorelievo answered 2/7, 2016 at 9:11 Comment(1)
can you check this please: github.com/marty-suzuki/URLEmbeddedView/issues/90Mankind
S
1

You can refer this: "An NSURL extension for showing preview info of webpages" https://www.cocoacontrols.com/controls/urlpreview

func fetchPageInfo(completion: ((title: String?, description: String?, previewImage: String?) -> Void), failure: ((errorMessage: String) -> Void)) {

        let request = NSMutableURLRequest(URL: self)
        let newUserAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2227.1 Safari/537.36"
        request.setValue(newUserAgent, forHTTPHeaderField: "User-Agent")
        ValidationQueue.queue.cancelAllOperations()

        NSURLConnection.sendAsynchronousRequest(request, queue: ValidationQueue.queue, completionHandler: { (response: NSURLResponse?, data: NSData?, error: NSError?) -> Void in
            if error != nil {
                dispatch_async(dispatch_get_main_queue(), {
                    failure(errorMessage: "Url receive no response")
                })
                return
            }

            if let urlResponse = response as? NSHTTPURLResponse {
                if urlResponse.statusCode >= 200 && urlResponse.statusCode < 400 {
                    if let data = data {

                        if let doc = Kanna.HTML(html: data, encoding: NSUTF8StringEncoding) {
                            let title = doc.title
                            var description: String? = nil
                            var previewImage: String? = nil
                            print("title: \(title)")
                            if let nodes = doc.head?.xpath("//meta").enumerate() {
                                for node in nodes {
                                    if node.element["property"]?.containsString("description") == true ||
                                    node.element["name"] == "description" {
                                        print("description: \(node.element["content"])")
                                        description = node.element["content"]
                                    }

                                    if node.element["property"]?.containsString("image") == true &&
                                        node.element["content"]?.containsString("http") == true {
                                            previewImage = node.element["content"]
                                    }
                                }
                            }

                            dispatch_async(dispatch_get_main_queue(), {
                                completion(title: title, description: description, previewImage: previewImage)
                            })
                        }

                    }
                } else {
                    dispatch_async(dispatch_get_main_queue(), {
                        failure(errorMessage: "Url received \(urlResponse.statusCode) response")
                    })
                    return
                }
            }
        })
    }
Seduce answered 29/6, 2016 at 7:23 Comment(1)
But some URL does not return image. Like google, flipkart, youtube. And also i need this code in Objective C.Gesellschaft
W
0

you'll need to download the html of the site, then parse it for the <title> tag (which gives you the title)

and the <link rel="icon" ... > tag which gives you the Favicon. Wikipedia article on Favicon link

Note that neither the title, nor the favicon are guaranteed to exist, though most sites do provide a title, and most major ones to provide a Favicon.

Wording answered 29/3, 2016 at 15:50 Comment(0)
P
0
  1. Download the home page
  2. Parse for meta properties like og:image and og:url
  3. Their content is what you need.
Pythian answered 29/3, 2016 at 16:3 Comment(2)
Actually some website give Proper URL but some does not give. So do you have proper code?Gesellschaft
Most of them provide <link rel="shortcut icon" href="---.ico" /> so this will ensure you a safe backup if you don't find og:image.Pythian
A
0

The other answers are correct, however it's worth noting that a favicon is either 16x16 pixels or 32x32 pixels. This is probably not sufficient for the size and resolution you wish to achieve.

There is no textbook way to achieve this, but one way could be to send a request to google for the website title + " icon" (for example "Facebook icon"), and pick the first result.

As for the title of the website, if you're looking for the title displayed in your browser when you enter the website you have to parse the html looking for the <title> </title> tag.

Achieving both of this could prove challenging, but it's definitely not impossible. There might be some sort of service out there that provides this, but not to my knowledge.

Assail answered 29/3, 2016 at 16:8 Comment(0)
S
0

I tried this library and it works for youtube, flipkart.

https://github.com/myell0w/MTDURLPreview

[MTDURLPreview loadPreviewWithURL:[NSURL URLWithString:@"http://www.flipkart.com"] completion:^(MTDURLPreview *preview, NSError *error) {

    }];

Printing description of preview->_title:
Online Shopping India | Buy Mobiles, Electronics, Appliances, Clothing and More Online at Flipkart.com
Printing description of preview->_domain:
www.flipkart.com
Printing description of preview->_imageURL:
http://img1a.flixcart.com/www/promos/new/20160701_015447_730x300_monsoon.jpg
Seduce answered 1/7, 2016 at 15:40 Comment(1)
would you please explain the steps to add MTDURLPreview to a project?Salimeter

© 2022 - 2024 — McMap. All rights reserved.