1

IOS バージョンは 9、Xcode 7 で、次のコードが機能しません。ただし、IOS バージョン 8、Xcode 6 で動作します。

以下のコードを使用して、サーバーからファイルをダウンロードしています。

 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];

エラーが発生し続けます:

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}

ここに GetDocuemntsD があります

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

*** 画像をドキュメント ディレクトリに保存できますが、ビデオ ファイルをバックグラウンドでダウンロードして進行状況をユーザーに表示したいのですが、失敗し続けます。

そして私のinfo.plistには次のものがあります:

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

2 に答える 2

1

iOS 9 では、HTTP を使用するときにいくつかの制限が追加されました。HTTP からダウンロードするには、info.plist ファイルに例外を追加する必要があります。詳細はこちら: NSURLSession/NSURLConnection HTTP ロードが iOS 9 で失敗しました

于 2015-10-15T09:24:03.073 に答える
0

iOS9 では、plist ファイルを確認し、これを使用して ATS 設定を追加する必要があります。

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

ATS は外部サーバーへのリクエストをブロックします

于 2015-10-15T11:47:44.843 に答える