ios 异步后台获取网络资源

ios9下面https 配置

 <key>NSAppTransportSecurity</key>
    <dict>
        <key>NSAllowsArbitraryLoads</key>
        <true/>
    </dict>

- (void)testNetWork
{    <span style="color:#ff0000;">//后台方式下获取网络资源
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{</span>
    
        NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"];
        
        NSError *error;
        NSString *data = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error];
        if (data != nil)
        {   <span style="color:#ff0000;"> 主线程中重新刷新界面UI
            dispatch_async(dispatch_get_main_queue(), ^{</span>
            
                NSLog(@"%@",data);
            });
        }
        
        else
        {
            NSLog(@"---------%@",error);
        
        }
    });

}


控制台打印的部分结果:
2015-10-13 22:11:29.070 CommonDemo[21656:298955] <!DOCTYPE html><!--STATUS OK--><html><head><meta http-equiv="content-type" content="text/html;charset=utf-8"><meta http-equiv="X-UA-Compatible" content="IE=Edge"><meta content="always" name="referrer"><meta name="theme-color" content="#2932e1"><link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" /><link rel="search" type="application/opensearchdescription+xml" href="/content-search.xml" title="百度搜索" /><link rel="icon" sizes="any" mask href="//www.baidu.com/img/baidu.svg"><link rel="dns-prefetch" href="//s1.bdstatic.com"/><link rel="dns-prefetch" href="//t1.baidu.com"/><link rel="dns-prefetch" href="//t2.baidu.com"/><link rel="dns-prefetch" href="//t3.baidu.com"/><link rel="dns-prefetch" href="//t10.baidu.com"/><link rel="dns-prefetch" href="//t11.baidu.com"/><link rel="dns-prefetch" href="//t12.baidu.com"/><link rel="dns-prefetch" href="//b1.bdstatic.com"/><title>百度一下,你就知道</title>


你可能感兴趣的:(ios 异步后台获取网络资源)