How to get UIWebView User-Agent - iphone

I've got a problem working with one remote server. My app makes a request to a server using [NSData initWithContentsOfURL:] method and as a response I get website's url which I open in UIWebView.
The problem is that those requests have different User-Agent and server can't serve me correct because it expects that I send all requests with the same User-Agent. I know how to change User-Agent (e.g Change User Agent in UIWebView (iPhone SDK)) but what I really want it is somehow to get UIWebView's User-Agent and set it to [NSData initWithContentsOfURL:] to avoid problems with server side

I just ran into a similar issue and needed to make the user agent sent by an NSURLConnection match the one sent by a UIWebView. My solution was to create a UIWebView and then just use javascript to pull out the user agent.
UIWebView* webView = [[UIWebView alloc] initWithFrame:CGRectZero];
NSString* secretAgent = [webView stringByEvaluatingJavaScriptFromString:#"navigator.userAgent"];

As #nate mentioned above, you can invoke Javascript in a webview:
UIWebView* webView = [[UIWebView alloc] initWithFrame:CGRectZero];
NSString* secretAgent = [webView stringByEvaluatingJavaScriptFromString:#"navigator.userAgent"];
But invoking new Javascript is a bit hacky and creating a zero-size webView is gratuitous, since you already have a webView you're dealing with.
Alternatively you can simply use native methods on your given webView:
NSString* ua = [webView.request valueForHTTPHeaderField:#"User-Agent"];
NSLog(#"User-Agent = %#", ua);

Related

UIWebView loading and general performance

I am using UIWebView to display textual content in my app (I store the content in local HTML files that I pack with the app). All together, I have three web views whose content I change dynamically based on user feedback.
Although some might argue that this is not the most accepted way, I find UIWebView very convenient to display formatted text, and modify that text using HTML if necessary. While this works 99% of the time, on occasion, I experience problems that generally fall into one of these categories:
Sometimes, the web view content loads slow and is late a second or so.
The content loads but is not showing. However, as long as, I touch the view (try to scroll or something) the content pops in.
A few times I received memory warnings (usually not long after the app's initial loading) that in no way affected the performance of my app. It logged the memory warning but the app worked like nothing happened.
This is the method I use to load content to my web views:
- (void)loadSimpleDocument:(NSString*)documentName inView:(UIWebView*)webView
{
NSString *path = [[NSBundle mainBundle] pathForResource:documentName ofType:#"html"];
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
}
Aside from this, the shouldStartLoadWithRequest delegate method is also implemented, always returning a YES.
My question is: Is there a way to improve the reliability/performance of my web views (in particular loading)? Is there something I have overlooked, did wrong, or do not know about?
Some additional info:
I am on iOS6 SDK, use ARC, and do everything programmatically (do not use IB or storyboard).
You have 2 options to check what's going on:
Implement webViewDidStartLoad & webViewDidFinishLoad delegate methods to check why the content isn't showing (may be the content isn't loaded yet).
read the html content to an NSString then use loadHTMLString:baseURL instead of using loadRequest and check if it loads faster.
Hope this could help.

URL load in Safari but not loading in UIWebview

I am facing an strange issue. I have a URL when I try to load that url in UIWebView it doesn't load web view remain blank but when I try to load the same url in Safari it works great and display the page. Is there any restriction for UIWebView like it can't load any specific type of widgets if webpage contain.
Thank in advance
UIWebView *webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
webView.delegate = self;
NSURL *url = [NSURL URLWithString:#"myurl"];
NSURLRequest *req = [[NSURLRequest alloc]initWithURL:url];
[self.view addSubview:webView];
[webView loadRequest:req];
Behaviour on Safari and the iOS simulator on your local machine will be the same. Both use the same proxy settings configured in System Preferences.
However - and here's the trick - each has their own distinct cookie store. To get the same behaviour you should first reset Safari and the Simulator.
This caught me out. My Safari URL worked because I had previously stored cookie values that enabled the server to fulfil my request. On the Simulator, no such cookies existed and therefore the server couldn't respond. On the surface it appeared as if the UIWebView just didn't work. However, that wasn't the case
Bottom line: reset both and try again.

how to scale to fit pages opened from HTML file containing web links

I have an app I am working on in Xcode. Part of the app area loads an html file. The HTML file itself has some links to external websites. They open fine, except they are not scaled to fit on the iPhone.
How would I go about ensuring all web pages opened are scaled to fit? The code from the .m file is below
NSString *fileString = [[NSBundle mainBundle] pathForResource: #"chapter5" ofType: #"html"];
NSURL *newURL = [[NSURL alloc] initFileURLWithPath: fileString];
NSURLRequest *newURLRequest = [[NSURLRequest alloc] initWithURL: newURL];
[webview loadRequest: newURLRequest];
The HTML file "chapter5.html contains links to websites.
I thought I would try entering
webview.scalesPageToFit = YES
in the .m file hoping it would apply to anything opened, but that didn't work.
Any advice would be greatly appreciated.
Thanks
The scalesPageToFit will take effect only when the webView loads the request. Implement the delegate of UIWebView webViewDidFinishLoad: method and try this code.
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
CGSize defaultSize = webView.frame.size;
CGSize fittingSize = [tableViewCell.webView sizeThatFits:defaultSize];
[webView sizeThatFits:fittingSize];
NSLog(#"Size : %#",NSStringFromCGSize(fittingSize));
}
Please set webview mode as Aspect-Fit
Thanks guys, I set webview mode as Aspect-Fit and set the Scaling to scale pages to fit and this worked, not sure how I missed that.

Open the Phone (Call) with a button and a Text field/View (iPhone) and come back to the application back

Hello, I have successfully developed an application in which clicking on UIButton the Mobile no in UITextField dialled.
But I am not able to come back in my application. Is it possible to come back on the page from which I have dialled the Mobile number.
-(void)newuser:(id)sender;
{
NSString *URLString = [#"tel://" stringByAppendingString:#")172-123456"];
NSURL *URL = [NSURL URLWithString:URLString];
[[UIApplication sharedApplication] openURL:URL];
}
Thanks
No, an application receiving a URL to process doesn't get information about the sender. Besides, as far as I know the Phone application doesn't have a Back button either.
not sure which iOS version this was implemented in but I use this simple approach in my latest app 'App Time' which is to launch the call from within a UIWebview, but doesn't require anything more than...
UIWebView *webView = [[UIWebView alloc] init];
NSURL *telURL = [NSURL URLWithString:#tel://yourNumber"];
[webView loadRequest:[NSURLRequest requestWithURL:telURL]];
the user is prompted to confirm the call with a UIAlertview but will be brought back to the same screen after the call completes.

Why would webview's loadHTMLString not hit webViewDidStartLoad callback?

Background: I am developing a news reader type app native for the iPhone which displays many of the news articles as html in UIWebViews. My current goal is to have a css file in my project that can style html that is programmitically meshed up (article + comments, etc). I have done a bunch of little proof of concepts and I know that I can use <link id=\"stylesheet\" rel=\"stylesheet\" href=\"Test.css\" type=\"text/css\" /> as part of the input string to webview's loadHTMLString and achieve the result of styling the html in an expected manner, using the mainbundle's bundlepath as the baseUrl input to that function. Unfortunately, this does not work when I do this in my actual project. Note here that my issue can be reproduced with simple html.
Here's the part where I scratch my head: I can use #"" as the base url and display the unstyled html as expected. But, if I use the bundlepath as the baseUrl instead, the UIWebView goes blank. Investigating further, the webViewDidStartLoad callback never hits it's breakpoint (it does in the #"" case). What would cause this not to fire? Of the 4 callbacks, the only one that fires is the shouldStartLoadWithRequest, which I return NO from. The didFailLoadWithError and the webviewDidFinishLoad are the other two.
My suspicion is that something must be wrong with the mainbundle, but I have no idea what.
I do receive this message in gdb sometimes, although this could be unrelated:
void SendDelegateMessage(NSInvocation*): delegate failed to return after waiting 10 seconds. main run loop mode: kCFRunLoopDefaultMode
If you were not using the touch screen for this entire interval (which can prolong this wait), please file a bug.
Code Snippets:
Note: the css link actually has no affect on this issue
UIWebView* contentView = [[UIWebView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
contentView.delegate = self;
NSString *htmlString = #"<html><body>Hello World</body></html>";
NSString *path = [bundle bundlePath];
NSURL *resourceBaseURL = [NSURL fileURLWithPath:path];
[contentView loadHTMLString:htmlString baseURL:resourceBaseURL];
Values at time of loadHTMLString call:
htmlString: "<html><body>Hello World</body></html>"
resourceBaseUrl: file://localhost/Users/U0107552/Library/Application%20Support/iPhone%20Simulator/3.1.3/Applications/D0EE8DA9-B156-47B3-BC53-9A731F813FB1/Test%20App.app/
The one difference that I can see between my working POC and my app is that gdb: po htmlString returns "[the above string] Current Language: auto; currently objective-c" in the POC but just the string in my app.
Sorry for the long-windedness.