swift - Fetching OpenGraph meta tags from URL - ios

I'm trying to fetch og (OpenGraph) tags from a URL and display in the app. I get the tags properly for most of the websites, but for some, I don't.
ex. This URL
When I render it on the browser and inspect, it shows me the meta tags, but when I try to fetch the html via cURL, it returns a html content without any meta. But, when I share the URL in Slack or FB, it renders the preview with the OG tags.
How do I do it on iOS?
Any help is appreciated.

The problem with the URL you shared is that the site requires Javascript to be enabled.
If you don't have Javascript enabled, you just get back a basic page saying that you need to have Javascript enabled to browse the site (hence, no Open Graph tags).
Your browser, Slack and Facebook execute Javascript but cURL does not. Neither will a URLSession in iOS.
In iOS, the only way you're going to be able to get access to the HTML for such a website is to use a WKWebView to render the page.
Then, when it's loaded, execute some Javascript using evaluateJavascript(_:completionHandler:) to get the OG tags.
That's the only workable solution for these cases using iOS libraries.
The iOS client for Facebook, LinkedIn etc do not do this on their iOS client. Their iOS client calls their backend, passing the URL for processing, and receives back the preview. Javascript is executed as part of this backend process.
There are public APIs around that will help you to do the same thing, such as:
https://opengraph.io

Related

Redirect user using NetworkExtension framework

I have an application that uses NEFilterProvider API to filter urls (allows/blocks browsing). ControlProvider and DataProvider extensions created.
So for this point I can block/allow certain urls.
I wonder if there is a way to implement redirection to custom url using NetworkExtension ?
According to NEFilterDataProvider:
If the Filter Data Provider chooses to block the web page, then a special “block” page is displayed in the WebKit browser object informing the user that their attempt to access the content was blocked. The Filter Data Provider can choose to add a link to this block page, giving the user the option of requesting access to the content.
There is no other way to redirect user besides a link from "block" page. Neither url nor page content could be changed using NEFilterProvider API. The best solution for redirection would be a VPN connection. You can find example here.

iOS deep linking is stripped out in Gmail

I'm trying to send an email with deep linking to my iOS app, using myapp:// format to open it up from email. It works (i.e. tapping on it opens the app) in any iOS mail client (Mail, Mailbox, etc.) but not in Gmail app (or even web), that strips it out leaving text only. Does anyone has a solution/alternative beside creating a web link that redirects then from browser to app?
Nope, unfortunately Gmail detects non-http/https protocols in links and strips the anchor () tag (so using data: or javascript: to perform a redirect is out too).
If/When Google implements Actions on Gmail for iOS/Android, those may work (https://developers.google.com/gmail/actions/actions/actions-overview), but as of now, they are not rendered on native mobile clients.
You can create a server with a regular endpoint that will redirect to the "special" myapp:// link.
If you are running Node + Express, here's an example of a middleware that does exactly that:
https://github.com/mderazon/node-deeplink

custom url schemes in twitter posts it does not work

I am an ios app developer. We have implemented a custom URL scheme 'my_app://section_name' or so where if the link is opened in the user's mobile browser, it will redirect the user to a specific section in the app.
We would like to be able to tweet these URLs and have users on their mobile device click on them to open up the app, however it just can click once
(when you click close , maybe you click wrong then you want to click it second but it does not work )
I hope this isn't too silly of a question. Thanks
Make sure to check the tweet body after it have been posted.
This may be due of an URL shortener, especially if the tweet is posted from the iOS 5 Twitter framework.
I'm having a similar issue with url schemes. We can include them in emails and text messages, and they highlight and work properly.
Unfortunately, when we do the same with a tweet, the iOS Twitter client fails to recognise the special URL scheme and so the user cannot tap on it to open our app.
Pretty big oversight, methinks. Anyone else had any joy including special URL scheme links in tweets?
A solution that you should consider involves not sharing the URI scheme directly, but rather creating a page on your web server to handle this. In fact, if you want to be able to share full URI schemes with paths, you're better off building a web server to dynamically generate a page with a URI scheme redirect.
This is a over-simplified representation of what we built at Branch. This includes some code to get you started though the web server will require a bit of setup not described here.
instead of testapp://some.data.here, you'll link to http://yoursite.com/hosted-redirect/some.data.here.
your server should listen at the route /hosted-redirect, grab some.data.here and build the following page (body here):
(source: derrrick.com)
So your server will have to generate and respond with this page, filling in some.data.here, anytime http://yoursite.com/hosted-redirect/some.data.here is requested.
A lightweight node app could do this with a single file.

Phonegap: iFrames and external URLs

I am in the process of creating an iOS app with Phonegap and jQuery, however, I am running into issues trying to allow both iFrames (to load normally) and external URLs (to open in Safari). I decided to choose the path of using iframes as I was not able to send POST and open the resulting page in Safari (i was trying to create a mobile friendly login window that opens to the full site in safari).
I recently updated to v1.5 hoping to resolve the issue, but it still occurs.
I have tried the trick "[url scheme] isEqualToString:#"http"...." however this forces any page in the iframe to load in safari.
So, I would like either to have external URLs and iframes to behave just like it does in a Webapp (add to homescreen button on iOS) or be able to send POST to Safari?
Has anyone got ideas? :)
Thanks!
You could change tactic slightly and login fully using your app, but then create an authentication token which would be passed via a standard link to be opened in safari.
You could generate the token new each time. Tokens are a valid system for access.

Link on Facebook with custom prefix/protocol (like myapp://blahblah)

I added to my iOS application option to detect and response to custom URL schemes to launch application ( http://iosdevelopertips.com/cocoa/launching-your-own-application-via-a-custom-url-scheme.html ).
Link is working perfectly on different sites (using href), but I'm having problem with Facebook. If I'm trying to post link (using Graph API) which looks like:
myapp://blabla
Facebook return error:
The url you supplied is invalid
And for feed with link return
link URL is not properly formatted.
I can't just post url as a message because it's not being detected as a URL and appear like text only.
Is there any way to post to Facebook wall with custom links?
Edit:
I have an idea, but I don't know if it gonna work. Putting
myapp://blabla directly into address field in mobile browser is launching application so probably accessing an webpage (like http://www.mywebpage/myapp) with only redirection to
myapp://blabla gonna work too, but is Facebook gonna accept that link?
I think your suggestion in the edit is the correct method, and should work. However applications like spotify seem to use an intersticial page which fires the "app link" with javascript, the advantage to this approach is that you can use that page to "sell" the app to users who don't have it and also provide lovely open graph tags for people who want to share it.

Resources