Unable to convert a string to a NSURL - objective-c

I am trying to turn off WAL in CoreData using this code:
// Code to disable journaling mode
NSString *applicationDocumentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *urlString = [applicationDocumentsDirectory stringByAppendingPathComponent: #"saori.sqlite"];
NSURL *url = [[NSURL alloc] initWithString:urlString];
NSDictionary *options = #{NSSQLitePragmasOption:#{#"journal_mode":#"DELETE"}};
NSPersistentStoreCoordinator *psc = [[NSPersistentStoreCoordinator alloc] init];
[psc addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:url options:options error:nil];
This is my NSString that I need to convert to a URL:
/Users/rolfmarsh/Library/Application Support/iPhone Simulator/7.1/Applications/7A614C51-AC51-4F5B-9716-6E3D2F160324/Documents/saori.sqlite
When I use that string in this statement, it generates nil.
NSURL *url = [[NSURL alloc] initWithString:urlString];
I don't see anything wrong with the string. What am I doing wrong?

Read the docs for NSURL. You have a file path and you need to create a file URL from the file path.
NSURL *url = [NSURL fileURLWithPath:urlString];

Related

NSURL unused variable

I have generated a PDF file within my application which is saved to the Documents directory. I am trying to fetch the pdf to display within a UIWebView but i have an unused NSURL variable. Could this be the reason why the file is not loading properly? Here is my code which is attached to an IBAction button
The only way to get rid of that warning is to NSLog that NSURL. But I want to display that saved file.
This is the code I am using.
-(IBAction)pdf:(id)sender
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathExtension:#"client.pdf"];
NSURL *url = [NSURL fileURLWithPath:filePath];
NSString *urlString = [url absoluteString];
NSString *encodedString=[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *webURL = [NSURL URLWithString:encodedString];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self.myWebView loadRequest:request];
NSLog(#"%#", webURL);
}
I figured it out. I needed to change my filePath NSString from stringByAppendingPathExtension to stringByAppendingPathcomponent. I can now view my PDF file in my UIWebView.

How to save and retrieve files to apps temp folder in ios

I'm new to IOS development. I'm developing an app which involves downloading files and saving that to apps temp folder and I dont know how to do that my current code is given below
NSURL *tmpDirURL = [NSURL fileURLWithPath:NSTemporaryDirectory() isDirectory:YES];
NSLog(#"%#",tmpDirURL);
NSString *myString = [tmpDirURL absoluteString];
for(int i=0;i<responseArray.count;i++){
ASIHTTPRequest *saveUrl = [ASIHTTPRequest requestWithURL:responseArray[i]];
[saveUrl setDownloadDestinationPath:myString];
[request startSynchronous];
}
NSError * error;
NSArray * directoryContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:myString error:&error];
NSLog(#"%#",directoryContents);
The response array contain a list of URL for downloading files. I know something wrong with my code but I cant find out that error please help me to solve this problem
I found the solution'
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *htmlFilePath = [documentsDirectory stringByAppendingPathComponent:fileName];
[data writeToFile:htmlFilePath atomically:YES];
When downloadDestinationPath is set, the result of this request will be downloaded to the file at this location. If downloadDestinationPath is not set, download data will be stored in memory
NSURL *tmpDirURL = [NSURL fileURLWithPath:NSTemporaryDirectory() isDirectory:YES];
NSLog(#"%#",tmpDirURL);
NSString *myString = [tmpDirURL absoluteString];
for(int i=0;i<responseArray.count;i++){
ASIHTTPRequest *saveUrl = [ASIHTTPRequest requestWithURL:responseArray[i]];
[saveUrl setDownloadDestinationPath:[myString stringByAppendingPathComponent:[NSString stringWithFormat:#"%i",i ]]; // for each file set a new location inside tmp
[request startSynchronous];
}
NSError * error;
NSArray * directoryContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:myString error:&error];
NSLog(#"%#",directoryContents);

how to access the files of Nested folders in Skydrive API?

NSArray *arr = [[operation.result objectForKey:#"data"] valueForKey:#"name"];
NSArray *arrId = [[operation.result objectForKey:#"data"] valueForKey:#"id"];
NSArray *folder=[[operation.result objectForKey:#"data"]valueForKey:#"type"];
[dictDocument setObject:arrId forKey:#"ID"];
[dictDocument setObject:arr forKey:#"Name"];
[dictDocument setObject:folder forKey:#"type"];
Arrid===>folder.id,file.id
arr===>filename
folder====>type of file Eg.Folder,audio,video
I can't able to access the files of nested folders??
try like this,and once check in copy bundle identifiers files are added succeessfully or not.
NSString *filepath = [[NSBundle mainBundle] pathForResource:#"vid" ofType:#"mp4"];
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
avPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:fileURL error:nil];
avPlayer.delegate = self;
[avPlayer prepareToPlay];
[avPlayer play];
FOR PLAYING VEDIO
You can use MPMoviePlayerController to play local file.
refer this one for playing vedio file.

About xmlrpc of objective-c

Recently I have been working about an iOS project.I want to send my picture to an server.I use xmlrpc to do this ,but when I write codes like this:
- (void)uploadImage:(UIImage *)sourceImage ForName:(NSString *)imageName
{
NSData *imageData = UIImagePNGRepresentation(sourceImage);
NSString *imageString = [imageData base64EncodedString];
NSURL *URL = [NSURL URLWithString: SERVER_URL];
XMLRPCRequest *request = [[XMLRPCRequest alloc]
initWithURL:URL];
NSArray *params = [NSArray arrayWithObjects:imageName,imageString,nil];
[request setMethod:#"upload_image" withParameter:params];
I give "imageName" to params,it will fail.But if I write code like this
- (void)uploadImage:(UIImage *)sourceImage
{
NSData *imageData = UIImagePNGRepresentation(sourceImage);
NSString *imageString = [imageData base64EncodedString];
NSURL *URL = [NSURL URLWithString: SERVER_URL];
XMLRPCRequest *request = [[XMLRPCRequest alloc]
initWithURL:URL];
NSString *imageName = #"hello.png";
NSArray *params = [NSArray arrayWithObjects:imageName,imageString,nil];
[request setMethod:#"upload_image" withParameter:params];
It will be ok and I can upload the picture to the server.
I don't know why,can someone explain it ?

Is it possible for UIActivityViewController to attach AAC?

I tried this with no luck:
NSData *imageData = UIImageJPEGRepresentation([sight.photo valueForKey:#"image"], 0.8);
NSString *docsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *filePath = [docsPath stringByAppendingPathComponent:#"sound.m4a"];
NSURL *fileUrl = [NSURL fileURLWithPath:filePath];
NSData *aacData = [sight.sound valueForKey:#"soundrecord"];
[aacData writeToURL:fileUrl atomically:YES];
// NSString *text = self.textField.text;
NSArray *items = #[imageData, aacData];
Yes, but you need to pass the audio file URL to the activity sheet rather than the NSData.
NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0];
NSString *filePath = [docPath stringByAppendingPathComponent:#"sound.aac"];
NSURL *fileUrl = [NSURL fileURLWithPath:filePath isDirectory:NO];
NSArray *activityItems = #[fileUrl];
UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
[self presentViewController:activityVC animated:YES completion:nil];
If you find that the attachment doesn't appear when launching the email composer from the activity sheet, make sure your file path and URL are correct and that the file exists (including the extension on the device/simulator drive).
To open a file on the simulator, go to folder (~/Library/Application Support/iPhone Simulator/...
To check the file name on device, go to XCode Organizer Window and select the Applications tab within the debugging device connected. You should see the file structure on the main panel.