认为没有用?

时间:2015-05-09 16:37:48

标签: ios objective-c uiwebview

我围绕Feedly API构建应用。因此,在用于登录的UIWebView中,我使用以下代码:

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    NSString *URLString = [[request URL] absoluteString];

    NSString *urlStart = [URLString substringToIndex:23];

    if ([urlStart isEqualToString:@"http://localhost/?code="])
    {
        NSString *haystack = request.URL.absoluteString;
        [self useURL:haystack];
    }

    return YES;
}

如您所见,这会调用方法useURL:

- (void)useURL:(NSString *)haystack {
    NSString *prefix = @"http://localhost/?code=";
    NSString *suffix = @"&state=";suffix!
    NSRange needleRange = NSMakeRange(prefix.length,
                              haystack.length - prefix.length - suffix.length);
    NSString *needle = [haystack substringWithRange:needleRange];
    NSLog(@"needle: %@", needle);

    NSString *valueToSave = needle;
    [[NSUserDefaults standardUserDefaults] setObject:valueToSave forKey:@"AuthCode"];
    [[NSUserDefaults standardUserDefaults] synchronize];

    NSString *client_id = @"id";
    NSString *client_secret = @"secret";
    NSString *redirect_uri = @"http://localhost";
    NSString *state = @"";
    NSString *grant_type = @"authorization_code";

    NSInteger success = 0;
    @try {
        NSString *post =[[NSString alloc] initWithFormat:@"code=%@&client_id=%@&client_secret=%@&redirect_uri=%@&state=%@&grant_type=%@",needle,client_id,client_secret,redirect_uri,state,grant_type];
        NSLog(@"PostData: %@",post);

        NSURL *url=[NSURL URLWithString:@"https://cloud.feedly.com/v3/auth/token"];

        NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

        NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];

        NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
        [request setURL:url];
        [request setHTTPMethod:@"POST"];
        [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
        [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
        [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
        [request setHTTPBody:postData];

        //[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[url host]];

        NSError *error = [[NSError alloc] init];
        NSHTTPURLResponse *response = nil;
        NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

        NSLog(@"Response code: %ld", (long)[response statusCode]);

        if ([response statusCode] >= 200 && [response statusCode] < 300)
        {
            NSString *responseData = [[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
            NSLog(@"Response ==> %@", responseData);

            NSError *error = nil;
            NSDictionary *jsonData = [NSJSONSerialization
                              JSONObjectWithData:urlData
                              options:NSJSONReadingMutableContainers
                              error:&error];

            NSString *accessToken = jsonData[@"access_token"];
            NSString *refreshToken = jsonData[@"refresh_token"];

            //[self performSegueWithIdentifier:@"presentNewView" sender:self];
            NewViewController *newView = [[NewViewController alloc] init];
            [self presentViewController:newView animated:NO completion:nil];
        } else {
            NSLog(@"Failed.");
        }
    }
    @catch (NSException * e) {
        NSLog(@"Exception: %@", e);
    }
}

大多数工作都很好,直到下一个视图应该进入的部分。我尝试了一个segue,我尝试了这里可以看到的方式,但两个都继续投掷Exception: *** -[NSURL initFileURLWithPath:]: nil string parameter

我在这里做错了什么?

1 个答案:

答案 0 :(得分:0)

如您的错误消息所示,您有一个零字符串。您可以在类和LLDB内使用断点,在调试器中使用po stringName在整个生命周期内打印所述字符串的值。找到它失去价值的地方,你就得到了答案。如果您是LLDB或断点的新手,可以使用以下一些有用的链接来帮助您快速完成此操作:

Breakpoints Guide

LLDB Commands

如果您对使用NSLog感觉更舒服,那么将它们放在对象生命周期中也会有所帮助,但我会按照第一个建议进行操作。