Avba
Avba

Reputation: 15266

uiwebview not loading any content

I am not able to load any webpages. Any idea why this could be? The frame for the webview (printed from the debugger)

<UIWebView: 0x8a49840; frame = (0 0; 320 241); autoresize = TM+BM; layer = <CALayer: 0x8a498f0>>

Here is my code:

#import "ViewController.h"

@interface ViewController () <UIWebViewDelegate>

@end

@implementation ViewController
@synthesize webview;

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.webview.delegate = self;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)viewDidAppear:(BOOL)animated
{

}
- (IBAction)buttonPress:(id)sender {
    NSLog(@"pressed");
    NSURL *theUrl = [NSURL URLWithString:@"www.google.com"];
    NSError *e ;
    // just for test - ALSO returning nil!!!
    NSString *str =  [NSString stringWithContentsOfURL:theUrl encoding:NSUTF8StringEncoding error:&e];
    NSLog(@"%@",str);
    NSURLRequest *theRequest = [NSURLRequest requestWithURL:theUrl];
    [self.webview loadRequest:theRequest];
}
- (void)webViewDidStartLoad:(UIWebView *)webView {
    NSLog(@"page is loading");
}
// this method is never called
-(void)webViewDidFinishLoad:(UIWebView *)webView {
    NSLog(@"finished loading");
}
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    return YES;
}

@end

Upvotes: 0

Views: 472

Answers (5)

Matthew Ferguson
Matthew Ferguson

Reputation: 95

(Xcode 5 iOS 7) Needed to update an Universal App for iOS 7 and Xcode 5. It is an open source project / example located here: Link to SimpleWebView (Project Zip and Source Code Example)

Upvotes: 0

ashokdy
ashokdy

Reputation: 1001

just use this in your buttoned pressed method

 NSURL *theUrl = [NSURL URLWithString:@"http://www.google.com"];
 NSURLRequest *theRequest = [NSURLRequest requestWithURL:theUrl];
 [self.webview loadRequest:theRequest];

always make sure that using the protocol type that is "http://" is prefixed to the web url string

Upvotes: 0

Bruno Delgado
Bruno Delgado

Reputation: 594

You may also check if your link doesn't contain the "http://" or "https://" and add it.

NSString *website = @"www.google.com";
NSRange textRangeHTTP = [[website lowercaseString] rangeOfString:@"http://"];
NSRange textRangeHTTPS = [[website lowercaseString] rangeOfString:@"https://"];
if((textRangeHTTP.location == NSNotFound) && (textRangeHTTPS.location == NSNotFound))
       website = [NSString stringWithFormat:@"http://%@",website];

NSURL *theUrl = [NSURL URLWithString:website];

Upvotes: 1

Dharmbir Singh
Dharmbir Singh

Reputation: 17535

You don't have proper URL. Because +URLWithString: expects a protocol (e.g. http://, https://), if you have www.google.com in that case it cannot build a URL.So please try to use like this..

NSURL *url = [NSURL URLWithString:@"http://www.google.com"];

Upvotes: 1

Balu
Balu

Reputation: 8460

use this one it'l work.

problem: in your url http:// was missed

NSURL *theUrl = [NSURL URLWithString:@"http://www.google.com"];

Upvotes: 4

Related Questions