使用 iOS 客户端库将自定义 headers 添加到 iOS (Swift) 中的 Cloud Endpoints 调用

Add custom headers to Cloud Endpoints call in iOS (Swift) using iOS client library

在 iOS 应用程序(使用 Swift)中执行 API 调用 Cloud Endpoints 时如何添加自定义 headers?

这里是针对 Android 回答的同一个问题,但我在任何地方都找不到 iOS 示例:

谢谢!

我不熟悉 google 应用引擎,但我看了一下他们的 iOS 示例应用 tic tac toe。所以那里没有什么复杂的。他们使用标准 iOS HTTP 请求。

你可以找到下面几行here

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:infoURL];

NSString *userAgent = [auth userAgent];
[request setValue:userAgent forHTTPHeaderField:@"User-Agent"];
[request setValue:@"no-cache" forHTTPHeaderField:@"Cache-Control"];

将 header 添加到 NSMutableURLRequest 之后,您可以将此请求与 NSURLConnection 一起使用。

因此,如果您需要其他内容,可以在此处添加。 here is a good tutorial if you are new to iOS development.

您可以使用与每个 GTLService 基础 class 关联的附加 HTTPHeaders 属性。例如

var service = GTLServiceTictactoe()
service.retryEnabled = YES
service.additionalHTTPHeaders = ["my custom header name " : "my custom header value"]
....

这是文档

对此属性的描述
/**
 *  Any additional HTTP headers for this queries executed by this service.
 *
 *  Individual queries may have additionalHTTPHeaders specified as well.
 */
@property(atomic, copy, nullable) NSDictionary<NSString *, NSString *> *additionalHTTPHeaders;