UIWebView using loadData compile error - ios

I have NSData in Core Data stored, could be image, rtf, video or text. I am trying to use UIWebView to show the NSData using loadData, but the build give me the error message:
No visible #interface for 'UIWebView' declares the selector 'loadData:MIMEType:textEncoding:baseURL:'
This is my code for RTF:
UIWebView *wv = [[UIWebView alloc] initWithFrame:view.bounds];
NSData *data = entry.data;
[wv loadData:data MIMEType:#"application/rtf" textEncoding:#"utf-8" baseURL:nil];
[view addSubview:wv];
I have seen a lot of links how to use this and also Apple Documentation explains it. Why I am getting the build error? iOS Development Target is 7.0

The comments in this link say to not pass a nil value for the base URL. Try replacing nil with even just #"https://"

I have restarted my MacBook and also Xcode, cleaned the project and the error is gone - very strange, but now it works fine.

Related

WKWebView gives SecurityError when bundling html and javascript with app

We are trying to migrate a hybrid app from UIWebView (iOS < 8) to WKWebView (iOS 8), but we are getting SecurityErrors when trying to store stuff using the DOM WebDatabase API (i.e. 'web sql databases').
The following throws an error if the index.html has been loaded from a bundled file with the app
// throws SecurityError: DOM Exception 18
var db = openDatabase('mydb', '1.0', 'key value store', 1);
The same code works fine with UIWebView. I can fallback to using Local Storage for some reason, but using WebSQL databases is a no go. I can only speculate that this has something to do with the same origin policy or something related.
The funny thing is that loading index.html from the network works fine :-/
Any clues as to how I can work around this? Any options to set on the WKWebView that fixes it?
This is how we load the web related stuff:
NSString *htmlPath = [[NSBundle mainBundle] pathForResource:#"index" ofType:#"html"];
NSURL *baseURL = [NSURL fileURLWithPath:htmlPath];
NSURLRequest *request = [NSURLRequest requestWithURL:baseURL];
WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
[config.userContentController addScriptMessageHandler:self.myCallbacks name:#"NativeApp"];
self.webView = [[WKWebView alloc] initWithFrame:self.view.frame configuration:config];
[self.webView loadRequest:request];
The html file simply loads a javascript file that has a relative path, "myCode.js".
There is an issue (OpenRadar) with WKWebView in iOS 8.0 (and 8.1 B1, I think) that prevents it from loading local files. It might be affecting local storage too. See this question for more details.
You can fix this by adding the following method to the UIDelegate of your WKWebView.
- (void) _webView:(WKWebView *)webView
decideDatabaseQuotaForSecurityOrigin:(WKSecurityOrigin *)securityOrigin
currentQuota:(unsigned long long)currentQuota
currentOriginUsage:(unsigned long long)currentOriginUsage
currentDatabaseUsage:(unsigned long long)currentUsage
expectedUsage:(unsigned long long)expectedUsage
decisionHandler:(void (^)(unsigned long long newQuota))decisionHandler {
decisionHandler(1024*1024*50); //default to 50MB
}
It gives all databases a quota of 50MB, instead of the default of 0 which allows them to be opened. This behavior isn't documented, so I don't know where Apple stands with this.
Also, it appears this issue will be fixed in iOS 10.
I've made a 'plugin' that allows you to use WebSQL (more an implementation of it) in the WKWebView. It can be found here
https://github.com/ajwhiteway/WKWebSQL
import WKWebSQL
.
.
.
var webView = WKWebView(frame: view.frame, configuration: WKWebViewConfiguration())
WKWebSQL.LoadPlugin(webView)
To get it loaded into the page. Versioning isn't really supported at this time. Feel free to add it.

Converting NSData to NSString returns nil

I know this question is asked before, but none of the solutions worked for me. I am trying to convert an NSData object to a NSString value. I am initing the NSString object like following:
NSString *html = [[NSString alloc] initWithData:urlData encoding:NSUTF8StringEncoding];
But the html is always nil. The NSData I am trying to convert is the source code of a website. It is fairly long. This is 'NSData` I am trying to convert.
Is it the length of the data that is causing the issue? I need the source code as a string. What can I do to resolve this issue?
What I tried so far:
Tried with all encoding formats as shown in this answer.
Tried with [NSString stringWithUTF8String:[urlData bytes]];
But whatever I do produce the same result. html always is nil whatever I do.
EDIT
It was a problem with the debug console. Even when the objects had values in it, the debug console always showed nil as the value for most of the objects. However NSLog always displays the value.
It's not a problem with debugger
The problem comes from compiler optimization, compiler see that string was not directly used, and optimizes the code by removing it and directly passing it to another method.
The key of the problem : You are running project on release scheme
Solution:
Here is a small guide to switch project to the Debug scheme
1) Click on the target, and click Edit scheme...
2) Popup will be displayed
3) Click Run %Your project%
4) Open Build Configuration popup
5) Select Debug
5) Press OK
6) You are ready to Go!, now you can debug anything :)
If you are using ARC, and you just wrote the code that converts the data to a string and haven't written any code yet that actually uses the string, it will get deallocated immediately. Check whether that is what is happening. For example, what does NSLog (#"%#", html) display?
NSAttributedString *str = [[NSAttributedString alloc] initWithData:data options:#{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
NSCharacterEncodingDocumentAttribute: [NSNumber numberWithInt:NSUTF8StringEncoding]}
documentAttributes:nil error:&error];
Try this one:
NSString *myString = [[NSString alloc] initWithData:urlData encoding:NSASCIIStringEncoding];
Generally, conversion from NSData to NSString returns nil means there is mismatch between encoding format received from server and approach used for encoding.

