廢話不多說座掘,直接上問題:
1.如何拼接請求頭远豺?
//申明返回的結(jié)果是json類型
manager.responseSerializer = [AFJSONResponseSerializer serializer];
//申明請求的數(shù)據(jù)是json類型
manager.requestSerializer=[AFJSONRequestSerializer serializer];
// /如果報接受類型不一致請?zhí)鎿Q一致text/html或別的
[manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[manager.requestSerializer setValue:value forHTTPHeaderField:key];
[manager.requestSerializer setValue: value forHTTPHeaderField:key];
[manager.requestSerializer setValue: value forHTTPHeaderField:key];
2.如何返回參數(shù)在調(diào)用的同一方法中熊户?
block,參數(shù)帶有block去實現(xiàn)
__block NSString *longitude; // __block,靜態(tài)變量
[self.manager POST:URL parameters:params success:^(NSURLSessionDataTask * _Nonnull task, id _Nonnull responseObject) {
NSDictionary *locDictionary = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
longitude = [locDictionary objectForKey:@"lon"]; // 糾正后經(jīng)度
if (finishBlock)
{
finishBlock(longitude, nil);
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"%@", error);
if (finishBlock)
{
finishBlock(nil, error);
}
}];