XCode Prevent UIWebView flashing white screen after Launch Image - xcode

I am fairly new to XCode - I'm designing an app with a single view that just displays a UIWebView that loads my jQuery mobile web site.
I have the Default.png and Default#2x.png files for the Launch images in place. When I run the app on my test device or in the emulator, it shows my Launch Image, and then flashes a white screen while the UIWebView loads the first page.
I know that I can change the background color and opacity so it will flash a different color than white, but I would like to prevent the flash altogether. I would like the app to launch, show the Launch image, and not show the UIWebView until the first page has completely loaded. Help?

# Andreas Volz (and anyone else wondering how I solved the problem of the white "flash")
First, add the name-of-loading-image.png and name-of-loading-image#2x.png files to your Xcode project. The regular image should be 320 x 460, and the #2x image should be 640 x 920.
Then, in my ViewController.h file I added these properties:
#property (nonatomic, retain) UIWebView *webView;
#property(nonatomic, strong) UIImageView *loadingImageView;
#end
And then in my ViewController.m file I added this:
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController;
#synthesize webView;
#synthesize loadingImageView;
- (void)viewDidLoad
{
[super viewDidLoad];
//**************** Set website URL for UIWebView
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.example.com"] cachePolicy:NSURLCacheStorageAllowed timeoutInterval:20.0]];
//**************** Add Static loading image to prevent white "flash" ****************/
UIImage *loadingImage = [UIImage imageNamed:#"name-of-loading-image.png"];
loadingImageView = [[UIImageView alloc] initWithImage:loadingImage];
loadingImageView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"name-of-loading-image.png"],
nil];
[self.view addSubview:loadingImageView];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
// Remove loading image from view
[loadingImageView removeFromSuperview];
}

To know when your webView has finished loading, you must implement the UIWebViewDelegate protocol.
In your viewController's .h file:
#interface viewController : UIViewController <UIWebViewDelegate>
{
UIWebView *webView;
}
#end
Then in your viewController's .m file, add this method:
- (void)webViewDidFinishLoad:(UIWebView *)webView {
NSLog(#"content loading finished");
// Display your webView
[self.view addSubview:webView];
}
You can use the -viewDidLoad method in your viewController's .m to continue displaying your launch image. You can also begin the loading of content for your webView:
- (void)viewDidLoad {
[super viewDidLoad];
// continue to display launch image
UIImage *launchImage = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"launchImage.png" ofType:nil ]];
UIImageView *launchImageView = [[[UIImageView alloc] initWithImage:launchImage] autorelease];
[self.view addSubview:launchImageView];
// now setup the base view for the webView controller
UIView *contentView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
contentView.backgroundColor = [UIColor blueColor];
// for rotation
contentView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
self.view = contentView;
//create a frame to size and place the web view
CGRect webFrame = [[UIScreen mainScreen] applicationFrame];
UIWebView *aWebView = [[UIWebView alloc] initWithFrame:webFrame];
self.webView = aWebView;
//aWebView.scalesPageToFit = YES;
aWebView.autoresizesSubviews = YES;
aWebView.autoresizingMask=(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth);
//set the web view delegates for the web view to be itself
[aWebView setDelegate:self];
//determine the path the to the index.html file in the Resources directory
NSURL *url = [[NSBundle mainBundle] URLForResource:#"index" withExtension:#"html"];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[aWebView loadRequest:requestObj];
}

Related

UIWebView that is added programmatically not scrolling

If I add a uiwebview using storyboard it works great. But webviews that is added programmatically not scrolling.
self.webview=[[UIWebView alloc] init];
path=[[NSBundle mainBundle] pathForResource:#"index" ofType:#"html"];
[self.webview setFrame:CGRectMake(-200, 0, 200, 300)];
[self.webview loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:path]]];
[self.view addSubview:self.webview];
UIWebview is added to a View programmatically using the below code:
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIWebView *webview=[[UIWebView alloc]initWithFrame:CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, self.view.frame.size.width,600)];
[webview setBackgroundColor:[UIColor redColor]];
NSString *url=#"http://www.facebook.com";
NSURL *nsurl=[NSURL URLWithString:url];
NSURLRequest *nsrequest=[NSURLRequest requestWithURL:nsurl];
[webview loadRequest:nsrequest];
[self.view addSubview:webview];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end

UIWebView in IOS 8 tables are not showing correctly

