View SVG createad in NSData in WKWebView - ios

I'm updating an old app that uses UIWebView. Apple will not accept my update unless I update to WKWebView.
I get a base64 string from an API, I convert it to NSData and then Show it in the UIWebView using its loadData:MIMEType:textEncodingName:baseURL: instance method.
On swapping over to WKWebView the app crashes with the error
unrecognized selector sent to instance
When calling loadData:MIMEType:textEncodingName:baseURL.
Is there something I am missing?
WKWebViewConfiguration * config = [[WKWebViewConfiguration alloc] init];
webView = [[WKWebView alloc] initWithFrame:frame configuration:config];
// Only Shows the Base64 string
// [webView loadHTMLString:imageString baseURL:[NSURL URLWithString:#"http://localhost/"]];
NSData * imageData = [[NSData alloc] initWithBase64EncodedString:imageString options:NSDataBase64DecodingIgnoreUnknownCharacters];
//Crashes the app.
[webView loadData:imageData MIMEType:#"image/svg+xml" textEncodingName:#"base64" baseURL:[NSURL URLWithString:#"http://localhost/"]];

Related

WKWebView with local file

I'm testing the WKWebView with local file, which is working in the simulator but it is not working in the device
#interface EDPresentationViewController ()<WKNavigationDelegate,WKScriptMessageHandler>
#property(nonatomic,strong)WKWebView *webView;
#property(nonatomic,strong)EDPresentationController *presentationController;
#end
#implementation EDPresentationViewController
-(void)viewDidLoad
{
[super viewDidLoad];
self.presentationController = [[EDPresentationController alloc]init];
WKWebViewConfiguration *webConfiguration = [[WKWebViewConfiguration alloc]init];
self.webView = [[WKWebView alloc]initWithFrame:self.view.frame configuration:webConfiguration];
NSURL *presentationFolder = [self.presentationController url];
NSURLRequest *request = [NSURLRequest requestWithURL:presentationFolder];
[self.webView loadRequest:request];
}
I grant the url from:
NSURL *presentationFolder = [self.presentationController url];
is ok, because I tested the same code with a UIWebview and works!
I always get the same error:
Could not create a sandbox extension for '/'
This wasn't work, I guess it would work in Objective-C as in swift
iOS Webkit not working on device, but works on simulator at swift
Any idea will be appreciated, thanks
Update 2-12-2014
I've discovered this could be a bug in iOS 8.1 and it may be fixed in 8.2
https://devforums.apple.com/thread/247777?start=25&tstart=0
I've tested moving the files to the temporary folder and I didn't get any error but the webView is just empty.
I've tested the same code (temporary folder) with a UIWebView and works fine!
Also, I've tried this:
https://stackoverflow.com/a/26054170/426180
As I could find out, this works because the css and the javascript is embebed in the html.
Try XWebView which has a very tiny embedded http server. It's much smaller than the GCDWebServer. A loadFileURL:allowingReadAccessToURL method is added through extension, so you are not aware of the server.
This worked like a charm ...
#interface ViewController () <WKScriptMessageHandler, WKNavigationDelegate>
...
WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
WKUserContentController *controller = [[WKUserContentController alloc] init];
configuration.userContentController = controller;
[configuration.preferences setValue:#"TRUE" forKey:#"allowFileAccessFromFileURLs"];
self.webView = [[WKWebView alloc] initWithFrame:[UIScreen mainScreen].bounds configuration:configuration];
self.webView.navigationDelegate = self;
// Also if you'd have bouncing problem
self.webView.scrollView.bounces = false;
self.webView.scrollView.alwaysBounceVertical = false;
[self.view addSubview:self.webView];
NSString* productURL = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"htmlapp/home.html"];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL fileURLWithPath:productURL]];
[self.webView loadRequest:request];

How to show Image in full screen from UIWebView?

