Changing the relative URL for an absolute URL in Objective-C

How can I take a file in the application directory (the same folder where the .app is located) and get its absolute path. Say the relative path is “Data / file.txt”, how would I use Objective-C to get this as an absolute path? I have a user who enters the name of their home folder if this is any help.

Please, help,

Higuy

+3
source share
3 answers

Find the location of the running application by doing:

NSString * applicationPath = [[NSBundle mainBundle] bundlePath];

Which will return something like:

/Users/myUser/Library/Application Support/iPhone Simulator/User/Applications/317CF5C7-57B3-42CE-8DF4-4DD10B070D95/Assignment1a.app

Then use - (NSString *)stringByDeletingLastPathComponentto cut the last bit.

+1

NSURL, URL-, :

NSURL * bundle = [[NSBundle mainBundle] bundleURL];
NSURL * file = [NSURL URLWithString:@"../Data/file.txt" relativeToURL:bundle];
NSURL * absoluteFile = [file absoluteURL];

"../" , . "../" , , , , . , -[NSBundle bundleURL] - API 10.6+, , NSURL -[NSBundle bundlePath].

+14

. NSHomeDirectory(), , , - :

NSString *path = [NSHomeDirectory() stringByAppendingString: @"/Data/file.txt"];
+1

Source: https://habr.com/ru/post/1729698/


All Articles