we recently upgraded to IOS 8 (and compiled the application).
we saw that tables with text are not showing well anymore (we use UIWebView).
did anyone encounter this? is this a known issue or a bug?
this is the code:
#import "ViewController.h"
#interface ViewController ()
#property (weak, nonatomic) IBOutlet UIWebView *viewWeb;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSString* fullPath = #"/Users/deployment/Downloads/table-2.ppt";
[_viewWeb loadData:[NSData dataWithContentsOfFile:fullPath] MIMEType:#"application/vnd.ms-powerpoint" textEncodingName:nil baseURL:nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
thanks!
Are you tried this code...?
UIWebView *webView = [[UIWebView alloc]initWithFrame:CGRectMake(10.0,10.0,300,300)];
[webView setScalesPageToFit:YES];
webView.backgroundColor=[UIColor clearColor];
NSString *ppt = [[NSBundle mainBundle] pathForResource:#"test" ofType:#"ppt"];
NSURL *pptURL = [NSURL fileURLWithPath:ppt];
NSURLRequest *request = [NSURLRequest requestWithURL:pptURL];
[webView loadRequest:request];
[self.view addSubview:webView];
If this code also not worked with your ppt file. Please share your ppt file too. This code working in my case. May be some issue with presentation file.

Url Download link image Can't be view in xcode

#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize NetImage;
#implementation PlayGameViewController
- (IBAction)imageView:(id)sender {
//Content of image should be received. But it not show on ImageView.
NSLog(#"\n Image == %# ",[NSData dataWithContentsOfURL:[NSURL URLWithString:#"http://www.so.cityhi.com/cometchat/plugins/filetransfer/download.php?file=559192_10152684963740198_1083006183_n.jpg"]]);
// NetImage is image View.
NetImage.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:#"http://www.so.cityhi.com/cometchat/plugins/filetransfer/download.php?file=559192_10152684963740198_1083006183_n.jpg"]]];
//It working good
//NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:#"http://so.cityhi.com/public/user/52/0052_37ea.png"]];
//[NetImage setImage:[UIImage imageWithData:data]];
NSLog(#"Success");
}
Simply If we want to use Image with URL.here is simply code and as you did is good.
NSString *ImageURL = #"YourURLHere";
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:ImageURL]];
imageView.image = [UIImage imageWithData:imageData];
//imageView is Your UIImageView That you created in your .h file with IBOutlet
But when i use your given Image url (http://www.so.cityhi.com/cometchat/plugins/filetransfer/download.php?file=559192_10152684963740198_1083006183_n.jpg)
and requested on Browser then Image not showing that's why it may an issue with your image try another image as well.

After viewing web in app, cannot return to app without closing it and relaunching

I have an app which has several different buttons leading to various views. One of the views will open a website in Safari on iPhone which is what I want to happen. THe problem is that if I then want to go back to the app, all I get is a white screen, and I have to double click on home button, close the app and relaunch it.
Would this relate to releasing memory or view perhaps? I'm not sure how to go about fixing it so after viewing the web page in safari I can then go back to the app and continue with other views.
Here is what I am using in webViewController.m
#import "webViewController.h"
#interface webViewController ()
#end
#implementation webViewController
- (void)viewDidLoad
{
NSURL *url = [ [ NSURL alloc ] initWithString: #"http://www.mydomain.com" ];
[[UIApplication sharedApplication] openURL:url];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#end
you can use webView to load this url, then you can make a back button on this webview to come back to your app without leaving the app.
UIWebView * webView =[[UIWebView alloc]init];
webView.backgroundColor = [UIColor clearColor];
[webView setFrame: CGRectMake(0, 51, 320, 398)];
NSURL *url = [NSURL URLWithString:#"http://www.mydomain.com"];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView setDelegate:self];
webView loadRequest:requestObj];
[self.view addSubview:webView];
You can also use some delegate methods of webView to know whether your page has been loaded or not
-(void)webViewDidStartLoad:(UIWebView *)webView
{
}
-(void)webViewDidFinishLoad:(UIWebView *)webView
{
}
-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
}
Ah I've figured it out, I've added
web view.scalesPagesToFit = Yes
statement and opening the webpage in the webview works exactly how I wanted it to.

How do I put a background image on a text area (IN THE IPAD)

How do I put a template'd background image on a text area?
Not sure what you mean with "template'd"... but....
In interface builder, add the imageview behind the textview, uncheck the "opaque" box for the textview.
You can set the contents property of the text views layer.
#import <QuartzCore/QuartzCore.h>
...
- (void) viewDidLoad {
[super viewDidLoad];
textView.layer.contents = (id)[UIImage imageNamed:#"myImage.png"].CGImage
}
should work
If you want to add the image programmaticaly I managed to do it this way:
First in my textviewViewController.h I added an outlet for the UITextView.
#import <UIKit/UIKit.h>
#interface textviewViewController : UIViewController {
UITextView *textView;
}
#property (nonatomic, retain) IBOutlet UITextView *textView;
#end
Next, in my .xib I added my UITextView and connected my outlet.
Lastly in the viewDidLoad of textviewViewController.m I add the image as a subview to my textview.
- (void)viewDidLoad {
[super viewDidLoad];
// Change the pathForResource to reflect the name and type of your image file
NSString *path = [[NSBundle mainBundle] pathForResource:#"d" ofType:#"jpg"];
UIImage *img = [UIImage imageWithContentsOfFile:path];
UIImageView *imgView = [[UIImageView alloc] initWithImage:img];
[self.textView insertSubview:imgView atIndex:0];
[imgView release];
}
I tried the insertSubview:atIndex: with both 0 and 1 with the same result, but I would stick with 0 idicating that it's behind the textview.

Resources