0
votes

Here's the method that's giving me an error when I compile (see the requestMainPage method)

- (void)loginToMistarWithPin:(NSString *)pin password:(NSString *)password success:(void (^)(void))successHandler failure:(void (^)(void))failureHandler {

    NSURL *url = [NSURL URLWithString:@"https://mistar.oakland.k12.mi.us/novi/StudentPortal/Home/Login"];

    //Create and send request
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];

    [request setHTTPMethod:@"POST"];

    NSString *postString = [NSString stringWithFormat:@"Pin=%@&Password=%@",
                            [self percentEscapeString:pin],
                            [self percentEscapeString:password]];
    NSData * postBody = [postString dataUsingEncoding:NSUTF8StringEncoding];
    [request setHTTPBody:postBody];

    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse
*response, NSData *data, NSError *error)
     {
         // do whatever with the data...and errors
         if ([data length] > 0 && error == nil) {
             NSError *parseError;
             NSDictionary *responseJSON = [NSJSONSerialization JSONObjectWithData:data options:0 error:&parseError];
             if (responseJSON) {
                 // the response was JSON and we successfully decoded it

                 NSLog(@"Response was = %@", responseJSON);

                 // assuming you validated that everything was successful, call the success block

                 if (successHandler)
                     successHandler();
             } else {
                 // the response was not JSON, so let's see what it was so we can diagnose the issue

                 NSString *loggedInPage = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
                 NSLog(@"Response was not JSON (from login), it was = %@", loggedInPage);

                 if (failureHandler)
                     failureHandler();
             }
         }
         else {
             NSLog(@"error: %@", error);

             if (failureHandler)
                 failureHandler();
         }
     }]; }

- (NSData *)requestMainPage {

    NSData *returner;
    //Now redirect to assignments page

    NSURL *homeURL = [NSURL URLWithString:@"https://mistar.oakland.k12.mi.us/novi/StudentPortal/Home/PortalMainPage"];
    NSMutableURLRequest *requestHome = [[NSMutableURLRequest alloc] initWithURL:homeURL];
    [requestHome setHTTPMethod:@"GET"]; // this looks like GET request, not POST

    [NSURLConnection sendAsynchronousRequest:requestHome queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *homeResponse, NSData *homeData, NSError *homeError) //Error is in this line.
     {
         // do whatever with the data...and errors
         if ([homeData length] > 0 && homeError == nil) {
             NSError *parseError;
             NSDictionary *responseJSON = [NSJSONSerialization JSONObjectWithData:homeData options:0 error:&parseError];
             if (responseJSON) {
                 // the response was JSON and we successfully decoded it

                 NSLog(@"Response was = %@", responseJSON);
             } else {
                 // the response was not JSON, so let's see what it was so we can diagnose the issue

                 NSString *homePage = [[NSString alloc] initWithData:homeData encoding:NSUTF8StringEncoding];
                 NSLog(@"Response was not JSON (from home), it was = %@", homePage);
                 return homePage;
             }
         }
         else {
             NSLog(@"error: %@", homeError);
         }
         return [NSString stringWithFormat:@"%@", homeData];
     }]; }

The error came up when I decided I wanted my method (requestMainPage) to have a return type of NSData * rather then just returning void.
So I'm not sure exactly where the problem is.

1
The code you posted has no errors or warnings, other than those caused by the fact that it's been cut off in the middle. - user3386109
Show all of your call to sendAsynchronousRequest including your block. - rmaddy
1. So where is your block? 2. You can't do it that way ;) since you send an asynchronous request the method requestMainPage returns immediately. If you want to work with blocks (they are great) you have to perform the action in the completion block rather than in the method you called requestMainPage from. - HAS
Please use your words careful. Code that "throws" means the code compiles, it runs, and throws an exception. It looks like the compiler is complaining about the code and gives an error. Say that. Be precise. You're a programmer, you must be precise. Since the declaration of the block starts with just ^ and no return type, the return type is derived from the code in the block. The method expects a block returning void, from your error it seems your block returns NSString*. Posting the block would have been useful; without this it is just a guess. - gnasher729
I'm sorry. I've edited my post to include the entire method - AndrewSB

1 Answers

0
votes

I figured out my mistake, you can't return values from inside a block.
So if I remove the return homeError and return [NSString stringWithFormat:@"%@", homeData];, the problems go away and I can compile