iphoneios5xcode4.2initwithcontentsofurl

initWithContentsOfURL suddenly gives a "deprecated" error


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.
}

Solution

  • 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.
    }