WKWebView with local file - ios

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];

Related

View SVG createad in NSData in WKWebView

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/"]];

didFinishNavigation not being called

I'm trying to load a web page in a WKWebView control without loading it as a subview:
WKPreferences *preferences = [[WKPreferences alloc] init];
preferences.javaScriptEnabled = YES;
WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
configuration.preferences = preferences;
WKWebView *webView = [[WKWebView alloc] initWithFrame:CGRectZero configuration:configuration];
webView.navigationDelegate = self;
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];
[urlRequest setValue:#"Safari" forHTTPHeaderField:#"User-agent"];
[webView loadRequest:urlRequest];
The url I'm trying to load is this:
https://www.google.com/search?q=mustang
The following WKNavigationDelegate method is not being called:
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation
{
[webView evaluateJavaScript:#"document.getElementsByTagName('html')[0].outerHTML" completionHandler:^(id result, NSError *error) {
NSLog(#"Result %#", result);
}];
}
My NSObject class conforms to the WKNavigationDelegate protocol.
Do I need to do something else?
Greatly appreciate any help.
I had a WK Scheme Handler in my code and I used it for tracking requests done and completed (amongst other things).
It showed that if one resource load failed, the didFinishNavigation: did not get called.
So, your content needs to load in its entirety for this to ever be called.
Loading the same URL seems to work for me.
Do note:
The didFinishNavigation method is called after main frame load completes (this includes loading stuff from other hosts which are present in the main frame). So if something would be responding slow the didFinishNavigation method won't be invoked until resolved.

UIWebView does not load full content from an url

I have a UIWebView where i load a url. But it does not load full content. I have checked all possible forums for solution but couldn't find a breakthrough yet. Any help would be really appreciated? You can try out the below url yourself. Basically, the url is supposed to contain html5 content/player.
-(void)loadPlayerUrl
{
activityIndicatorView.hidden = NO;
NSURL* nsUrl = [NSURL URLWithString:#"http://watch.nimbletv.com/tv"]
NSURLRequest* request = [NSURLRequest requestWithURL:nsUrl cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:30];
[self.playerWebView loadRequest:request];
}
However, this works perfectly in safari or any other browser.
You should create ViewControllerWebPage
Then
- (void)viewDidLoad
{
[super viewDidLoad];
self.screenName = #"Info Ekran";
NSURL *mobileCreaURL = [NSURL URLWithString:#"http://mobilecrea.com/"];
NSURLRequest *myRequest = [NSURLRequest requestWithURL:mobileCreaURL];
[mobileCrea loadRequest:myRequest];
}
Also create new view, select your new custom class (ViewControllerWebPage) and than create UIWebView

Not able to make phone call using UIWebView

I want to make phone call using UIWebView. I tried below code which works fine in if I simply put a button and on button click execute below code. But currently on button click, I call an api and on response I execute below code.
// Make a call to given phone number
- (void)callPhoneNumber:(NSString *)phoneNumber
{
if (!self.webView)
{
webView = [[UIWebView alloc] init];
[self.view addSubview:self.webView];
self.webView.delegate = self;
}
// Remove non-digits from phone number
phoneNumber = [[phoneNumber componentsSeparatedByCharactersInSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]] componentsJoinedByString:#""];
// Make a call
NSURL * url = [NSURL URLWithString:[NSString stringWithFormat:#"tel:%#", phoneNumber]];
[self.webView loadRequest:[NSURLRequest requestWithURL:url]];
}
Its not even calling webview delegate methods.
What can be the reason?
Please note that I want to call using webview only, so please don't suggest to use native contact app. Using webview keeps flow within the app. When call is ended users is in app only. Using native app, if user wants to come back to my app user has to manually open the app.
To dial a phone number you have to call -[UIApplication openURL:]:
NSString *phoneNumber = #"+5512345678";
NSURL *phoneNumberURL = [NSURL URLWithString:[NSString stringWithFormat:#"tel://%#", phoneNumber]];
[[UIApplication sharedApplication] openURL:phoneNumberURL];
Why not use this?
NSString *phoneNumberURL = [#"telprompt://" stringByAppendingString: phoneNumber];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumberURL]];
This takes the user back to the app automatically once the call is finished. I hope that is what you want to achieve using webview.
https://stackoverflow.com/a/12065542/569497
Are you sure self.webView has been initialized?
Change: webView = [[UIWebView alloc] init];
To: self.webView = [[UIWebView alloc] init];
and this: [NSString stringWithFormat:#"tel:phoneNumber"] don't work; try: [NSString stringWithFormat:#"tel:%#",phoneNumber]
Personally, however, I never tried to make a phone call in this way.
If you want to call via UIWebView then use this example:
+ (void)callWithString:(NSString *)phoneString
{
[self callWithURL:[NSURL URLWithString:[NSString stringWithFormat:#"tel:%#",phoneString]]];
}
+ (void)callWithURL:(NSURL *)url
{
static UIWebView *webView = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
webView = [UIWebView new];
});
[webView loadRequest:[NSURLRequest requestWithURL:url]];
}

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