1

IOS version is 9, Xcode 7 and following code is not working. But it works with IOS version 8, Xcode 6.

I'm using codes below to download a file from server :

 NSURLSessionConfiguration *configuration;
if ([[[UIDevice currentDevice] systemVersion] compare:@"8.0" options:NSNumericSearch] != NSOrderedAscending) {
    configuration = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:@"Test"];
} else {
    configuration = [NSURLSessionConfiguration backgroundSessionConfiguration:@"Test"];
}
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://hw13.asset.aparat.com/aparat-video/e361693675a47fa4ae758756b40a11653215108-360p__71758.mp4"]];
NSProgress *progress;

NSURLSessionDownloadTask *downloadTask =   [manager downloadTaskWithRequest:request progress:&progress destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
    AppDelegate *appDelegate = [UIApplication sharedApplication].delegate;

    NSString *fileAddress = [[appDelegate GetDocumentDirectory] stringByAppendingPathComponent:@"Myfile.mp4"];
    return [NSURL fileURLWithPath:fileAddress];

} completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
    if(error != nil){
        //ERROR
        [[NSFileManager defaultManager] removeItemAtPath:[filePath absoluteString]
                                                   error:nil];
        [progress removeObserver:self forKeyPath:@"fractionCompleted" context:NULL];
        [downloadTask cancel];
        return ;
    }
    //SUCCESS
    [progress removeObserver:self forKeyPath:@"fractionCompleted" context:NULL];
}];

[downloadTask resume];
[progress addObserver:self
           forKeyPath:@"fractionCompleted"
              options:NSKeyValueObservingOptionNew
              context:NULL];

keeps giving the error:

Error Domain=NSURLErrorDomain Code=-1 "unknown error" UserInfo={NSErrorFailingURLKey=http://hw13.asset.aparat.com/aparat-video/e361693675a47fa4ae758756b40a11653215108-360p__71758.mp4, NSLocalizedDescription=unknown error, NSErrorFailingURLStringKey=http://hw13.asset.aparat.com/aparat-video/e361693675a47fa4ae758756b40a11653215108-360p__71758.mp4}

here is GetDocuemntsD

-(NSString *)GetDocumentDirectory{
self.fileMgr = [NSFileManager defaultManager];
self.homeDir = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
return self.homeDir;
}

*** i can save images to the document directory, but i want to download video files in background and show the progress to the user but it keeps failing.

and my info.plist has:

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>
farzadshbfn
  • 2,710
  • 1
  • 18
  • 38

2 Answers2

1

iOS 9 added some restrictions when using HTTP, you need to add an exception to your info.plist file to download from HTTP. More info here: NSURLSession/NSURLConnection HTTP load failed on iOS 9

Community
  • 1
  • 1
Ted Huinink
  • 846
  • 1
  • 7
  • 14
  • I did change my Plist. i can download images form server, the problem is i can't download videos in background – farzadshbfn Oct 15 '15 at 10:33
  • Look at this https://developer.apple.com/library/prerelease/ios/technotes/App-Transport-Security-Technote/ – Lorenzo Oct 15 '15 at 12:41
0

With iOS9 you have to check your plist file and add the ATS setting using this:

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

The ATS blocks the request to external server

Lorenzo
  • 3,293
  • 4
  • 29
  • 56