Cocoa WebView (MacApp) - how to use custom fonts? - macos

I have a WebView in a Cocoa application (macgap).
(NOTE I'm not talking about a UIWebView in an iOS app)
In my CSS file, there are some custom font declarations:
#font-face{
font-family:'Quivira';
src:url("./Quivira.ttf") format("truetype");
font-weight:normal;
font-style:normal;
}
I can't get these to load however - some sort of security issue I believe.
Sometimes, I can see there is an error in the console "Not allowed to load local resource: Quivira.ttf" (it doesn't always show this though)
What could be causing this? The index.html file is loaded via the file:/// protocol so there shouldn't be a security issue, as it is already local and should therefor be able to load local resources.
I'm thinking it could be a bug in webkit.
Is anyone able to load custom fonts in a Cocoa WebView?

I have tried two approaches to load webpages with custom fonts.
From the app bundle (using NSBundle)
From a local path specified by the user (using absolute path)
In both cases I am successfully able to display Custom Fonts. There is a HTML file and beside it, a .ttf font file. In the HTML file I have defined #font-face in inline CSS.
In the second case you can give any full path from local filesystem and the webview will load from that (like /Users/johndoe/Desktop/maiwebsite/index.html).
Code is on Github.
And from another answer on SO:
Instead of getting the resource path and stapling a subpath onto it yourself, ask the bundle to give you the correct path.
Instead of handling a path at all, ask the bundle to give you the correct URL. (Requires Mac OS X 10.6 or later.)
If this doesn't help then could you put up your code somewhere so we can try to debug it?

For WKWebView (WebView now deprecated), load custom fonts from the MacApp bundle like so:
[_webView loadHTMLString:htmlContent baseURL:[[NSBundle mainBundle] resourceURL]];
Unlike iOS where all one needs to supply for bundle resources is: [[NSBundle mainBundle] bundlePath] (iOS Bundle is flatter), for MacApps the path to resources is more complex and bundlePath alone will NOT work. The above approach DOES work.
In your CSS simply use #font-face like so and any fonts in the location specified by resourceURL will be found.
#font-face {font-family:'myfont';src:url('myfont.ttf');}

Related

how app extension can access the containing app images/assets/resource data

I implemented the document provider app extension feature with my own
ios app. The problem is the app extension was not able to access the
containing app images/assets or the containing apps resource data.
Does any one knows how to achieve it?.
The extension has its own target in your Xcode project. In order to access resources in the extension you also need to add them to that target (in the File Inspector also check your extension target in the Target Membership section). Just be aware that those resources also get copied into the extension bundle, which increases your overall app size.
Sidenote: In my extension I didn't add my default image asset catalogue (Images.xcassets) to the extension, but was still able to access the containing images. Maybe that's an exception.
UPDATE
If you are supporting iOS 8 and up, you can create a framework to share among your app and it's extensions, and embed resources, strings, etc. inside of it. Make sure to use the [UIImage imageNamed:inBundle:compatibleWithTraitCollection:] and NSLocalizedStringFromTableInBundle methods, passing in the bundle that is your framework.
PREVIOUS ANSWER
Given that extensions are (and hopefully always will be) in a sub-folder of the main app at /Plugins/PluginName and have permissions to access files just like the main app, you can do the following (and I do in some of my apps on the store):
// image
UIImage* image = [UIImage imageNamed:#"../../image_name.png"]
// data
NSString* path = [NSHomeDirectory() stringByAppendingPathComponent:#"../../file.json"];
NSData* data = [NSData dataWithContentsOfFile:path];
This will save you a lot of space, especially if your extension uses a lot of images.

Display WebArchive from Mail.app and Notes.app

Cocoa's WebView can display .webarchive files. The ones I try to display come from the pasteboard, e.g. when copying parts of a web page in Safari or Mail.app.
The issue I am having is that webarchives from Mail and Notes won't display in a WebView, while webarchives from Safari do.
I looked inside the data of those archives (BBEdit can decypher their binary plist format and show it as XML nicely) and found that the issue is caused by the unusual URL references Mail (and Notes) puts in there:
<key>WebResourceURL</key>
<string>x-msg://4/</string>
If I remove that entry or change it to something using http://, WebView suddenly can display such archives.
Now, how do I solve this in general in my code?
I don't want to have to decode the webarchive, find the WebResourceURL entry and remove it before passing the archive to the WebFrame for loading.
I wonder if there's something else I have to set up with either the WebView or its main frame to make this work.
I noticed that Xcode can show these webarchives just fine, suggesting that Xcode uses WebKit in a more "proper" way that solves the issue. But then, maybe that's just because it has NSWebFrame load the archive from disk, while my code loads it from a CFData object - when loading from a file, WebKit may be using that file URL as the base URL, while it only chokes on it when it doesn't get a usable URL at all.
I have created a little demo project for Xcode here: http://files.tempel.org/Various/Mail-WebArchive-Display-Issue.zip
It includes both an original archive from Mail ("mail-bad.webarchive") and a fixed one ("mail-good.webarchive"), both of which are displayed in two webViews in the demo app.
I had also opened a Tech Support Indident (TSI) with Apple DTS, and the responded that I should file a bug. The bug report can be seen here: http://openradar.appspot.com/radar?id=2843403
After brooding over what Xcode might be doing I found a solution:
This does not work:
WebArchive *archive = [[WebArchive alloc] initWithData:webData];
[[webView mainFrame] loadArchive:archive];
But this does:
[[webView mainFrame] loadData:webData MIMEType:#"application/x-webarchive" textEncodingName:#"" baseURL:NULL];
So, the explicit function to load the archive is faulty while the one that wants an explicit URL, even though I give it none, works.
Go figure.

Font awesome not displaying properly on Firefox

I am trying to use a font awesome icon in a widget that is installed on a customers website. The font awesome icons displays correctly in Safari and Chrome but doesnt in Firefox. However, it still displays correctly in Firefox when previewing it on our site. Does this have something to do with how Firefox displays third party fonts across domains?
Any ideas would be greatly appreciated. Thanks.
Note: This was tested using Firefox V9 and above.
Firefox only allows cross-domain linking of fonts if the server the font is on sends the right CORS headers. And it does this because the spec very clearly says to do it, at http://dev.w3.org/csswg/css3-fonts/#default-same-origin-restriction
I notice a strange behaviour, probably related to the firefox security policies.
I had the same problems with a project configuration like so:
/site/html <--- where all the .html files go
/site/resources/... <--- where all the css, font, img, js stuff goes
now, I included the font-awesome.min.css in an html file located under the /site/html directory and I experimented your problem.
But when you download the Font Awesome package it is shipped with html demo files that actually works in firefox. What's the trick?!
Their project structure has the "resources" folder (they call it "assets") nested inside the "html" folder. This seems to calm down the firefox security policy.
Finally, my answer is: get a configuration like the following
/site/html <--- where all the .html files go
/site/html/resources/... <--- where all the css, font, img, js stuff goes
it worked for me.
If you are hosting your font on S3, you have to enable CORS on the bucket. See my answer to this other question for details
MaxCDN identified and fix this issue. They set the right CORS headers and embedding this line to your website should work:
<link href="//maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css" rel="stylesheet">
Firefox blocks Cross-Origin Request.
Firefox disallows reading the remote resource due to Same Origin Policy for below CDN:
https://maxcdn.bootstrapcdn.com/font-awesome/4.1.0/css/font-awesome.min.css
I dug little to fix CORS issue instead I replaced above CDN with below one and icons rendering fine:
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.1.0/css/font-awesome.min.css" media="all" rel="stylesheet" type="text/css">
Use direct link for including css files , also make sure you don't get a cross-domain error in the debugging console .
For example when you access your website from :
http://www.domain.tld make sure you link css file from the same path including www
like so : http://www.domain.tld/css/style.css
and when you access from http:// > link css files also from that very same path without www.
http://domain.tld/css/style.css
i got that issue some time ago and it was fixed by modifying css paths to request css files from the "same" web address / path .
example:
you can view font awesome icons in this path
http://webake.ro/
But not in this one :
http://www.webake.ro/
because the font was linked from within http://domain.tld path without adding www. in the
link href=
Font from origin 'http://webake.ro' has been blocked from loading by Cross-Origin Resource Sharing policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://www.webake.ro' is therefore not allowed access.
Turn your fonts into base64 and include through CSS. This way you push the fonts through the browser code and the font files are not downloaded in the usual fashion needing cross domain permissions. This is also a DISA STIG issue to disable downloadable fonts but probably not your issue here. The solution can be seen at this post and also copied here:
You just need to Base64 the font and include it in a CSS file. Make sure to remove your call to the downloadable WOFF file once you include the call to the new FontAwesomeB64.css
Use https://www.base64encode.org/ to encode the WOFF Font-Awesome font file.
Edit the the resulting file and add these lines. When you get to the src:url line, make sure to run that right into the base64 information you received (don't use the greater than and less than signs I show here.) At the end of that base64 information add the single quote, parentheses, a semi-colon, and curly brace to finish:
#font-face {
font-weight: 400;
font-style: normal;
font-family: 'FontAwesome';
src:url(data:application/x-font-woff;base64,<insert base64 code here>);}
You now have a base64 CSS file of the Font-Awesome font that bypasses all font download denial settings in browsers.
I've found that this works with all fonts, a little heavier on the download but worth the guarantee of functionality.
#font-face{font-family:'FontAwesome-webfont';
trust me, this really works.

How to add an external stylesheet/js file to a UIWebView without having it stored?

I want to change some CSS properties and HTML content from a random web, so I download and save the html file in the device and load it in a UIWebView to modify it.
The problem occurs when the html has referenced stylesheet/javascript files and they aren't loaded in the UIWebView.
Is there a way to access automatically to these files?
I've thought of getting the path through javascript and add the web url to it, and then download... but first I'll have to check if the path is relative or absolute and that...
Is there an easy and clean way to do this?
(I have the same problem with the images that are not url linked...)
Any idea would be appreciated :)

Is there a syntax highlighting editor component for Mac OS X

I am looking for a syntax highlighting component that I can include in a Mac OS X XCode project to allow editing of Ruby, C++, Lua, etc.
I need a component that is open source or has the source included.
My Google searches didn't turn up much in the way of Mac OS X frameworks or components at all, let alone the type I am looking for.
Thanks for any pointers!
UKSyntaxColoredTextDocument (Mac-specific) or Scintilla (cross-platform and in use in a variety of editors, including Komodo) might be what you're looking for.
Try to use scintilla. http://www.scintilla.org/
You could try using CodeMirror. I do this in my SQLite Professional app to highlight user editable queries. It's also fairly simple to setup:
In your project, add the CodeMirror contents as a Folder rather than a group.
Add the WebKit.Framework to your project.
Add a WebView to your nib/xib.
Add the following code:
// Load our webview from our local CodeMirror path
NSURL * url = [NSURL fileURLWithPath: [NSString stringWithFormat:#"%#/CodeMirror/Demo/preview.html", [[NSBundle mainBundle] resourcePath]]];
NSString * html = [NSString stringWithContentsOfURL: url encoding:NSUTF8StringEncoding error:nil];
[webView.mainFrame loadHTMLString: html baseURL: url];
They also have lots of formats pre-defined, so it works quite well. The one issue I have found with this, is that the native Find dialog does not work when in the context of the WebView.

Resources