Load rtf and pdf using UIWebview from local - ios

I knew this question is already asked here. But I am facing some issue which is not getting resolved.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsPath = [paths objectAtIndex:0];
NSString *fileCacheDirPath = [docsPath stringByAppendingPathComponent:kATTACHMENTS_FOLDER];
NSString *filePath = [fileCacheDirPath stringByAppendingPathComponent:_fileItemDataModel.name];
NSURL *url = [NSURL fileURLWithPath:filePath isDirectory:NO];
Error:
url :
file:///Users/Sanoj/Library/Developer/CoreSimulator/Devices/722-6542-4E14-9CC5-24F96EE305D9/data/Containers/Data/Application/B990D3-4311-B580-D2E00E75/Documents/AttachmentsFolder/Scrum_Methodology.pdf
Error Domain=NSURLErrorDomain Code=-1001 "The request timed out."
UserInfo={NSUnderlyingError=0x60800084d950 {Error
Domain=kCFErrorDomainCFNetwork Code=-1001 "The request timed out."
UserInfo={NSErrorFailingURLStringKey=file:///Users/Sanoj/Library/Developer/CoreSimulator/Devices/722CC1FA-6542-4E14-9CC5-24F96EE305D9/data/Containers/Data/Application/B992A9C0-A0D3-4311-B580-D2E0049CFE75/Documents/AttachmentsFolder/Scrum_Methodology.pdf,
NSErrorFailingURLKey=file:///Users/Sanoj/Library/Developer/CoreSimulator/Devices/722CC1FA-6542-4E14-9CC5-24F96EE305D9/data/Containers/Data/Application/B992A9C0-A0D3-4311-B580-D2E0049CFE75/Documents/AttachmentsFolder/Scrum_Methodology.pdf,
_kCFStreamErrorCodeKey=-2102, _kCFStreamErrorDomainKey=4, NSLocalizedDescription=The request timed out.}},
NSErrorFailingURLStringKey=file:///Users/Sanoj/Library/Developer/CoreSimulator/Devices/722CC1FA-6542-4E14-9CC5-24F96EE305D9/data/Containers/Data/Application/B992A9C0-A0D3-4311-B580-D2E0049CFE75/Documents/AttachmentsFolder/Scrum_Methodology.pdf,
NSErrorFailingURLKey=file:///Users/Sanoj/Library/Developer/CoreSimulator/Devices/722CC1FA-6542-4E14-9CC5-24F96EE305D9/data/Containers/Data/Application/B992A9C0-A0D3-4311-B580-D2E0049CFE75/Documents/AttachmentsFolder/Scrum_Methodology.pdf,
_kCFStreamErrorDomainKey=4, _kCFStreamErrorCodeKey=-2102, NSLocalizedDescription=The request timed out.}

