我的文件路径URL如下
给出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文件。
它已经是一个有效的编码URL。请勿将其注册两次。您正在将路径分离器变成%2F,并且该方案分离器变成%3A,因此它无法找到方案或路径组件。
在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];