iPhone: How to send a HTTP request to a web service using Xcode

前端 未结 4 826
灰色年华
灰色年华 2021-02-02 03:03

How to send a HTTP request to a web service using Objective-C ? I need to fetch some data from my MySQL DB, so I need to send the request so I could fetch the data.

相关标签:
4条回答
  • 2021-02-02 03:37

    Edit, because this is a popular question and time keeps going on. In the meantime Apple introduced NSJSONSerialization. Have a look a the docs: https://developer.apple.com/documentation/foundation/jsonserialization Only if you need to create code for iOS earlier than 5.0 you may want to use the json-framwork as mentioned below.

    Besides that, the following original answer is still valid:

    Assuming that you exchange your data in JSON, you may be interested in reading this. http://iosdevelopertips.com/cocoa/json-framework-for-iphone-part-2.html

    However, the first part of that article answers your more general question on how to receive data from a web server/service:

    - (NSString *)stringWithUrl:(NSURL *)url
    {
        NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url
                                                        cachePolicy:NSURLRequestReturnCacheDataElseLoad
                                                        timeoutInterval:30];
            // Fetch the JSON response
        NSData *urlData;
        NSURLResponse *response;
        NSError *error;
    
        // Make synchronous request
        urlData = [NSURLConnection sendSynchronousRequest:urlRequest
                                            returningResponse:&response
                                                        error:&error];
    
        // Construct a String around the Data from the response
        return [[NSString alloc] initWithData:urlData encoding:NSUTF8StringEncoding];
    }
    

    This is a simple example of a http request and surely good enough for your next steps. This method is given a URL and it returns the data sent by the server in an NSString.

    Once that is working properly you will like to avoid that your app freezes while the request is "on the air". You can fix that by asynchronous requests. Google or the search on stackoverflow will provide you with links to examples and tutorials about that. But I suggest to go from here first, although this may not be the final solution.

    0 讨论(0)
  • 2021-02-02 03:52

    There are many ways, but perhaps the easiest is to use an existing framework, ASIHTTPRequest.

    See: http://allseeing-i.com/ASIHTTPRequest/

    0 讨论(0)
  • 2021-02-02 03:59

    Following is a lot of very famous library you can use for request

    ASIHTTPRequest

    AFNetwork

    MFNetwork

    You can search it in google, download in github or, yes, of course, why don't use Cocoapods. that's the best thing when i compare objective-c with other language.

    Good luck

    0 讨论(0)
  • 2021-02-02 03:59

    ASIHTTPRequest is a great and simple framework, so I second the recommendations from other posts. To be complete though, you should also know that you can do this natively in Objective-C without another framework. Look at the Apple documentation for the NSURLConnection and NSURLConnectionRequest classes for background.

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