0

I know this is a common question but none of the answers fix my problem. I have a UIWebView in my app. I have everything set up correctly; the delegate is set, the webView is a property of my view controller. It is set up to load an html string upon loading the viewController. When the html string loads, in the webViewDidFinishLoad method it checks if this is the first time it loaded, and if it is it is told to load a request with a url. The string i use for the url is a property of my viewController. That string is exactly what it is supposed to be. There is just something preventing the webView from actually loading the request. Any ideas would be very much appreciated. Thank you

    @interface WebViewController : UIViewController <UIWebViewDelegate, UIActionSheetDelegate, UIPrintInteractionControllerDelegate, MFMailComposeViewControllerDelegate, NSURLConnectionDelegate>

@property (nonatomic, assign) BOOL firstLoad;
@property (nonatomic, retain) UIWebView *webView;
@property (nonatomic, retain) NSString *prefixURLString;
@property (nonatomic, retain) NSString *suffixURLString;
@property (nonatomic, retain) UIActionSheet *actionSheet;

- (id)initWithTitle:(NSString *)newTitle andSuffixURL:(NSString *)suffix;
- (void)reload;
- (void)addActionButton;
- (void)showActions:(UIBarButtonItem *)sender;
- (void)dismissActionSheet;

@end



@implementation WebViewController

@synthesize webView;
@synthesize prefixURLString;
@synthesize suffixURLString;
@synthesize firstLoad;
@synthesize actionSheet;

- (id)initWithTitle:(NSString *)newTitle withPrefixURL:(NSString *)prefix andSuffixURL:(NSString *)suffix
{
    self = [super initWithNibName:nil bundle:nil];
    if (self) {
        // Custom initialization
        [self setPrefixURLString:prefix];
        [self setFirstLoad:YES];
        [self setTitle:newTitle];
        [self setSuffixURLString:suffix];
        [self setActionSheet:nil];
    }
    return self;
}

- (id)initWithTitle:(NSString *)newTitle andSuffixURL:(NSString *)suffix
{
    return [self initWithTitle:newTitle withPrefixURL:@"https://customer.stld.com/flatroll/ios/%@" andSuffixURL:suffix];
}

- (void)reload
{
    if(self.prefixURLString && self.suffixURLString)
    {
        NSString *fullURLString = [NSString stringWithFormat:self.prefixURLString, [self.suffixURLString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
        [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:fullURLString]]];
    }
}
- (void)dealloc
{
    [suffixURLString release]; suffixURLString = nil;
    [prefixURLString release]; prefixURLString = nil;
    [actionSheet release]; actionSheet = nil;
    [super dealloc];
}


- (void):(UIWebView *)aWebView didFailLoadWithError:(NSError *)error
{
    //NSLog(@"WebViewController:DidFailLoadWithError: %@", [error localizedFailureReason]);
    [self setTitle:@"Loading Error"];
    [self.webView loadHTMLString:[NSString stringWithFormat:@"<html><head><meta name=\"viewport\" content=\"width=device-width, user-scalable=yes\" /></head><body>Loading error:<br />%@</body></html>", [error localizedFailureReason]] baseURL:nil];
}

- (void)webViewDidFinishLoad:(UIWebView *)aWebView
{
    if(self.firstLoad)
    {
        [self setFirstLoad:NO];
        [self reload];
        [self addActionButton];
    }
}

#pragma mark - View lifecycle

- (void)loadView
{
    UIWebView *newWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
    [newWebView setDelegate:self];
    [newWebView setScalesPageToFit:YES];
    [newWebView setUserInteractionEnabled:YES];
    [self setWebView:newWebView];
    [self setView:newWebView];
    [newWebView release];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.webView loadHTMLString:@"<html><head><meta name=\"viewport\" content=\"width=device-width, user-scalable=yes\" /></head><body><br />Gathering data and generating report.<br />Depends on data and parameters you have requested, this could take time.  Please wait...</body></html>" baseURL:nil];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    [self.webView setDelegate:nil];
    [self setWebView:nil];
    [self setView:nil];
}
1
  • 'NSURLRequest *requestObj = [NSURLRequest requestWithURL:url]; [webView loadRequest:requestObj]; ' Commented Sep 26, 2012 at 13:18

2 Answers 2

1

I had the same problem, I'm not entirely sure why, but I guess it has to do something with viewControllers life cycle. Anyway try load URL in viewDidAppear method. It fixed it for me. Hope it will help

Sign up to request clarification or add additional context in comments.

1 Comment

Doing this didn't fix it for me, but I have a feeling you are onto something with the view life cycles. I copied the code from another app I have and the code was working in that app but not in this one... Thanks for your help!
0

It turns out my problem had nothing to do with the UIWebView. I just had to change the credential persistence in the URLConnectionDelegate I had set up for my app. I had it set as NSURLCredentialPersistanceNone but I changed it to NSURLCredentialPersistenceSession. I hope this can help anyone else out there who had the exact problem as I did

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.