我无法在html解析过程中使back
按钮(导航中的上一页)工作。我要做的是解析一个html并在viewDidAppear
上获取一个pdf文件。在此期间,我希望启用back
按钮,因为解析可能需要很长时间。因此用户可以决定离开解析过程。
这是我的代码:
-(void)viewDidAppear:(BOOL)animated
{
[self performSelector:@selector(getPDFUrl) withObject:nil afterDelay:0];
}
-(void) getPDFUrl
{
NSURL *programURL = [NSURL URLWithString:@"http://www.example.com/somepdf/"];
NSData *programHtmlData;
@try
{
programHtmlData = [NSData dataWithContentsOfURL:programURL];
}
@catch(NSException* ex)
{}
// 2
TFHpple *programHTMLParser = [TFHpple hppleWithHTMLData:programHtmlData];
NSString *studiosXpathQueryString =
@"//div[@class='ultra_wrapper']/div[@class='container columns extra_pad boxed_lay centered']/div[@id='prk_ajax_container']/div[@id='centered_block']/div[@id='main_block']/div[@id='content']/div[@id='main']/div[@class='twelve columns sidebarized']/div[@class='prk_no_composer']/p/a";
NSArray *programNodes = [programHTMLParser searchWithXPathQuery:studiosXpathQueryString];
NSMutableArray *activities = [[NSMutableArray alloc] init];
Tutorial *tutorial;
if (programNodes.count > 0) {
for (TFHppleElement *element in programNodes)
{
@try
{
tutorial = [[Tutorial alloc] init];
tutorial.url = [element objectForKey:@"href"];
}
@catch(NSException* ex)
{
}
}
NSURL *targetURL = [NSURL URLWithString:tutorial.url];
webView.scalesPageToFit=YES;
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[webView loadRequest:request];
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"warning"
message:@"warning!"
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}
}
答案 0 :(得分:4)
[self performSelector:@selector(getPDFUrl) withObject:nil afterDelay:0];
将在主线程上执行。 因此阻止您的用户界面。
你应该使用
[self performSelectorInBackground:@selector(getPDFUrl) withObject:nil];
或者您可以使用NSOperationQueue
或NSOperation
或更简单的NSBlockOperation
。
NSBlockOperation *op = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"dasds");
}];
[op start];