Alterando o userAgent de NSURLConnection

Ei, estou usando uma conexão NSURL para receber dados.

[NSURLConnection sendSynchronousRequest: //create request from url [NSURLRequest requestWithURL: //create url from string [NSURL URLWithString:url] ] //request parameters returningResponse:nil error:nil ] 

É possível alterar a string do agente do usuário? agora mesmo é:

AppName / AppVersion CFNetwork / 459 Darwin / 10.0.0.d3

Obj-C:

 NSString* userAgent = @"My Cool User Agent"; NSURL* url = [NSURL URLWithString:@"http://whatsmyuseragent.com/"]; NSMutableURLRequest* request = [[[NSMutableURLRequest alloc] initWithURL:url] autorelease]; [request setValue:userAgent forHTTPHeaderField:@"User-Agent"]; NSURLResponse* response = nil; NSError* error = nil; NSData* data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; 

Rápido:

 let userAgent = "My Cool User Agent" if let url = NSURL(string: "http://whatsmyuseragent.com/") { let request = NSMutableURLRequest(URL: url) request.setValue(userAgent, forHTTPHeaderField: "User-Agent") var response:NSURLResponse? = nil; var error:NSError? = nil; if let data = NSURLConnection.sendSynchronousRequest(request, returningResponse: &response, error: &error) { // do something with your data } } 

Sim, você precisa usar um NSMutableURLRequest e definir um campo de header personalizado para a string do agente do usuário.