Попробуйте эту функцию;
ETA
Хорошо, я идиот, и я набрал код, не пытаясь сначала.
Это работает. У меня также есть простой проект Xcode , который работает с этим, который вы можете загрузить, чтобы попробовать сами, если я здесь что-то не так напечатал.
// Get the URL for the Password.txt file on the desktop.
NSURL *fileURL = [NSURL fileURLWithPath:[@"~/Desktop/Password.txt" stringByExpandingTildeInPath]];
// Read the contents of the file into a string.
NSError *error = nil;
NSString *fileContentsString = [NSString stringWithContentsOfURL:fileURL
encoding:NSUTF8StringEncoding
error:&error];
// Make sure that the file has been read, log an error if it hasn't.
if (!fileContentsString) {
NSLog(@"Error reading file");
}
// Create the string to search for
NSString *password = @"Password123";
// Search the file contents for the given string, put the results into an NSRange structure
NSRange result = [fileContentsString rangeOfString:password];
// -rangeOfString returns the location of the string NSRange.location or NSNotFound.
if (result.location == NSNotFound) {
// Password not found. Bail.
NSLog(@"Password not found in file");
return;
}
// Continue processing
NSLog(@"Password found in file");
}