How to create then destroy a UIWebView inside a class file - objective-c

I am trying to dynamically create a UIWebView inside a class file upon launching the application.
I have successfully gotten a function to be called inside that class file upon launching, but when I add the the code to create the webview to that function, I am getting errors like this: "Tried to obtain the web lock from a thread other than the main thread or web thread. This may be the result of calling to UIKit from a secondary thread. Crashing now..."
My code to create the UIWebView is this:
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 16, 16)];
[webView setDelegate:self];
[webView setHidden:YES];
NSURL *url = [NSURL URLWithString:address];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[webView loadRequest:urlRequest];
Where am I going wrong?

You are creating the webview from a background thread, which is not allowed by Apple and makes your app crash.
You should use dispatch_async to create your webview from the main thread :
dispatch_async(dispatch_get_main_queue(), ^{
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 16, 16)];
[webView setDelegate:self];
[webView setHidden:YES];
NSURL *url = [NSURL URLWithString:address];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[webView loadRequest:urlRequest];
});

Related

HTTP load failed and I have tried all of the SO suggestions, but nothing works

Error 9802 is a "Fatal Alert" according to the Apple's docs. I have looked at Google and SO and found nothing referencing 9802 that fixes this problem. I can change my code to go for "apple.com" and I get the page, but using my own URL, I get the error above. I have tried everything that I can think of, and nothing works. Time to ask for help! :D
The result of using
/usr/bin/nscurl --ats-diagnostics https://pragerphoneapps.com
is all tests fail (results are uploaded here)
This is an image of my app's .plist file (only pertinent part)
and this is an image of the target info:
And this is my code:
- (IBAction)showHTMLHelp:(UIButton *)sender {
// make the popover
UIViewController* popoverContent = [UIViewController new];
UIView* popoverView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 450, 500)];
popoverView.backgroundColor = [UIColor colorWithWhite:(CGFloat)1.0 alpha:(CGFloat)1.0]; // frame color?
popoverContent.view = popoverView;
//resize the popover view shown in the current view to the view's size
popoverContent.preferredContentSize = CGSizeMake(450, 500);
NSString *urlAddress = #"https://pragerphoneapps.com"; // #"https://www.pragerphoneapps.com/bookstore-inventory-manager/upload-help/";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
// add the UIWebView for RichText
UIWebView *webView = [[UIWebView alloc] initWithFrame: popoverView.frame];
webView.backgroundColor = [UIColor whiteColor]; // change background color here
// add the webView to the popover
[webView loadRequest:requestObj];
[popoverView addSubview:webView];
// if previous popoverController is still visible... dismiss it
if ([popoverController isPopoverVisible]) {
[popoverController dismissPopoverAnimated:YES];
}
//create a popover controller
popoverController = [[UIPopoverController alloc] initWithContentViewController:popoverContent];
[popoverController presentPopoverFromRect:((UIButton *)oShowHelp).frame inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
I seriously recommend you to fix your SSL certificate.
ATS requires you to have a valid SSL certificate, TLS version 1.2 or above and support for forward secrecy.
This will be a requirement at the beginning of 2017 so better get ready.
Anyway, meanwhile try the following steps to get this working.
Put this on top of your file:
#interface NSURLRequest (InvalidSSLCertificate)
+ (BOOL)allowsAnyHTTPSCertificateForHost:(NSString*)host;
+ (void)setAllowsAnyHTTPSCertificate:(BOOL)allow forHost:(NSString*)host;
#end
Disable App Transport Security in Info.plist.
Allow invalid SSL certificates before loading request:
[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[url host]];
Sample code that successfully loads your site:
NSString *urlAddress = #"https://pragerphoneapps.com"; // #"https://www.pragerphoneapps.com/bookstore-inventory-manager/upload-help/";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[url host]];
UIWebView *webView = [[UIWebView alloc] initWithFrame: self.view.frame];
webView.backgroundColor = [UIColor whiteColor];
[self.view addSubview:webView];
[webView loadRequest:requestObj];
I wonder why it can't work for setting "NSAllowArbitraryLoads to YES".If there are caches, try to reboot your Mac or Xcode.
And your "Exception Domains" settings have the wrong format.

Display a .pdf in an iOS app without downloading it from the internet

I'm looking to package a .pdf file with my app so that users can view it on one of the app's tabbed UIViews. Most of the posts I've read explain how to display a .pdf that must be downloaded first from the internet. My question is: how do you access a .pdf that is packaged with the app and display it on a UI(Web)View (with no internet access/download required)?
EDIT: This snippet was the key (where "ReadFile.pdf" was .pdf added to the project):
NSString *urlAddress = [[NSBundle mainBundle] pathForResource:#"ReadFile" ofType:#"pdf"];
This is what you should do:
– (void) displayLocalPdfInWebView
{
CGRect rect = [[UIScreen mainScreen] bounds];
CGSize screenSize = rect.size;
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0,0,screenSize.width,screenSize.height)];
webView.autoresizesSubviews = YES;
webView.autoresizingMask=(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth);
NSString *urlAddress = [[NSBundle mainBundle] pathForResource:#"JaapSahib-Gurmukhi" ofType:#"pdf"];
NSURL *url = [NSURL fileURLWithPath:urlAddress];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[webView loadRequest:urlRequest];
[window addSubview:webView];
[webView release];
}
Source: http://www.developerfeed.com/how-display-local-pdf-file-ios-uiwebview/
What this will essentially do is:
Create a CGRect to set the screen bounds
Allow autoresizing
Set the UIWebView to to a resource
Load the file into the UIWebView
Comment, and tell me how it goes!

Objective-c Web/URL preview as in facebook app

In facebook app, when we post url, it can show small preview page. When we tap on that, it will go to that url page.
Is it possible to do preview page with webview? If so, may I know how to do? If not, what method shall I use to show preview?
You can do it with something like this:
UIWebView *webView = [[UIWebView alloc] initWithFrame: CGRectMake(0.0, 0.0, 1.0, 1.0)];
webView.delegate = self;
NSURLRequest *request = [[NSURLRequest alloc] initWithURL: [NSURL URLWithString: #"www.my_url.com"] cachePolicy: NSURLRequestUseProtocolCachePolicy timeoutInterval: myTimeoutValue];
[self.webView loadRequest: request];

UIWebView sharp corners

I have an UIWebView with and using QuartzCore to add some style to this. But when i'm execute:
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(10, 10, self.view.bounds.size.width-20.0, self.view.bounds.size.height-30.0)];
NSString *urlAddress = #"http://www.google.com/";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
webView.layer.cornerRadius = 10;
webView.layer.borderColor = [UIColor whiteColor].CGColor;
webView.layer.borderWidth = 3.0f;
[self.view addSubview:webView];
I get an rounder View but content is sharp. There is a way to roundend corners in content in WebView
The content view is actually the scrollView of UIWebView, so
webView.scrollView.layer.cornerRadius = 10;
Agreed with #graver Answer but not completely Coz His working will not work on iphone 3g and iPod 2nd generation .
webView.layer.cornerRadius = 10;
[webView setClipsToBounds:YES];
ClipsToBound will simple resize all the subviews under webView.

How to tell when UIWebView has finished displaying content

I'm making a UIWebView load a pdf like this:
UIWebView *pdfView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 768, 1024)];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL fileURLWithPath:path]];
pdfView.delegate = self;
[pdfView loadRequest:request];
And then doing some stuff with the view once it loads:
- (void)webViewDidFinishLoad:(UIWebView*)sender {
[sender takeScreenshot]; // my UIView category screenshot method
[sender release];
}
It all works, apart from the fact that at the point webViewDidFinishLoad is called, the view hasn't displayed anything yet, it displays a few milliseconds later. So is there any way to tell when the UIWebView has finished displaying its content (a pdf in this case)?
You could do:
[self performselector:#selector(getScreenshot) withObject:sender afterDelay:0.1];
-(void) getScreenshot:(UIWebView *)webView {
[webView takeScreenshot];
[webView release];
}
Or something similar.