The web view loads the page. It doesn't call the webViewDidStartLoad or webViewDidFinishLoad methods.
N_NumberAppDelegate.h:
#interface N_NumberAppDelegate : UIViewController <UIWebViewDelegate> {
UIWindow *window;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#end
N_NumberAppDelegate.m:
#implementation N_NumberAppDelegate
#synthesize window;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 20, 320, 480)];
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://registry.faa.gov/aircraftinquiry/NNum_Results.aspx?NNumbertxt=2N"]]];
[window addSubview:webView];
[window makeKeyAndVisible];
[window release];
}
- (void)webViewDidStartLoad:(UIWebView *)webView {
NSLog(#"Started loading");
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
NSLog(#"Finshed loading");
}
i think you have forgot to set your delegate.
ie webView.delegate=self;
good luck
Related
I am attempting to load a UIWebView only after the page loads. Until the page is ready, I only want the splash screen to display. I tried following the first answer on this page (XCode Prevent UIWebView flashing white screen after Launch Image) but it doesn't seem to work...the splash screen loads and the UIWebView never loads.
Any advice?
ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController{
IBOutlet UIWebView *webView;
}
#property (nonatomic, retain) IBOutlet UIWebView *webview;
#property(nonatomic, strong) UIImageView *loadingImageView;
#end
ViewController.m
#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://http://warm-chamber-7399.herokuapp.com/"] cachePolicy:NSURLCacheStorageAllowed timeoutInterval:20.0]];
//**************** Add Static loading image to prevent white "flash" ****************/
UIImage *loadingImage = [UIImage imageNamed:#"Default.png"];
loadingImageView = [[UIImageView alloc] initWithImage:loadingImage];
loadingImageView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"Default.png"],
nil];
[self.view addSubview:loadingImageView];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
// Remove loading image from view
[loadingImageView removeFromSuperview];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
Try this,
#interface ViewController () <UIWebViewDelegate>
{
UIWebView * webView;
UIImageView * imageView;
}
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
imageview = [[UIImageView alloc]init];
[imageview setFrame:CGRectMake(0.0, 0.0, 320.0, 460.0)];
[imageview setImage:[UIImage imageNamed:#"Red.png"]];
[self.view addSubview:imageview];
webView = [[UIWebView alloc]initWithFrame:CGRectMake(0.0, 0.0, 320.0, 460.0)];
[webView setDelegate:self];
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.apple.com"]]];
// Do any additional setup after loading the view, typically from a nib.
}
// UIWebView Delegate
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
[imageView removeFromSuperview];
[self.view addSubview:webView];
}
#end
Have you set the delegate of webView? Without setting that, your webViewDidFinishLoad: method will never fire.
webView.delegate = self;
Why dont you directly add launch image to your .plist file
So I'm having a little difficulty setting this up. I'm using this: UIWebView open links in Safari for the code but I can't seem to get it to work.
Here's my .h file:
#import <UIKit/UIKit.h>
#interface SecondViewController : UIViewController
{
}
#property (strong, nonatomic) IBOutlet UIWebView *webView;
#end
And here's my .m file:
#import "SecondViewController.h"
#interface SecondViewController ()
#end
#implementation SecondViewController
#synthesize webView = _webView;
- (void)viewDidLoad
{
[super viewDidLoad];
[_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http:m.youtube.com/user/haatfilms/uploads"]]];
[_webView setBackgroundColor:[UIColor clearColor]];
[_webView setOpaque:NO];
_webView.scrollView.bounces = NO;
_webView.scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
}
-(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType {
if ( inType == UIWebViewNavigationTypeLinkClicked ) {
[[UIApplication sharedApplication] openURL:[inRequest URL]];
return NO;
}
return YES;
}
#end
And I think I've connected the UIWebView delegate:
But still, when I click on a link, it still loads in the UIWebView and not in Safari.
Can anyone shed some light as to why it won't work? Thank you so much
Found the problem. Instead of using IB to connect my webView delegate, just do this:
[super viewDidLoad];
webView.delegate = self;
I have a problem with my app (ipad) on iOS4. The Keyboard doesn't work, doesn't display on my webview AFTER the first view (login view).
Appdelagate :
AppDelegate.h
#class RootViewController;
#interface StandardFacileAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
RootViewController *rootViewController;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet RootViewController *rootViewController;
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
rootViewController = [[RootViewController alloc] init];
window.rootViewController = rootViewController;
//[window addSubview:rootViewController.view];
[window makeKeyAndVisible];
return YES;
}
RootViewController :
- (void)viewDidLoad {
[super viewDidLoad];
[self loadViewController];
[self.view addSubview:viewController.view];
}
- (void)loadViewController {
ViewController *viewControllertemp = [[ViewController alloc]initWithNibName:#"ViewController" bundle:nil];
self.viewController = viewControllertemp;
[viewControllertemp release];}
viewController is my login view after request i call the method for change the view (my webview) :
- (void)changeViewToIpadTheWebView:(UIViewController *)fromView{
UIViewController *currentView = (UIViewController*)fromView;
NSLog(#"currentView =>%#",currentView);
if(!ipadTheWebView)
[self loadipadTheWebView];
[currentView.view removeFromSuperview];
for (UIView *view in [self.view subviews]) {
[view removeFromSuperview];
}
[self.view addSubview:ipadTheWebView.view];
NSURL *url = [NSURL URLWithString:#"http://www.google.com/"];
NSURLRequest *requestURL = [NSURLRequest requestWithURL:url];
[ipadTheWebView.webView loadRequest:requestURL];
My webview is ok but if i want search word in google, the keyboard doesn't display (the view scroll) when the textfield have the focus.
What is the problem, this code working on iOS5 but not ios4?
Thanks for your help ;)
Edit :
Thanks Mathieu,
RootViewController.h
#import <UIKit/UIKit.h>
#class ViewController;
#class IpadTheWebView;
#interface RootViewController : UIViewController {
ViewController *viewController;
IpadTheWebView *ipadTheWebView;
}
#property (nonatomic,retain) ViewController *viewController;
- (void)loadViewController;
- (void)changeViewToViewController:(UIViewController *)fromView;
#property (nonatomic,retain) IpadTheWebView *ipadTheWebView;
- (void)loadipadTheWebView;
- (void)changeViewToIpadTheWebView:(UIViewController *)fromView;
- (void)keyboardWillShow:(NSNotification *)notification;
- (void)keyboardWillHide:(NSNotification *)note;
#end
In my app I am using TapDetectingWindow to detect touches on a UIWebView (exactly as detailed on http://mithin.in/2009/08/26/detecting-taps-and-events-on-uiwebview-the-right-way).
I used it in one View Controller as suggested with a reference in the header file and the following in the implementation file like
- (id)init {
self = [super init];
if (self) {
tapDetectingWindow = (TapDetectingWindow *)[[UIApplication sharedApplication].windows objectAtIndex:0]; //mWindow, variable reference to TapDetectingWindow
tapDetectingWindow.viewToObserve = webView;
tapDetectingWindow.controllerThatObserves = self;
webView = [[UIWebView alloc] init];
[webView setFrame:CGRectMake(0, 0, 340, 1900)];
[webView setTag:1];
[webView addSubview:keyboardText];
[webView setUserInteractionEnabled:NO];
[webView setDelegate:self];
[webView setOpaque:0.0];
[webView loadHTMLString:htmlString baseURL:NULL];
[self.view addSubview:webView];
keyboardText = [[UITextField alloc] init];
keyboardText.autocorrectionType = UITextAutocorrectionTypeNo;
[keyboardText setDelegate:self];
[self.view addSubview:keyboardText];
}
return self;
}
but my app is crashing at
"tapDetectingWindow.viewToObserve = webView "
with a report * -[UIWindow setViewToObserve:]: unrecognized selector sent to instance 0x4a26b90
Can Any one help me out ...
The problem is not in the above code. Its due to new property introduction in iOS 5.0 with name window under UIApplicationDelegate protocol.
Change your window name in appdelegate file.
In .h file
#class WebViewController;
#class TapDetectingWindow;
#interface test_touchAppDelegate : NSObject <UIApplicationDelegate>
{
TapDetectingWindow *myWindow;
WebViewController *viewController;
}
#property (retain, nonatomic) TapDetectingWindow *myWindow;
#property (retain, nonatomic) WebViewController *viewController;
#end
In .m file
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.myWindow = [[TapDetectingWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.viewController = [[WebViewController alloc] init];
self.myWindow.rootViewController = self.viewController;
[self.myWindow makeKeyAndVisible];
return YES;
}
Hope this will help.
what Apurv told you is correct and work like a charm. I see you ask for alternate method to recognise gestures on a webview (I assume that it means click on UIWebView.). The answer is yes, you have to use " UIWebView delegate method ".
#pragma mark -
#pragma mark uiwebview delegate
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
if(navigationType==UIWebViewNavigationTypeLinkClicked){
NSURL *URL = [request URL];
if(URL){
[[UIApplication sharedApplication] openURL:URL];
return NO;
}
}
return YES;
}
The code above, when you click on UIWebView, it will open the link of UIWebView that you clicked. Good luck.
Ok I want to add a UIImageView as a subview and then remove it after a couple of seconds in the way a splash screen works. I found three different approaches to do it but I can not understand which one is the best approach according to Objective-C and Apple.
Below are the three different approaches:
1)
In my MyAppDelegate.h
#interface MyAppDelegate : NSObject <UIApplicationDelegate> {
MyViewController *myViewController;
UIImageView *myImageView;
}
#property (nonatomic, retain) IBOutlet MyViewController *myViewController;
#property (nonatomic, retain) IBOutlet UIWindow *window;
#end
and in MyAppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
myImageView =[[UIImageView alloc] initWithFrame:CGRectMake(0.0,0.0,self.window.frame.size.width,self.window.frame.size.height)];
myImageView.image=[UIImage imageNamed:#"Yoga.png"];
[self.window addSubview:myImageView ];
[self.window bringSubviewToFront:myImageView];
[self performSelector:#selector(removeImage) withObject:nil afterDelay:2.5];
return YES;
}
-(void) removeImage
{
[myImageView removeFromSuperview];
[myImageView release];
[self.window addSubview:myViewController.view];
[self.window makeKeyAndVisible];
}
2) in the second approach:
In my MyAppDelegate.h
#interface MyAppDelegate : NSObject <UIApplicationDelegate> {
MyViewController *myViewController;
UIImageView *myImageView;
}
#property (nonatomic, retain) IBOutlet UIImageView *myImageView;
#property (nonatomic, retain) IBOutlet MyViewController *myViewController;
#property (nonatomic, retain) IBOutlet UIWindow *window;
#end
and in MyAppDelegate.m
#synthesize myImageView;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
myImageView =[[UIImageView alloc] initWithFrame:CGRectMake(0.0,0.0,self.window.frame.size.width,self.window.frame.size.height)];
myImageView.image=[UIImage imageNamed:#"Yoga.png"];
[self.window addSubview:myImageView ];
[self.window bringSubviewToFront:myImageView];
[self performSelector:#selector(removeImage) withObject:nil afterDelay:2.5];
return YES;
}
-(void) removeImage
{
[myImageView removeFromSuperview];
[myImageView release];
[self.window addSubview:myViewController.view];
[self.window makeKeyAndVisible];
}
- (void)dealloc
{
[myViewController release];
[myImageView release];
}
3) in the third approach:
In my MyAppDelegate.h
#interface MyAppDelegate : NSObject <UIApplicationDelegate> {
MyViewController *myViewController;
}
#property (nonatomic, retain) IBOutlet MyViewController *myViewController;
#property (nonatomic, retain) IBOutlet UIWindow *window;
#end
and in MyAppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIImageView *myImageView =[[UIImageView alloc] initWithFrame:CGRectMake(0.0,0.0,self.window.frame.size.width,self.window.frame.size.height)];
myImageView.image=[UIImage imageNamed:#"Yoga.png"];
myImageView.tag=22;
[self.window addSubview:myImageView ];
[myImageView release];
[self.window bringSubviewToFront:myImageView];
[self performSelector:#selector(removeImage) withObject:nil afterDelay:2.5];
return YES;
}
-(void) removeImage
{
for (UIView *subview in [self.view subviews]) {
if (subview.tag == 22){
[subview removeFromSuperview];
}
}
[self.window addSubview:myViewController.view];
[self.window makeKeyAndVisible];
}
- (void)dealloc
{
[myViewController release];
}
So to sum up.. The first approach does not use a property for the UIImage only a variable, the second one uses a property and the third one just creates the UIImage and adds it as a subview and then removes it based on its tag..
Which is the right approach to follow..I believe that all three options sound right.. But is there any certain way I should follow. Is any of these options better in terms of memory and performance?
Thanks in advance,
Andreas
You could use an animation attached to the view's layer. Code below fades the view out - but there are many other ways to remove it. (you need to attach the QuartzCore framework)
myImageView.layer.opacity = 0.0;
// this is the state the view will be in after the animation (e.g. invisible)
CABasicAnimation *theFade;
theFade = [CABasicAnimation animationwithKeyPath:#"opacity"];
theFade.duration = 10.0;
theFade.fromValue = [NSNumber numberWithFloat:1.0]; // i.e. visible
theFade.toValue = [NSNumber numberWithFloat:0.0]; // i.e. invisible
[myImageView.layer addAnimation:theFade forKey:#"animateOpacity"];
If you are not going to use the image again, there is no need to keep a pointer to it. Further, if you use IBOutlet, you need to add the view in IB as well. In this specific example I would say option 3 makes the most sence, especially considering that with this choice you can began with a standard "view based application" template and just add the bits about the image view and leave the rest alone. One last observation of choice 3 though; the 2 messages to window;
[self.window addSubview:myViewController.view];
[self.window makeKeyAndVisible];
Appear to be outside the scope of any method. This is likely just a copy and paste error, but make note that they should located within "didFinishLaunchingWithOptions:"