After attempting to create a UIWebView with a GIF in it, when webViewDidFinishLoad is called, the URL is always just about:blank. Why?

I'm trying to show a UIWebView with a GIF in it, but only once the GIF has loaded.
I load the GIF as follows:
self.GIFWebView = [[UIWebView alloc] init];
self.GIFWebView.delegate = self;
NSString *html = [NSString stringWithFormat:#"<html><head></head><body><img src=\"%#\"></body></html>", post.url];
[self.GIFWebView loadHTMLString:html baseURL:nil];
Where post is just an object with some properties such as the URL for the GIF.
Then in webViewDidFinishLoad: I show the web view:
- (void)webViewDidFinishLoad:(UIWebView *)webView {
NSLog(#"%f", webView.scrollView.frame.size.width);
NSLog(#"%#", [webView.request.URL absoluteString]);
}
I get "0" and "about:blank" for the NSLogs each time, however.
Why does it not load the GIF properly?
I get "0" and "about:blank" for the NSLogs each time, however.
Not surprising. You're telling the web view to load HTML that you're providing in a string rather than giving it a request. The URL that you're logging is the request URL, and since there's no request, there's no request URL.
Why does it not load the GIF properly?
Possibly because you're misusing the URL object. Look at the code:
NSString *html = [NSString stringWithFormat:#"<html><head></head><body><img src=\"%#\"></body></html>", post.url];
We can't tell what type post.url is, but it's probably NSURL*. You're probably passing a NSURL into the format string, and that may not produce the result you're looking for. Try passing in a string like [post.url absoluteString] instead of the actual NSURL object.
Also, you might want to log the value of html right after you create it so that you can check the full HTML that you're sending to the web view.
Update: Some additional things to check:
Are you running the code in question on the main thread?
Is the thread's run loop getting time?
Have you tried setting a non-nil base URL?
If the web view's delegate has a -webView:shouldStartLoadWithRequest: method, does it return YES?
What happens if you use a constant string that includes the HTML you want and the hard-coded URL instead of constructing the string with +stringWithFormat:?
Is the test device connected to the network? (Sometimes it's the simplest thing that gets you.)
Does it work correctly if you use a different image? I notice that the URL you're using is for an animated .gif file, try a non-animated .gif or a .jpg image instead.
Update 2: The problem lies in your creation of the web view. Look at the very first line in the code that you showed:
self.GIFWebView = [[UIWebView alloc] init];
That looks okay for a typical object, but -init is not the designated initializer for a view. You should use -initWithFrame: instead. The image loads fine in your sample project when I change the code in your project to use the right initializer:
UIWebView *GIFWebView = [[UIWebView alloc] initWithFrame:self.view.bounds];

NSData initWithContentsOfURL reading not all data, but only on device

I am banging my head about an issue I have on iOS7 development. I use the following piece of code to load an image from a webserver:
NSData* data = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:#"http://someServer/someImage.jpg"]];
This works like a charme in simulator, reading exactly the 134185 bytes that the image has. Creating an UIImage from that data works as intended.
Once I test the exact same code on a device (iPad Mini, iOS 7.03), though, it just reads 14920 byte from the same URL. Needless to say that I can't create an UIImage from that data then, creation fails and returns a nil.
The read does not produce any errors (no console output, and also using the signature with the error output param returns nil here). Is there anything I missed around this rather straightforward task? Haven't found anything on the web on this…
Thanks, habitoti
So you don't have any error, and something is downloading. Maybe try to read this response and post here (I guess it is html/text body)?
You can use NSString method:
+ (instancetype)stringWithContentsOfURL:(NSURL )url encoding:(NSStringEncoding)enc error:(NSError *)error;
Can I suggest you use a library like SDWebImage to retrieve your image, it caches it and downloads the images asynchronously.
It also has a category for UIImageView so you can just call [imageView setImageWithURL:]; and it will load the image in when its ready.

UIDocumentInteractionController doesn't open the app (didEndSendingToApplication: never called)

I have a UIDocumentInteractionController instance (that DOES have a strong reference in my class, I am aware of the memory issues about it) and I want to send a photo file to Instagram.
I saved the file using the ig extension (tried igo as well) and I am presenting the controller. Instagram is displayed on the list. I tap Instagram, and nothing happens.
NSURL *imageFile = [NSURL fileURLWithPath:path];
interactionController = [UIDocumentInteractionController interactionControllerWithURL:imageFile];
interactionController.UTI = #"com.instagram.photo";
interactionController.annotation = [NSDictionary dictionaryWithObject:#"my caption" forKey:#"InstagramCaption"];
interactionController.delegate = self;
[interactionController presentOpenInMenuFromRect:self.view.frame inView:self.view animated:YES];
To investigate further, I've set my calling class as a delegate and implemented the willBeginSendingToApplication: and didEndSendingToApplication: methods. Interestingly, I've realized that willBeginSendingToApplication: does get called, but didEndSendingToApplication: does not. I've tried changing my file extensions, changing UTI to com.instagram.exclusivegram, checking if the file URL is correct etc. but none of them seem to work. No error, nothing in the console or anything. The interaction controller closes, my app keeps working as it was working before, just nothing happens. I've read that there can be some issues on iOS 6, but my app is an iOS 6 app, so I can't test it on iOS < 6. The only thing that is close to my problem that I've found is UIDocumentInteractionController, No File Extension but UTI but it dives too much into the low level bits, nor I have a non-ARC code.
What could be the cause of the problem?
This can happen if the file doesn't exist, but also if you haven't constructed the file URL correctly. This plagued me for a while.
Make sure you construct your file URL like this:
NSURL *pagesURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"MyGreatPDF.pdf" ofType:nil]];
and not like this:
NSURL *pagesURL = [NSURL fileURLWithPath:#"MyGreatPDF.pdf"];
The latter still forms a valid URL, but it gets a "private" prefix, i.e. file:///private/var/mobile/Applications/00000000-0000-0000-0000-000000000000/MyGreatApp.app/MyGreatPDF.pdf rather than file:///var/mobile/Applications/00000000-0000-0000-0000-000000000000/MyGreatApp.app/MyGreatPDF.pdf
After a long while, I've found out that the file was not saved correctly, and didn't exist. iOS wasn't throwing out any sort of an error, failing silently. I've corrected the code about generating the file, and when the file was there, the controller appeared. Maybe Apple should add some assertion/exception mechanism for handling non-existent files in document interaction controller.
This may also caused by the file name's extension.
If the target app declare it only support file with png extension in Info.plist -> Exported Type UTIs -> Equivalent Types -> public.filename-extension, and you send a file with jpg extension, the target app won't open as well.

Resources