【问题标题】:Why won't startLoading be called in my NSURLProtocol subclass?为什么不在我的 NSURLProtocol 子类中调用 startLoading?
【发布时间】:2014-03-13 18:43:48
【问题描述】:

我正在查看 NSURLProtocol 并尝试插入一些标题。

- (void)startLoading {
    NSMutableURLRequest *newRequest = [self.request mutableCopy];
    [NSURLProtocol setProperty:@YES forKey:kAccessCodeProtocolKey inRequest:newRequest];
    self.connection = [NSURLConnection connectionWithRequest:self.request delegate:self];
}

但我的 startLoading 从未被调用过

【问题讨论】:

  • 对不起...我编辑帖子太晚了。

标签: ios nsurlprotocol


【解决方案1】:

您是否实现了 canInitWithRequest: ?它返回YES吗?如果没有, startLoading 将永远不会被调用。

【讨论】:

    【解决方案2】:

    我感觉你还没有深入搜索,无论如何要添加标题使用以下sn-p

    NSURL *URL = [NSURL URLWithString:@"http://example.com/..."];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL
                                                           cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                       timeoutInterval:30.0];
    [request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    

    【讨论】:

      【解决方案3】:

      你通常会用一个 NSURL 实例来初始化它,NSMutableURLRequest 会为你提供

      - (void)addValue:(NSString *)value forHTTPHeaderField:(NSString *)field
      

      方法,这样你就可以用这种方式设置你的标题

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-01-10
        • 2016-08-01
        • 2018-08-22
        • 1970-01-01
        • 2023-03-05
        • 1970-01-01
        • 2014-10-13
        • 1970-01-01
        相关资源
        最近更新 更多