i have the following code to try and load a local html page in a cocoa app...
NSString *basePath = #"file//Users/david/Documents/My Project/index.html";
NSString *escapedPath = [basePath stringByTrimmingCharactersInSet:
[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSURL*url=[NSURL URLWithString:escapedPath];
NSURLRequest*request=[NSURLRequest requestWithURL:url];
NSLog(#"current file is %#", url);
Unfortunately, the url is always null, and if i look at the value of escapedPath i see '%20' escaping the space. however, this doesn't work in local. is there a stringBy function that escapes correctly for a local path?
Thanks!
For local paths you need to use fileURLWithPath:
See the docs here:
https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSURL_Class/Reference/Reference.html
Related
Can anyone help me to find what's wrong with my code while reading file from desktop
NSString *filename=#"~/Users/user12345/Desktop/Sample/Data.txt";
NSString *fileString=[NSString stringWithContentsOfFile:filename encoding:NSUTF8StringEncoding error:nil];
NSArray *namesArray=[fileString componentsSeparatedByString:#"\n"];
for(NSString *names in namesArray)
{
NSLog(#"names:%#",names);
}
If you want to use the tilde – which represents /User/<currentUser>/ – you have to ...expandingTildeInPath and remove /Users/user12345
NSString *filename = [#"~/Desktop/Sample/Data.txt" stringByExpandingTildeInPath];
that makes the path independent of the current user name, otherwise remove the tilde:
NSString *filename = #"/Users/user12345/Desktop/Sample/Data.txt";
Caveat: If your app is sandboxed the path does not point to the visible desktop.
NSString *filename=#"~/Users/user12345/Desktop/Sample/Data.txt";
The ~ used in a path is a convention which means your home directory, but it doesn't work in all contexts (e.g. when used in -stringWithContentsOfFile:`) and you've supplied an absolute path anyway. Remove it from the front of your path
NSString *filename=#"/Users/user12345/Desktop/Sample/Data.txt";
and it should work as long as the file does actually exist at that path.
I am now recording audio file and saving to document directory. The given name contain space character. When I save to document directory, space characters are changed to %20. I would like to know how to save properly so that that audio file name contain space character.
NSString *myDBnew = [documentsDirectory stringByAppendingPathComponent:recordingAudioName ];
NSURL *recordedTmpFile = [NSURL fileURLWithPath:myDBnew];
Edited - This is to do audio recording and save to local directory.
recorder = [[ AVAudioRecorder alloc] initWithURL:recordedTmpFile settings:recordSetting error:&error];
You're using NSURL and spaces are invalid characters in a URL. That's why it's converted to a %20.
please use
-(BOOL)writeToFile:options:error:
instead of
-(BOOL)writeToURL:options:error:.
writeToFile takes a NSString as argument (not a NSURL). The spaces are preserved this way.
NSString *myDBnew = [documentsDirectory stringByAppendingPathComponent:recordingAudioName];
NSerror *error;
if (![data writeToFile:myDBnew options:0 error:&error]) {
// Error handling
}
I get the html source of a page to a NSString like this
NSString* url = #"example url";
NSURL *urlRequest = [NSURL URLWithString:url];
NSError *err = nil;
NSString *response = [NSString stringWithContentsOfURL:urlRequest encoding:kCFStringEncodingUTF8 error:&err];
a part of the response is like : 2 \u00cf\u0083\u00cf\u0087\u00cf\u008c\u00ce\u00bb\u00ce\u00b9\u00ce\u00b1
How can i have the Greek characters shown as they should in the NSString response?
The encoding of the page is "charset=iso-8859-7"
Ahhh, I understand your question a little bit better now.
The Apple-supplied native implementation of NSString doesn't know what to do with iso-8859-7 encoding.
You have two options.
1)
Try requesting different encodings to [NSString stringWithContentsOfURL: encoding: error:] to see if one successfully loads. My first attempt would be with NSISOLatin1StringEncoding.
2)
I found a third party library (and NSString category extension) that does do iso-8859-7 conversion. But to get access to CkoCharset will cost you (or your client) $290 USD. It might be a worthwhile investment to save time & hassle.
https://chilkatsoft.com/charset-objc.asp
and documentation is here:
http://www.chilkatsoft.com/refdoc/objcCkoCharsetRef.html
I am trying to get HTML files from the web, using stringWithContentsOfURL:. My problem is, sometimes it works but sometimes it doesn't. For example, I tried:
NSString *string = [NSString stringWithContentsOfURL:
[NSURL URLWithString:#"http://www.google.com/"]
encoding:encoding1
error:nil];
NSLog(#"html = %#",string);
This works fine, but when I replace the URL with #"http://www.youtube.com/" then I only get "NULL". Is there anyone that knows what's going on? Is it because of YouTube having some sort of protection?
Google's home page uses ISO-8859-1 encoding (aka "Latin-1", or NSISOLatin1StringEncoding). YouTube uses UTF-8 (NSUTF8StringEncoding), and the encoding you've specified with your encoding1 variable has to match the web page in question.
If you just want the web page and don't really care what encoding it's in, try this:
NSStringEncoding encoding;
NSError *error;
NSString *string = [NSString stringWithContentsOfURL:
[NSURL URLWithString:#"http://www.google.com/"]
usedEncoding:&encoding
error:&error];
NSLog(#"html = %#",string);
This method will tell you what the encoding was (by writing it to the encoding variable), but you can just throw that away and focus on the string.
I've looked at quite a few of the related questions and cannot find a similar problem or a solution so my apologies if there is a duplicate out there somewhere.
Anyway, I'm trying to generate a file's NSURL to use with an NSXMLDocument. I have the following components:
const NSString * PROJECT_DIR = #"~/SP\\ BB/";
const NSString * STRINGS_FILE = #"Localizable.strings";
and construct the URL like so:
NSURL * stringsURL = [NSURL fileURLWithPath:[[NSString stringWithFormat:#"%#%#",PROJECT_DIR,STRINGS_FILE] stringByExpandingTildeInPath]];
however, the resulting path in the NSURL is:
file://localhost/Users/timothyborrowdale/SP2B/Localizable.strings
I have tried changing the PROJECT_DIR to
#"~/SP BB/"
#"~/SP\\\\ BB/" (changes to SP엀2B)
#"~/SP%20BB/"
#"~/SP\%20BB/"
with the same problem. I also tried typing out the file url completely and using [NSURL URLWithString:]
I have also tried using stringByAddingPercentEscapesUsingEncoding with both NSUTF8Encoding and NSASCCIEncoding and these have the same issue.
The NSString displays properly before being passed to NSURL or stringByAddingPercentEscapesUsingEncoding but has the problem once outputted from either.
Try this:
NSString *fnam = [#"Localizable" stringByAppendingPathExtension:#"strings"];
NSArray *parts = [NSArray arrayWithPathComponents:#"~", #"SP BB", fnam, (void *)nil];
NSString *path = [[NSString pathWithComponents:parts] stringByStandardizingPath];
NSURL *furl = [NSURL fileURLWithPath:path];
Foundation has a host of platform-independent, path-related methods. Prefer those over hard-coding path extension separators (often ".") and path component separators (often "/" or "\").
Try abandoning stringWithFormat: (never the right answer for stapling paths together) and stringByExpandingTildeInPath and using NSHomeDirectory() and stringByAppendingPathComponent: instead.
#"~/SP\\ BB/" (changes to SP엀2B)
How did you arrive at that conclusion?