可可:获取Mime类型的网址?

Ale*_*987 3 objective-c nsurlconnection ios mime-types

我想同步得到一个网址的Mime类型.我不想使用NSURLConnection.就像这样:

NSString*theMimeType = [self getMimeTypeFromURL:theURL];

有任何想法吗?

Jac*_*kin 9

绝对没有理由使用异步请求.

使用NSURLConnection委托方法.

NSString        *url  = ...;
NSURLRequest    *req  = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
NSURLConnection *conn = [NSURLConnection connectionWithRequest:req delegate:self];
[conn start];
Run Code Online (Sandbox Code Playgroud)

在你的其他地方@implementation:

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {

   NSString *mime = [response MIMEType];
   //do something with mime
}
Run Code Online (Sandbox Code Playgroud)

  • 同步请求"更容易",因为它们将请求后处理逻辑隔离到首先发送请求的相同代码区域.可悲的是,我觉得这是人们仍然使用同步请求的一个原因.: - \ (4认同)