2015-05-09 59 views
0

我正在圍繞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); 
    } 
} 

大多數的正常工作,直到部分地方下一個視圖應該進來。我嘗試了一個賽格,我嘗試了在這裏可以看到的方式,但都保持投擲Exception: *** -[NSURL initFileURLWithPath:]: nil string parameter

我在做什麼錯在這裏?

+0

這個錯誤發生在哪裏?什麼線? – luk2302

+0

@ luk2302我沒有找到一行,但是當我註釋掉segue和/或'presentViewController'時,我沒有收到錯誤。 – user4486205

+0

是否在您的NewViewController中調用了initFileURLWithPath?如果是這樣,也許在嘗試並呈現它之前,您沒有傳遞URL字符串。 – Gismay

回答

0

正如您的錯誤消息所述,您有一個零字符串。您可以在類中使用斷點,並使用LLDB在調試器中使用po stringName來在整個生命週期中打印該字符串的值。找到它失去價值的地方,你就有了答案。下面是一些有用的鏈接幫助,如果你是新來LLDB您快速做到這一點或斷點:

Breakpoints Guide

LLDB Commands

如果你更舒適的使用NSLog,將它們放置在對象生命週期也可能有所幫助,但我會首先提出建議。