2013-07-28 127 views
0

下面是問題,我使用AFNetwoking獲取頁面html返回。 我可以看到responseObject是一個html頁面。 這是我使用的代碼。如何從html代碼加載頁面

NSURL *url = [NSURL URLWithString:@"https://graph.z.qq.com/moc2/authorize"]; 
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url]; 


NSMutableDictionary *params = [[NSMutableDictionary alloc] init]; 
[params setValue:@"code" forKey:@"response_type"]; 
[params setValue:@"100266567" forKey:@"client_id"]; 
[params setValue:@"http://www.qq.com" forKey:@"redirect_uri"]; 

NSMutableURLRequest *request = [httpClient requestWithMethod:@"GET" path:@"https://graph.z.qq.com/moc2/authorize" parameters:params]; 

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; 
[httpClient registerHTTPOperationClass:[AFHTTPRequestOperation class]]; 

__block NSData * data = [[NSData alloc] init];

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject){ 
    data = responseObject; 
    NSLog(@"Response: %@", [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]) ; 
} failure:^(AFHTTPRequestOperation *operation, NSError *error){ 
    NSLog(@"Error: %@", error); 
}]; 

[operation start]; 

現在我想將響應對象加載到uiwebview中。 我試過loadHTMLString,但沒有奏效。

[webView loadHTMLString:[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] baseURL:url ]; 

有沒有人有這方面的想法?

回答

0

它看起來不像你正在加載任何處理程序塊中的數據。

AFNetworking是異步的。因此,如果您在返回任何數據之前嘗試加載數據,則不會發生任何事情。

將加載代碼放在您記錄數據的相同位置。

+0

是的,你說得對,我使用AFNetworking獲取返回數據。回報數據就是我想要的。但現在問題是返回數據只是一個NSData類型的對象。我不知道如何使用它,以uiwebview或其他方式顯示它。 – zedzhao

0

嘗試重寫此方法,並返回YES:

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType 
{ 
    return YES; 
} 

記得設置你的類UIWebViewDelegate和你的UIWebView的.delegate屬性鏈接到self