contentsOfURL Error

I get the following error when I try and run the app on my watch:



Error Domain=NSCocoaErrorDomain Code=256 "The file “GetPic.php” couldn’t be opened." UserInfo={NSURL=https://website.com/GetPic.php}


The actual line of code is:


let URL: String = try String(contentsOfURL: NSURL(string: "https://website.com/GetPic.php")!)


This works fine in xcode. I updated to xCode 7.1 and am running the latest watchos version. Any ideas how to fix this?


Thanks

Answered by konfu in 63252022

Firstly, the url returns 404 now. And it seems there is a bug in NSData implementation. You may use NSURLSession instead.


    NSURL * url = [NSURL URLWithString:@"https:..."];
    NSURLSessionDataTask *downloadTask = [[NSURLSession sharedSession]
                                          dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
                                              NSLog(@"data size %ld", (unsigned long)data.length);
                                          }];
  
    [downloadTask resume];
Accepted Answer

Firstly, the url returns 404 now. And it seems there is a bug in NSData implementation. You may use NSURLSession instead.


    NSURL * url = [NSURL URLWithString:@"https:..."];
    NSURLSessionDataTask *downloadTask = [[NSURLSession sharedSession]
                                          dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
                                              NSLog(@"data size %ld", (unsigned long)data.length);
                                          }];
  
    [downloadTask resume];
contentsOfURL Error
 
 
Q