2014-10-09 179 views
1

dataWithContentsOfURL當主機無法連接並且存在Internet連接時,在iOS8上崩潰。dataWithContentsOfURL在iOS8上運行時崩潰

NSURL *url = [NSURL URLWithString:myurl]; 

    @try { 
     data = [NSData dataWithContentsOfURL:url]; 
    } 
    @catch (NSException *exception) { 
     data = nil; 
    } 
    if(data!=nil){ 
     [data writeToFile:filePath atomically:YES]; 
    }else{ 
     data = [NSData dataWithContentsOfFile:filePath]; 
     if (data==nil) { 
      return nil; 

     } 
    } 
+2

什麼是崩潰日誌? – 2014-10-09 08:44:15

+0

崩潰日誌不顯示任何崩潰 – user3347073 2014-10-09 08:47:57

回答

0

從文檔:

Important: Do not use this synchronous method to request network-based URLs. 
For network-based URLs, this method can block the current thread for tens of 
seconds on a slow network, resulting in a poor user experience, and in iOS, 
may cause your app to be terminated. 

如果您需要知道失敗的原因,用dataWithContentsOfURL:options:error:

或者

相反,對於非文件URL,可以考慮使用NSSession類的dataTaskWithURL:completionHandler:方法。