it worked with following code
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsPath = [paths objectAtIndex:0];
NSString *fileCacheDirPath = [docsPath stringByAppendingPathComponent:kATTACHMENTS_FOLDER];
NSString *filePath = [fileCacheDirPath stringByAppendingPathComponent:_fileItemDataModel.name];
[self.webView loadData:_fileItemDataModel.data MIMEType:#"application/pdf" textEncodingName:#"" baseURL:[NSURL URLWithString:filePath]];
Please, check

Related

ios p12 odd behavior

I've an odd behavior with p12 certificates and amazon iot.
If i include a p12 file in the project ide, and connect to aws iot, every thing works fine
NSBundle *bundle = [NSBundle mainBundle];
NSString *resource = [bundle pathForResource:nil ofType:#"p12"];
certificate = [NSData dataWithContentsOfFile:resource];
[self continueConnect:certificate];
but if i download the certificate from the server and save into a local file
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *appFile = [documentsDirectory stringByAppendingString:#"/certificate.p12"];
certificate = [NSData dataWithContentsOfFile:appFile];
[self continueConnect:certificate];
I get the following error: CFNetwork SSLHandshake failed (-9829)
I've checked in a for loop that both certificates are indentical
any tip?

Webview not loading PDF file from Document Directory

I want to show a PDF file in web view. For that I write code like:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:#"201507234Surat_GDCR.pdf"];
self.pdfWeb.hidden=FALSE;
self.JantriImage.hidden=TRUE;
self.pdfWeb.delegate=self;
self.pdfWeb = [[UIWebView alloc] initWithFrame:CGRectMake(0,0,ScreenWidth,ScreenHeight-64)];
self.pdfWeb.scalesPageToFit = YES;
NSURL *url = [NSURL fileURLWithPath:filePath];
NSString *urlString = [url absoluteString];
NSString *encodedString=[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *webURL = [NSURL URLWithString:encodedString];
NSURLRequest *request = [NSURLRequest requestWithURL:webURL];
[self.pdfWeb loadRequest:request];
Here self.pdfWeb is my webview and file is already in document directory but webview can't load file.
Try this code:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:#"201507234Surat_GDCR.pdf”];
NSURL *url = [NSURL fileURLWithPath:filePath];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:#"201507234Surat_GDCR.pdf”];
success = [fileManager fileExistsAtPath:filePath];
if (success)
{
NSURL *url = [NSURL fileURLWithPath:filePath];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
webView.dataDetectorTypes=UIDataDetectorTypeAll;
}

Incorrect URL for AVURLAsset

I am preparing the URL for one method of AVURLAsset:
+ (AVURLAsset *)URLAssetWithURL:(NSURL *)URL options:(NSDictionary *)options
My URL belongs to a file which is written to the document path of my app. The result of NSLog the path is:
/var/mobile/Containers/Data/Application/E0E64E72-9A47-4E62-B9C6-818AB8BF3F4C/Documents/audio.wav
After got the NSString *path, I tried to convert NSString to NSURL by [NSURL URLWithString:path] and [NSURL fileURLWithPath:path], but both of them are incorrect for URLAssetWithURL.
Error of URLWithString is:
Error Domain=AVFoundationErrorDomain Code=-11800 "The operation could
not be completed" UserInfo=0x170269f80 {NSUnderlyingError=0x17405f440
"The operation couldn’t be completed. (OSStatus error -12780.)",
NSLocalizedFailureReason=An unknown error occurred (-12780),
NSLocalizedDescription=The operation could not be completed}
And fileURLWithPath crashed the app.
I know url = [[NSBundle mainBundle] URLForResource:fileURL withExtension:#"wav"] will fit the situation. But I want to get the url of a local file to fit URLAssetWithURL as well. Any idea? Thanks.
This way of getting a file from the documents should work:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [[paths.firstObject stringByAppendingPathComponent:#"file"] stringByAppendingPathExtension:#"wav"];
if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
NSURL *url = [NSURL fileURLWithPath:path];
}

Getting Exc_Bad_access in AVAssetReader

I am getting Exc_bad_Access exception while reading a file with AVAssetReader .
The File is located in document directory and is in m4a format. Here is the code i am using. Not able to figure out where m getting it wrong:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filepath = [documentsDirectory stringByAppendingPathComponent:#"My_Recording2.m4a"];
NSURL *newURL=[NSURL URLWithString:filepath];
if ([[NSFileManager defaultManager] fileExistsAtPath:newURL.absoluteString])
{
NSLog(#"File exists");
}
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:newURL options:nil];
NSError *assetError;
if(asset)
NSLog(#"all ok");
self.assetReader = [AVAssetReader assetReaderWithAsset:asset error:&assetError];
I am getting the exception at last line i.e.
self.assetReader = [AVAssetReader assetReaderWithAsset:asset error:&assetError];
I think your [NSURL URLWithString:filepath] is not right.
Can you try with fileURLWithPath method. Hopefully that will solve your issue.

Download from protected with password FTP server with use of CFNetwork

I am making an app that can dowload from FTP server that protected with username and password file and saved it in documentsDirectory. I have managed to download file directly from HTTP, save and dispaly it. Now I wish to do so from FTP. Here is my code:
-(IBAction)download:(id)sender
{
NSString *stringURL = #"http://www.image.png";
NSURL *url = [NSURL URLWithString:stringURL];
NSData *urlData = [NSData dataWithContentsOfURL:url];
if ( urlData )
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:#"%#/%#", documentsDirectory,#"filename3.png"];
[urlData writeToFile:filePath atomically:YES];
}
}
-(IBAction)viewImage:(id)sender
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *getImagePath = [documentsDirectory stringByAppendingPathComponent:#"filename3.png"];
UIImage *img = [UIImage imageWithContentsOfFile:getImagePath];
[imageV setImage:img];
}
Can anyone please point me to CFNetwork example. I need to connect to ftp server, list files in folder and download them. Already looked at http://developer.apple.com/library/ios/#Documentation/Networking/Conceptual/CFNetwork/CFFTPTasks/CFFTPTasks.html#//apple_ref/doc/uid/TP30001132-CH9-SW1 but it`s not so clear.
Consider using BlackRaccoon to work with FTP.

Resources