Dec 5, 2011
SVG on iOS
I could not find a good library for SVG for iOS:
- SVGQuartzRenderer doesn’t compile.
- SVGKit works good only with shipped samples and crahes on “real world” SVGs.
- CKSVG looks good, but it doesn’t support iOS out from box as soon as it’s designed for Mac, and opens my files with some errors (anyway better than SVGKit)
The main problem is a lack of free time. I had no time to fix compilers’ error, crashes and port code for Mac to iOS. I needed a ready solution and I need it quick.
I was not alone in my research. Another guy had a same problem (and I made the same reserch as me). But I could not use unstable solution in hope that it will work in my case, so I started a little investigation and I found out, that SVG can handle touches and executes javascript. OMG it can be even animated! So I tried to put it into UIWebView and now, at 1:47 am looks like I succeed.
First, SVG can be loaded with a loadHTMLString: method; I had to use this code instead:
NSData * svg = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"africa" ofType:@"svg"]];
[webview loadData:svg MIMEType:@”image/svg+xml” textEncodingName:0 baseURL:[[NSBundle mainBundle] bundleURL]];
And SVG should looks something like that:
...
Now I can load this SVG into web view and handle an URL map://egypt in my
-(BOOL) webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
delegate’s method!
I still had to get rid of nasty black rectangle (link highlighting) when I tapping on map, but anyway, this approach is much better than rasterization my SVG images or dealing with buggy third party code…