WatchOS 2 on the device does not read NSData

In my watchOS 2 Apple Watch WatchKit Extension, I use NSURL and NSData to request the URL and get the sent JSON. However, this only works in the simulator. At first I had a problem, because I used an unreliable address (internal IP address), but after adding some keys and values ​​to Info.plist, this problem was fixed. To make sure the URL was trusted, I used the public URL from the GitHub API ( https://api.github.com/users/mralexgray/repos ). I use the following code to get a response:

var responseData: NSData? = nil
if let url = NSURL(string: "https://api.github.com/users/mralexgray/repos") {
    if let data = NSData(contentsOfURL: url){
        responseData = data
    }
}

In my watchOS 2 simulator, responseData fills correctly, but on my Apple Watch it looks like line number 3 and its body is simply skipped. Is it a problem, a function, or am I something wrong?

+4
source share
1 answer

It sounds like an error in the NSData method, contentsOfURL: option: error: says the file does not open.

NSURLSession is working fine.

NSURL * url = [NSURL URLWithString:@"https://api.github.com/users/mralexgray/repos"];

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

[downloadTask resume];
+2
source

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


All Articles