ios - 当 NSUrlSendAssynchronousrequest 操作运行时,选项卡栏应用程序按钮被阻止

标签 ios objective-c uiwebview nsurlconnection uitabbar

我有一个选项卡栏应用程序,其中一个按钮将网页内容加载到 UIWebView。 我面临的问题是,当连接尝试获取数据时,选项卡栏按钮没有响应,直到页面加载完成或连接失败。 是否有更好的方法来执行此操作,以便用户可以在他/她改变主意时切换出该选项卡。 谢谢,这是代码:

- (void)loadThrillsSite
{
    [self.webView setDelegate:self];
    NSString *urlString = @"https://www.Thrills.in";
    NSURL *url = [NSURL URLWithString:urlString];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];

    NSOperationQueue *queue = [[NSOperationQueue alloc] init];

    [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
     {
         if ([data length] > 0 && error == nil) [self.webView loadRequest:request];
         else if (error != nil) {
             UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Connection Error!" message:@"Failed to connect to server\nPlease check your internet connection and try again" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
             [alert show];
         }
     }];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView;
{
    [hudImageView removeFromSuperview];

    [self.spinner stopAnimating];

    [patternView removeFromSuperview];
}

最佳答案

UIWebView的loadRequest方法说明:

Connects to a given URL by initiating an asynchronous client request.

它已经进行了异步调用,因此您无需使用其他 block 。只需调用 [self.webView loadRequest:request]; 您可以在 - (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error 方法上处理错误。

其他注意事项:您的网址现在无法正常工作,并且不要执行任何与 UI 相关的工作,例如在后台线程上显示警报 View 。希望对您有所帮助。

- (void)loadThrillsSite
{
    [self.webView setDelegate:self];
    NSString *urlString = @"https://www.Thrills.in";
    NSURL *url = [NSURL URLWithString:urlString];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];

    [self.webView loadRequest:request];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView;
{
    [hudImageView removeFromSuperview];

    [self.spinner stopAnimating];

    [patternView removeFromSuperview];
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Connection Error!" message:@"Failed to connect to server\nPlease check your internet connection and try again" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
    [alert show];
}

关于ios - 当 NSUrlSendAssynchronousrequest 操作运行时,选项卡栏应用程序按钮被阻止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23284604/

相关文章:

iphone - 如何在 iOS 5、iOS 4 及更早版本上开发 Twitter

iphone - iPhone 上的 CSS 媒体查询

iphone - 从 HTML 文件链接到 View

android admob 未显示 - 屏幕尺寸太小

ios - 在 iOS 中仅使用模数和指数使用 RSA 加密字符串

ios - 如何从 iOs 中的 QuickDialog 获取值

iphone - 使用 iPhone API 拍照并将其传输到服务器

objective-c - 任何用于 Objective-C 的 XML 绑定(bind)框架?

iphone - 如何检查哪个类是在 Objective C 中初始化的对象

ios - 将 UIWebView 内容保存为 A4 大小的 PDF