I have a UIWebView that showing text and images from web.
I want to do it like when i tap on Image in UIWebView , i want to show it in Full Screen including Orientations.
so i tested with following codes.
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
if (navigationType == UIWebViewNavigationTypeLinkClicked) {
NSURL *URL = [request URL];
NSData *data = [[NSData alloc] initWithContentsOfURL:URL];
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageWithData:data]];
GGFullscreenImageViewController *vc = [[GGFullscreenImageViewController alloc] init];
vc.liftedImageView = imageView;
[self presentViewController:vc animated:YES completion:nil];
return NO;
}
else
{
return YES;
}
}
It doesn't work and showing error when i tap image in UIWebView.
2014-03-12 13:59:40.397 MMSP[1368:a0b] *** Terminating app due to uncaught exception 'CALayerInvalidGeometry', reason: 'CALayer bounds contains NaN: [0 0; nan nan]'
*** First throw call stack:
Is there anyway to do like i said above?
a) I think you should set the frame for GGFullscreenImageViewController
b) Generally (not having to do with your crash), i think you should not use [[NSData alloc] initWithContentsOfURL:URL]; to load the image at this point. On clicking a link, it will wait until the image has downloaded, and then show it, resulting in a 'lag' for your user.
Instead, just hand over the URL to your GGFullscreenImageViewController, the view controller and have it display an activity indicator until it has downloaded the image.
If you already have a UIImageView setup on your GGFullscreenImageViewController then why not just create the UIImage and assign, instead of creating a new UIImageView. The image view instance in liftedImageView is lost when you assign newly created instance.
Just do this,
NSData *data = [[NSData alloc] initWithContentsOfURL:URL];
GGFullscreenImageViewController *vc = [[GGFullscreenImageViewController alloc] init];
vc.liftedImageView.image = [UIImage imageWithData:data];
Ideally, you should pass the image url to the GGFullscreenImageViewController by providing initWithUrl: method. Do the downloading of image asynchronously or you could use open source code which does this, ex: SDWebImage.
Hope this helps!

Run HTML5/CSS3 in UIWebView

I've a problem that i couldn't solve, i wanna run the Apple - HTML5 Showcase - Gallary
in UIWebView inside my ipad app.
I've tried a local html file:
NSString *strr = [[NSBundle mainBundle] pathForResource:#"photo-gallery" ofType:#"html"];
NSString *str = [[NSString alloc] initWithContentsOfFile:strr encoding:NSASCIIStringEncoding error:nil];
NSLog(#"%#",str);
[webView loadHTMLString:str baseURL:nil];
and also a server html file, but it didn't work, can anyone help me, i wanna show an html5/css3 in UIWebView.
thx in advance.
hi this is what you can do
UIWebView *aWebView = [[UIWebView alloc] initWithFrame:webFrame];
//getthe path of the local html file
NSURL *baseURL = [NSURL fileURLWithPath:fileNamePath];
NSURLRequest *aRequest = [NSURLRequest requestWithURL:baseURL];
//load the html file into the web view.
[aWebView loadRequest:aRequest];
//add the web view to the content view
[view addSubview:aWebView];
For server html you simply skip the second line, and put the address.
Hope it helps
You can take a look at phonegap. Its a complete framework for building html5 apps inside a UIWebview with native capabilities.
See: http://www.phonegap.com

UIWebView unable to load RSS Page

Here is my code, it doesn't seem to work for RSS pages, but works fine for google.com (or any other plain old web page), any idea why?
UIWebView *wv = [[UIWebView alloc] initWithFrame:CGRectMake(0, 510, 1024, 230)];
NSURL *url = [[NSURL alloc] initWithString:#"http://feeds.marketwatch.com/marketwatch/topstories/"];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
[wv loadRequest:request];
The Marketwatch RSS URL you are using in your example returns XML, not HTML. The reason it looks like HTML when you view it in a browser is that it has an xml-stylesheet attached to it that the browser is using to render a human-readible view. However the UIWebView component does not do the same thing.
For your purposes you'd probably want to request this XML, parse it, and use the contents in a UITableView.

View Html in ios/ipod Apps

Hi i want to make a few http request and generate html from within my ipod app. I hear i want a WebView to view the html as if it were a browser. So my question is how do i make the http request? How do i parse json (or xml) and how do i save/load data? I figure i can use webstorage to load/save the data however i am thinking since its an ipod app i have a better way of storing it?
Here's a snippet to load a local html web page contained inside your app bundle:
UIWebView *webView = [[UIWebView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
NSString *path = [[NSBundle mainBundle] pathForResource:#"mywebpage" ofType:#"html"];
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:path]]];
Another alternative is to embed a local web server running in a background thread, and use a URL to localhost.
A UIWebview supports HTML5 local storage.
Here is the working code snippet. for iPod/iPhone/iPad;
Works with iOS7 and iOS6.. I have not tested in others...
UIWebView *_webView = [[UIWebView alloc] initWithFrame:self.view.frame];
[self.view addSubview:_webView];
NSURL *url = [NSURL URLWithString:#"http://www.google.com"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[_webView loadRequest:request];

Resources