-1

I just opened my project in the new XCode 4.2 for the first time and I'm suddenly getting a whole slew of these warnings: 'initWithContentsOfURL:' is deprecated

Here's the code - anyone know what needs to be fixed here? (it was working perfectly fine in XCode 4.0)

- (void)viewDidLoad
{

    NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"TermsConditions" withExtension:@"html"];
    NSString *myHtml = [[NSString alloc] initWithContentsOfURL:modelURL];
    [self.TermsWebView loadHTMLString:myHtml baseURL:modelURL];
    [myHtml release];
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}
sirab333
  • 3,662
  • 8
  • 41
  • 54

5 Answers5

4

You need to use the method initWithContentsOfURL:usedEncoding:error:

- (void)viewDidLoad
{ 

    NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"TermsConditions" withExtension:@"html"];

    NSStringEncoding *encoding;
    NSError *error;

    NSString *myHtml = [[NSString alloc] initWithContentsOfURL:modelURL usedEncoding:&encoding error:&error];
    [self.TermsWebView loadHTMLString:myHtml baseURL:modelURL];
    [myHtml release];
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}
dtuckernet
  • 7,817
  • 5
  • 39
  • 54
  • 1
    I tried your code and I'm getting this warning now: "incompatible integer to pointer conversion sending 'int' to parameter of type NSStringEncoding". Any ideas? – sirab333 Jan 12 '12 at 12:01
  • I finally had a chance to drop this is into Xcode. I made a few small changes above. In this case, it determines the encoding for you - so you need to pass in a pointer to a variable of type of NSStringEncoding. After you fetch the data, this variable will be populated with the encoding of the file at the URL. – dtuckernet Jan 12 '12 at 14:33
  • that sorta worked. I still got warnings when I typed it as you suggested above, so I tweaked til it worked. I didn't & still don't understand what the '&' in front of 'encoding' and 'error' is all about (why are we using an '&' here? I've only seen '&' used in a 'scanf' statement...) - so I tweaked that. The warning-free version of the code I finally came up with is: **NSString *myHtml = [[NSString alloc] initWithContentsOfURL:modelURL usedEncoding:encoding error:&error];** So there's no '&' in front of 'encoding', and there IS an '&' in front if 'error' . Don't ask me y or how - it works :-) – sirab333 Jan 14 '12 at 03:55
  • The issue is that NSStringEncoding is an integer. So you need to drop the asterisk in this line of code: "NSStringEncoding *encoding;" – lifjoy Jan 30 '12 at 19:40
1

When you get a message that a given method is deprecated, check the documentation for information. In this case, you'll find that -initWithContentsOfURL: is no longer listed on the NSString reference page.

Another useful resource is the header file for the class in question. If you check NSString.h, you'll find:

- (id)initWithContentsOfURL:(NSURL *)url DEPRECATED_IN_MAC_OS_X_VERSION_10_4_AND_LATER;
Caleb
  • 124,013
  • 19
  • 183
  • 272
  • thank you for this. I do see it in the docs. However, when calling 'initWithContentsOfURL:usedEncoding:error:' I'm not sure what to pass into the 'usedEncoding' parameter. The suggestion made in the other reply to my question produces a warning. Any thoughts? – sirab333 Jan 12 '12 at 12:10
0

That method has been deprecated and replaced by

initWithContentsOfURL:enconding:error

or

initWithContentsOfURL:usedEnconding:error

Cheers

bitoiu
  • 6,893
  • 5
  • 38
  • 60
0

Try this out it seems to be error free

- (void)viewDidLoad

{

NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"TermsConditions" withExtension:@"html"];

NSStringEncoding *encoding = NULL;
NSError *error;

NSString *myHtml = [[NSString alloc] initWithContentsOfURL:modelURL usedEncoding:encoding error:&error];
[self.TermsWebView loadHTMLString:myHtml baseURL:modelURL];
[myHtml release];
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.

}

AMAN77
  • 6,218
  • 9
  • 45
  • 60
0

This seems to be the correct code:

NSStringEncoding *encoding = NULL; NSString *jsonReturn = [[NSString alloc]initWithContentsOfURL:url encoding:*encoding error:NULL];

The warnings will be eliminated only if , 1.you initialise encoding to NULL. 2.Put a * before encoding.