clement
clement

Reputation: 4266

uiwebview without interface builder

I've to switch a part of my app: I will implement a web view insread of a picture (imageview).

So I tried to make it with the interface builder but there is too many errors so I prefer to use code!

for the pictures, I user those lines :

image = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]];
self.imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(0,64,320,367);
[self.view addSubview:imageView];
[self.imageView release];

Now, I tried to use this code but nothing's showed:

@property (nonatomic, retain) IBOutlet UIWebView *webView;

...

[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.fr"]]];
[self.view addSubview:webView];

If you know how to make .... would be nice for me,

THANKS!

Upvotes: 1

Views: 1653

Answers (2)

adjust the frame as u needed and if add anything like imageview as subview to webview then u have to use bringsubviewtofront function.

        - (void) initUIWebView {
            NSLog(@"DetailViewController->initUIWebView {");

                UIWebView *aWebView;

                    //  init and create the UIWebView
                aWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 400)];
                aWebView.autoresizesSubviews = YES;
                aWebView.autoresizingMask=(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth);

                    //set the web view delegates for the web view to be itself
                [aWebView setDelegate:self];

                    //Set the URL to go to for your UIWebView
                NSString *urlAddress = @”http://www.google.com”;

                    //Create a URL object.
                NSURL *url = [NSURL URLWithString:urlAddress];

                    //URL Requst Object
                NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];

                    //load the URL into the web view.
                [aWebView loadRequest:requestObj];

                    //add the web view to the content view
                [self.view addSubview:aWebView];

                [aWebView release], aWebView = nil;

            }

              - (void)viewDidLoad {

                  [self initUIWebView]; // <<<<<<<<<<<< this calls the UIWebView Function
                  [super viewDidLoad];

              }

Upvotes: 1

Alex Terente
Alex Terente

Reputation: 12036

Declare in your h file:

UIWebView *contentWebView;  

And also tell to your viewController that you will implement <UIWebViewDelegate>

In viewDidLoad Add:

    contentWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    contentWebView.delegate = self;
    contentWebView.multipleTouchEnabled = YES;
    contentWebView.scalesPageToFit = NO;    
    [self.view addSubview:contentWebView];

In dealloc don't forget to release the webView:

[contentWebView release];

And implement the delegate methods:

#pragma mark -
#pragma mark WebViewDelegates

- (void)webViewDidStartLoad:(UIWebView *)webView{   


}

- (void)webViewDidFinishLoad:(UIWebView *)webView{  

}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{

}

And also you need to load an request. You can doit in viewDidLoad after you have added the webView to your view:

[contentWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.apple.com/"]]];

Upvotes: 4

Related Questions