The first of these methods
UIWebView *webview = [[UIWebView alloc] Initwithframe:cgrectzero];
NSString *useragent = [WebView stringbyevaluatingjavascriptfromstring:@ "navigator.useragent"];
NSLog (@ "-----------------%@", useragent);
The second method of
@interface Viewcontroller () <UIWebViewDelegate>
{
UIWebView *_webview;
NSString *_useragent;
}
{
_webview = [[UIWebView alloc] init];
_webview.delegate = self;
[_webview loadrequest:[nsurlrequest requestwithurl:[nsurl urlwithstring:@ "http://www.baidu.com"]];
NSLog (@ "-----------------%@", [self useragentstring]);
}
-(NSString *) useragentstring {
While (_useragent = = nil)
{
NSLog (@ "%@", @ "in while");
[[Nsrunloop Currentrunloop] Runmode:nsdefaultrunloopmode beforedate:[nsdate distantfuture]];
}
return _useragent;
}
-(BOOL) WebView: (UIWebView *) WebView shouldstartloadwithrequest: (nsurlrequest *) Request Navigationtype: ( Uiwebviewnavigationtype) Navigationtype {
if (WebView = = _webview) {
_useragent = [Request valueforhttpheaderfield:@ "User-agent"];
//Return No, we don ' t care on executing an actual request.
return NO;
}
return YES;
}
IOS Get user-agent