【问题标题】:Add custom headers to Cloud Endpoints call in iOS (Swift) using iOS client library使用 iOS 客户端库将自定义标头添加到 iOS (Swift) 中的 Cloud Endpoints 调用
【发布时间】:2017-01-21 07:05:17
【问题描述】:

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

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

Modify HTTP headers in Google App Engine Endpoints (Android)

谢谢!

【问题讨论】:

  • Google Cloud Endpoints 是否有您正在使用的 iOS 框架?

标签: ios google-app-engine google-cloud-endpoints


【解决方案1】:

您可以使用与每个 GTLService 基类关联的附加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;

【讨论】:

    【解决方案2】:

    我不熟悉谷歌应用引擎,但我查看了他们的 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"];
    

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-06
      • 2014-05-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多