Cocoa path string conversion - objective-c

I want to convert /Users/Irwan/Documents/test.jpg into MyMac:Users:Irwan:Documents:test.jpg
I can do that manually but I wonder if there is easy way to do it ?
thanks

NSURL *url = [NSURL fileURLWithPath:path];
path = (NSString *)CFURLCopyFileSystemPath((CFURLRef)url, kCFURLHFSPathStyle);
[path autorelease];

Related

Issues with escaping a NSString

I want to escape an NSString to use in NSURL. Here the line...
NSURL *url = [NSURL URLWithString:#"http://cdn.tutsplus.com/mobile/uploads/2013/12/sample.jpg" stringByAddingPercentEscapesUsingEncoding:NSUFT8StringEncoding];
But I get an error:
No known class method for selector: "URLWithString:string By Adding
Percent Escape Using Encodin"
Could you figured out what's the problem?
You mean surely:
NSURL *url = [NSURL URLWithString:[#"http://cdn.tutsplus.com/mobile/uploads/2013/12/sample.jpg" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
And take care of NSUFT8StringEncoding vs NSUTF8StringEncoding :)

document Path is showing nil for .doc file but working for .txt objective c cocoa

I am using below code
// it is showing nil for path
NSString *path = [[NSBundle mainBundle] pathForResource:#"filetext.doc" ofType:nil];
NSURL *url1 = [NSURL fileURLWithPath:path];
// it is showing working
NSString *path = [[NSBundle mainBundle] pathForResource:#"filetext.txt" ofType:nil];
NSURL *url1 = [NSURL fileURLWithPath:path];
Please help what is the issue
According to the docs, the method returns nil if the file cannot be found. The conclusion, therefore, is that the file is missing. Check your project's Build Phases/Copy Bundle Resources to ensure that the file is actually present because in all likelihood it is not.

iOS: Show PDF in WebView

I´m trying to show a pdf in a webview, but the webview is always white / blank.
if i log the path from the local file, it can be logged, so the file is there..
Here is my code:
NSString *path = [[NSBundle mainBundle] pathForResource:#"fileName" ofType:#"pdf"];
NSURL *targetURL = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[webView loadRequest:request];
(webView is an iboutlet)
Maik
Please make sure the User Interaction Enabled & Multiple Touch are enabled.
Your targetURL is maybe nil, please debug this.
If the targetUrl is nil, this is maybe the 'path' has space.
So you should encode the path:
//Encode Url
+(NSString *) urlEncodeString:(NSString *) str
{
return [str stringByReplacingOccurrencesOfString:#" " withString:#"%20"];
}
Controlling the whole characters rather than not just the space is more appropriate.Try this one for encoding the path of a file.
-(NSString *)urlenc:(NSString *)val{
CFStringRef safeString =
CFURLCreateStringByAddingPercentEscapes(NULL,
(CFStringRef)val,
NULL,
CFSTR("/%&=?$#+-~#<>|*,.()[]{}^!şığüöçĞÜŞİÖÇ"),
kCFStringEncodingUTF8);
return [NSString stringWithFormat:#"%#", safeString];
}

Why do I get this error when I try to pass a parameter in NSURL in iOS app?

This is what I have in a public method - (IBAction)methodName
NSString *quoteNumber = [[self textBox] text];
NSURL *url = [[NSURL alloc] initWithString:#"http://TestSite.com/virdirectory/Webservice1/Service1.asmx/GetQuote?number=%d", quoteNumber];
The error I get is:
Too many arguments to method call, expected 1, have 2
What am I doing wrong?
I think you are thinking of NSString's stringWithFormat::
[NSURL URLWithString:[NSString stringWithFormat:#"http://TestSite.com/virdirectory/Webservice1/Service1.asmx/GetQuote?number=%#", quoteNumber]]
Also note the change to %# for the format specifier, since it is an instance of NSString (not an int)
You need to format your string. Try this:
NSString *urlString = [NSString stringWithFormat:#"http://TestSite.com/virdirectory/Webservice1/Service1.asmx/GetQuote?number=%#", quoteNumber];
NSURL *url = [[NSURL alloc] initWithString:urlString];
The initWithString method can only accept a normal NSString, you are passing it a formatted NSString, Take a look at this code:
NSURL *url = [[NSURL alloc] initWithString:[NSString stringWithFormat:#"http://TestSite.com/virdirectory/Webservice1/Service1.asmx/GetQuote?number=%d", quotedNumber]];
That might be a bit confusing, you can break it up as follows:
NSString *urlString = [NSString stringWithFormat:#"http://TestSite.com/virdirectory/Webservice1/Service1.asmx/GetQuote?number=%d", quotedNumber];
NSURL *url = [[NSURL alloc] initWithString:urlString];
Now your string is properly formated, and the NSURL initWithString method will work!
Also, just so it is clearer for you in the future, you can take advantage of Objective-C's dot notation syntax when you set your quoteNumber string, as follows:
NSString *quoteNumber = self.textBox.text;
Also, you are trying to pass this quoted number into your urlString as a digit (as seen with the %d), remember that quotedNumber is a NSString object, and will crash when you try to pass it to the stringWithFormat method. You must convert the string first to a NSInteger, or NSUInteger.
Please refer to this SO question to how to do that (don't worry it's very easy)!
The problem is
[NSURL initWithString:]
requires ONE parameter of NSString type but you passed TWO parameters .
You need to pass a single NSString parameter . Change your code from
NSURL *url = [[NSURL alloc] initWithString:#"http://TestSite.com/virdirectory/Webservice1/Service1.asmx/GetQuote?number=%d", quoteNumber];
to
NSURL *url = [[NSURL alloc] initWithString:[NSString stringWithFormat:#"http://TestSite.com/virdirectory/Webservice1/Service1.asmx/GetQuote?number=%d", quoteNumber]];

Append NSString to NSURL?

I have an NSURL, a file path, and I want to add an NSString to the end of it (the file name) how can I do this? But after this is don't I want the entire thing to be an NSURL.
Thanks.
I think it's good solution:
NSURL *bUrl = [aUrl URLByAppendingPathComponent:#"newString"];
In Swift you could do the following,
var bURL = aURL.URLByAppendingPathComponent( "newString" )
You can also state whether the URL is a directory,
var bURL = aURL.URLByAppendingPathComponent( "newString", isDirectory: true )
I think it's as simple as:
NSString *s = [aUrl.path stringByAppendingString:#"newString"];
If you have a file NSURL to a directory and you want to end up with a NSString containing the NSURL's path with a file name appended to it, use this:
NSURL *url = [NSURL fileURLWithPath:#"/System" isDirectory:YES];
NSString *filename = #"foo";
NSString *result = [url.path stringByAppendingPathComponent:filename];
You can also use URLByAppendingPathComponent but that adds an extra step which creates an extra NSURL object that isn't needed.
NSURL *url = [NSURL fileURLWithPath:#"/System" isDirectory:YES];
NSString *filename = #"foo";
NSURL *newURL = [url URLByAppendingPathComponent:filename];
NSString *result = newURL.path;