iphone - 如何执行来自 iOS 应用程序的 URL 请求?

标签 iphone objective-c cocoa-touch url

我想向服务器发送以下请求。服务器已经知道如何处理它,但是我该如何发送它呢?

http://www.********.com/ajax.php?script=logoutUser&username=****

最佳答案

对于同步请求,您将执行以下操作:

NSURL *url = [NSURL URLWithString:@"http://www.********.com/ajax.php?script=logoutUser&username=****"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];

NSURLResponse *response;
NSError *error;
//send it synchronous
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
// check for an error. If there is a network error, you should handle it here.
if(!error)
{
    //log response
    NSLog(@"Response from server = %@", responseString);
}

更新:要进行异步请求,请参阅此 example

关于iphone - 如何执行来自 iOS 应用程序的 URL 请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10968794/

相关文章:

iphone - iPhone游戏的游戏中心集成?

iphone - 使用本地时区将 unix 时间戳转换为 NSdate

ios - 核心数据不覆盖对象

ios - 优化从相机胶卷中选取的图像以减小文件大小

ios - 无法使用内部使用 .xib 文件的 iOS 框架

cocoa-touch - NSNumberFormatter : string to Double

iphone - iPhone/iPad 上的 CGBitmapContextCreate

iphone - objective-c 中的子类化和继承

iphone - 第二次点击按钮不起作用

ios - 使用相同的本地化按钮切换语言应用程序