NSData dataWithContentsOfURL: not returning data for URL that shows in browser

前端 未结 5 1000
无人及你
无人及你 2020-12-18 16:10

I am making an iOS client for the Stack Exchange API. After a long, drawn out fight I finally managed to implement authentication - which gives me a token I stick into a URL

相关标签:
5条回答
  • 2020-12-18 16:39

    NSCocoaErrorDomain Code=256 actually means a "file system or file I/O related error whose reason is unknown".

    Why you get this error is likely because using dataWithContentsOfURL: will not work with that remote URL - or maybe because of the query params which contain the authentication and the token. Thus, you get the "weird" error.

    In general, NSData's dataWithContentsOfURL: should only be use to access local file resources.

    In order to solve your problem, you should improve your code in two steps:

    1) Use NSURLConnection's convenient class method

    + (void)sendAsynchronousRequest:(NSURLRequest *)request queue:(NSOperationQueue *)queue completionHandler:(void (^)(NSURLResponse*, NSData*, NSError*))handler

    The block defines what to do with the response data when the request finished. Generally, first check the error parameter, then status code of the response and Content-type - in this order.

    2) Replace the former with your own instance method (or one from a third party) with a similar signature but which is much more sophisticated.

    Approach #2 enables you to implement and use the following important features

    • Cancellation
    • customize authentication in every aspects
    • load body data to files
    • process received chunks simultaneous
    • perform multiple requests in a queue which controls the number of simultaneous connections

    and a couple more.

    Approach #2 is oftentimes implemented as a subclass of NSOperation and encapsulates a NSURLConnection object (which you need to cancel the connection).

    You'll find answers of how to use NSURLConnection in asynchronous mode implementing the delegates. Also, there are third party solutions.

    You might find the official documentation invaluable, too: Using NSURLConnection

    For a quick start, you may take a look at my "Simple GET request" class on Gist:

    SimpleGetHTTPRequest

    This class is NOT based on NSOperation, but it can be modified easily. Consult the official documentation of NSOperation how to make a subclass. This is basically easy, but has a few important things (KVO) which you should get correct.

    0 讨论(0)
  • 2020-12-18 16:43

    In my case, adding AppTransportSecuritySettings dictionary into info.plist and setting key AllowArbitraryLoads to true.

    Fixed my problem...

    Hope it helps new developers.

    0 讨论(0)
  • 2020-12-18 16:46

    Your URL might be having a space that's why it returns nil.Just replace the space in URL with a '+' :

    stringByReplacingOccurrencesOfString:@" " withString:@"+"
    
    0 讨论(0)
  • 2020-12-18 16:47

    If you are trying to access a remote url via HTTP and using XCode 7 or later you may get a NSCocoaErrorDomain returned from [NSData dataWithContentsOfURL:].

    The root cause of this may actually be your "App Transport Security Settings". By default iOS doesn't allow arbitrary loading of URLS.

    0 讨论(0)
  • 2020-12-18 17:02

    I had this problem when I changed my folder structure. It gave me other NSError codes such as 512 and 4 for any file operations (local and web). The solution was to delete my IOS Simulator folders (Library\Developer\CoreSimulator).

    0 讨论(0)
提交回复
热议问题