How to get the user agent on iOS?

后端 未结 6 2006
天涯浪人
天涯浪人 2020-12-14 19:59

Is there a way on iOS to get the user agent of the device? I don\'t want to hard code it since I need the user agent for all devices and I need to append the user agent to a

6条回答
  •  时光取名叫无心
    2020-12-14 20:29

    You don’t actually need to make the request in order to get the user-agent. Just return NO from the following delegate method and retain the user-Agent header:

    -(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
    

    It might look something like this:

    -(BOOL)webView:(UIWebView *)webView
     shouldStartLoadWithRequest:(NSURLRequest *)request
     navigationType:(UIWebViewNavigationType)navigationType
    {  
         userAgent = [[request valueForHTTPHeaderField:@"User-Agent"] copy]; 
         NSLog(@"user-agent: %@", userAgent);
    
         _webView.delegate = nil; 
         [_webView release]; 
    
         return NO; 
    }
    

提交回复
热议问题