我有文件路径网址,如下所示
http://ccidahra.com/wp-content/uploads/2016/03/sample.ppt
当我在浏览器中打开时,浏览器会提供下载文件的选项。这意味着URL正确,下载的文件也是打开的,因此文件也正确。但是当我尝试在iOS中使用代码执行相同操作时。我收到了错误。我在下面写了代码来下载文件。
NSString *fullURL = @"http://ccidahra.com/wp-content/uploads/2016/03/sample.ppt";
NSString *encodedURL = [fullURL stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLHostAllowedCharacterSet]];
NSURL *url = [NSURL URLWithString:encodedURL];
NSLog(@"fullURL %@",fullURL);
NSURLSession *sessions = [NSURLSession sharedSession];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:url];
NSURLSessionDataTask *dataTask = [sessions dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (!error) {
NSString *docPath = [[AppDelegate documentsDirectory] stringByAppendingString:@"/sample.ppt"];
if ([[NSFileManager defaultManager] fileExistsAtPath:docPath]){
[[NSFileManager defaultManager ]removeItemAtPath:docPath error:nil];
}
if ([[NSFileManager defaultManager] createFileAtPath:docPath contents:data attributes:nil]) {
NSLog(@"file created ar %@",docPath);
}
else{
NSLog(@"file can not created ar %@",docPath);
}
}else
{
NSLog(@"Error %@",error);
dispatch_async(dispatch_get_main_queue(), ^{
[MBProgressHUD hideHUDForView:self.view animated:YES];
});
[AlertView showAlertWithTitle:@"Error" withMessage:@"Service error! Please connect to the internet" inViewController:self];
}
}];
[dataTask resume];
下载文件时出现以下错误。
Error Domain=NSURLErrorDomain Code=-1002 "unsupported URL" UserInfo={NSUnderlyingError=0x608000253fb0 {Error Domain=kCFErrorDomainCFNetwork Code=-1002 "(null)"}, NSErrorFailingURLStringKey=http%3A%2F%2Fccidahra.com%2Fwp-content%2Fuploads%2F2016%2F03%2Fsample.ppt, NSErrorFailingURLKey=http%3A%2F%2Fccidahra.com%2Fwp-content%2Fuploads%2F2016%2F03%2Fsample.ppt, NSLocalizedDescription=unsupported URL}
请帮我解决这个问题。我想在UIWebview中打开ppt,pptx,doc文件。
答案 0 :(得分:0)
它已经是一个有效的编码网址。不要对其进行两次URL编码。您将路径分隔符转换为%2F,将方案分隔符转换为%3A,因此无法再找到方案或路径组件。
答案 1 :(得分:0)
在Swift 3.0代码中
@IBOutlet weak var myWeb: UIWebView!
let url = URL(string: "Your URL")
let urlRequest = URLRequest(url: url!)
myWeb.scalesPageToFit = true
myWeb?.loadRequest(urlRequest)
目标C
NSString *urlString = @"Your Url";
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
_myWeb.scalesPageToFit = true;
[_myWeb loadRequest:request];