Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1088555
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:05:07+00:00 2026-05-16T23:05:07+00:00

All, I am fairly new to XCode and am trying to get a handle

  • 0

All,

I am fairly new to XCode and am trying to get a handle on how to best deal with connection issues when trying to use a WebView. I know there are related questions on SO, but none seem to offer complete solutions. I have the following code, but it seems a little inefficient. Hopefully, someone can help me refactor it down to a point where it can be usable anywhere that a UIWebView is called.

NOTE: Please ignore memory issues for now. I realize that has to be added as well.

- (void)viewDidLoad {
    [webView setDelegate:self];

    NSString *urlAddress = @"http://www.somesite.com/somepage";
    NSURL *url = [NSURL URLWithString:urlAddress];
    NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];

    NSURLConnection *urlConnection = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self];

    [super viewDidLoad];
}

// Check for URLConnection failure
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    UIAlertView *connectionError = [[UIAlertView alloc] initWithTitle:@"Connection error" message:@"Error connecting to page.  Please check your 3G and/or Wifi settings." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
    [connectionError show];
    webView.hidden = true;
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {

    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;

    //Check for server error
    if ([httpResponse statusCode] >= 400) {
        UIAlertView *serverError = [[UIAlertView alloc] initWithTitle:@"Server error" message:@"Error connecting to page.  If error persists, please contact support." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
        [serverError show];
        webView.hidden = true;

    //Otherwise load webView
    } else {
        // Redundant code
        NSString *urlAddress = @"http://somesite.com/somepage";
        NSURL *url = [NSURL URLWithString:urlAddress];
        NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];

        [webView loadRequest:urlRequest];
        webView.hidden = false;
    }
}

// Seems redundant since we are already checking the URLConnection
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {

    UIAlertView *connectionError = [[UIAlertView alloc] initWithTitle:@"Connection error" message:@"Error connecting to page.  Please check your 3G and/or Wifi settings." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
    [connectionError show];
}

I guess what I’m wondering is, are there any shortcuts to achieve the desired functionality? Can I somehow access the URLResponse via the WebView directly? Does a nil value for the URLConnection or UIWebView imply connection errors without having to explicitly check for them? Is there an easier way to pass the URLRequest down into the delegate methods so it doesn’t have be recreated twice?

Thanks in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T23:05:07+00:00Added an answer on May 16, 2026 at 11:05 pm
    - (void)viewDidLoad {
        webView = [[UIWebView alloc]init];
        [webView setDelegate:self];
        [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://yourUrl.com"]]]
        [super viewDidLoad];
    }
    
    #pragma mark UIWebView delegate methods
    - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
        //read your request here
        //before the webview will load your request
        return YES;
    }
    - (void)webViewDidStartLoad:(UIWebView *)webView{
        //access your request
        webView.request;
    }
    - (void)webViewDidFinishLoad:(UIWebView *)webView{
        //access your request 
        webView.request;    
    }
    - (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
        NSLog(@"could not load the website caused by error: %@", error);
    }
    -(void)dealloc{
    [webView release];
    [super dealloc];
    }
    

    You can load the webpage with NSURLRequest directly into your webview. With the delegate callback methods you can change the behavior of your webview.

    With this piece of code you should get it done.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Disclaimer: I'm fairly new to python! If I want all the lines of a
Ok, I'm fairly new to Xcode, and I've created a few sample projects and
First of all, I'm fairly new to Java, so sorry if this question is
First of all, I'm fairly sure snapping to grid is fairly easy, however I've
I have a fairly standards compliant XHTML+CSS site that looks great on all browsers
While I understand this question is fairly vague since I'm not giving you all
All front-end developers know the pain of coding for Firefox, then viewing our then
All I know about the constraint is it's name ( SYS_C003415 ), but I
Being fairly new to C++ I have a question bascially concerning the g++ compiler
Am fairly new to using MySQL and a total novice at Perl but am

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.