URL load in Safari but not loading in UIWebview - iphone

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.

Related

iPad: Cannot load facebook page wall in UIWebview

Its already too long i am struggling with this issue. After searching a lot I decided to post a question here.
What my app does
Captures photo
Uploads the photo on the wall of the page
Displays the facebook page wall in a UIWebview after upload is complete
Everything was working as expected 4 days back :) Suddenly something went wrong :(
Code
NSString *facebookPageURL =#"https://m.facebook.com/pages/<myPageName>/<myPageID>?v=wall"
UIWebView *webView = [[UIWebView alloc] initWithFrame:kAppFrame];
[webView setUserInteractionEnabled:NO];
[webView setBackgroundColor:[UIColor clearColor]];
webView.delegate = self;
[webView setHidden: YES];
NSURL *url = [NSURL URLWithString:[facebookPageURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] relativeToURL:[NSURL URLWithString:#"http://login.facebook.com"]];
NSMutableURLRequest *request = nil;
if(url)
request = [NSMutableURLRequest requestWithURL:url];
[webView loadRequest:request];
[self.view addSubview:webView];
[webView reload];
[self.view bringSubviewToFront:webView];
webView = nil;
Scenario
If I open the url facebookPageURL in Safari in iOS Simulator it works well
If I open the url in any browser on Mac it works well
In webView I see a white screen
If I change the facebookPageURL to remove ?v=wall to ?v=info I am stil able to see the page.(not blank screen atleast).
Note
1. My facebook Page is NOT unpublished and is visible.
2. I have cross checked the facebook page permissions.
I suspect there is something changed on facebook side overnight.
Please guide.
It seems the ever capricious Facebookhas removed the permission to view the wall. I came to this conclusion as when I tried the same thing in separate project I was getting both info and photos but for wall it turned grey
So Here the solution for al those suffering or might suffer form this issue.
I am getting a dictionary in response to the post
{"id":"<imge_id>","post_id":"<post_id>"}
http://www.facebook.com/photo.php?fbid=<imge_id>
and it works like a charm.
Edit:1
Just now I figured out an issue
For the pages with an age restriction, we need to authenticate which SSO doesn't supports for UIWebView inside my app.
I had to downgrade to NOT using SSO anymore. And now it works no matter what.
Just to add here you can find how to neatly downgrading to non-sso mechanism.
I think it's will help you
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
if ([(NSMutableURLRequest *)request respondsToSelector:#selector(setValue:forHTTPHeaderField:)]) {
[(NSMutableURLRequest *)request setValue:#" Safari/537.1" forHTTPHeaderField:#"User_Agent"];
}
}
I hit my head against the wall on this one for a while. The same request and webview on an iPhone will pull up a facebook page, but results in a blank white page on an iPad. Changing user agent doesn't solve the problem.
I was able to hack around it by pulling down the HTML for the page I was looking for using stringWithContentsOfURL and then loading the HTML manually into the webview
You don't want to call [NSString stringWithContentsofURL from your main thread if you can help it, since it will hold up your thread until the results come back. Here is my solution for running it in the background and updating a UIWebView that I had already created and added to my subview called "facebookWebView":
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSString *rawHTML = [NSString stringWithContentsOfURL:[NSURL URLWithString:#"http://www.facebook.com/link.to.your.page.on.facebook"] encoding:NSASCIIStringEncoding error:nil];
dispatch_async(dispatch_get_main_queue(), ^(void) {
[facebookWebView loadHTMLString:rawHTML baseURL:[NSURL URLWithString:#"http://www.facebook.com"]];
});
});
Hope this helps some people.

uiwebview youtube starting in landscape thumbnail does resize on rotation

I have navigation controller with a tableview. When you click on one of the cells it pushes on a view with a uiwebview on it. You are taken to a YouTube page.
When you are on the table view in portrait and click on a cell you see the youtube page in portrait. Changing your orientation the video thumbnail does not refresh. So the thumbnail is smaller. This is fine. I actually prefer it smaller. All the content that would consume 2 lines will then consume 1 line. So in other words everything else adjusts for the new dimensions.
The problem comes in when you start off in landscape. Since the thumbnail doesn't resize on orientation change, changing to portrait mode, the image now goes off the screen, while the rest of the content adjusts correctly.
[webVIew refresh];
does work but it obviously loads the entire page again. So depending on the connection there will be a flicker or possibly the site will go white until its finished loading (on slower connections).
I also tried load the website in an iframe. I asked a similar question yesterday, this was for local pages i was creating. The answer to that question was to put <meta name='viewport' content='width=device-width; initial-scale=1.0; maximum-scale=1.0;'> in the head. So I tried that with an iframe going and getting the page. That seemed like a dumb hack to begin with, but i was willing to go there. It wouldn't even load the page at all. I guess because the youtube page i'm loading redirects to yet other page. Upon further research it seemed like there were other issues with the iframe such as scrolling.
So my question is how can i:
A. Get just the thumbnail to resize/reload on orientation change
OR
B. Get the thumbnail to load in the dimensions it would load in portrait mode all the time, even if it was started in landscape.
You should override this method in your UIViewController class and do the resizing there:
(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration;
I found the answer, maybe someone could explain it or offer a better solution. There was also an addition bug that I had to work out.
I made sure the uiwebview had the delegate set to files owner.
I changed the parent view when it pushed the view onto the form from:
[self.navigationController pushViewController:controller animated:YES];
to (the important part being the 320, i'm restricting the view to portrait):
[self.navigationController pushViewController:controller animated:YES];
controller.webView.hidden = YES;
controller.webView.frame=CGRectMake(0, 0, 320,367);
3.On the webview i load the url as i always did:
NSURL *url = [NSURL URLWithString:sUrl];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
4.In the webviewdidfinishload i now have:
if (self.interfaceOrientation ==
UIInterfaceOrientationLandscapeLeft ||
self.interfaceOrientation ==
UIInterfaceOrientationLandscapeRight) {
self.webView.frame=CGRectMake(0, 0, 480,227);
}
self.webView.hidden = NO;
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
All this would normally have solved my issue, but then i found out that the gdata url i'm getting from youtube service actually gets resolved to a 2nd url. Then i guess youtube changed their url format so it is forwarded to a 3rd url. This means that my didfinishload code was being called before the final url had loaded. To solve this i added:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
if ([[[NSString stringWithFormat:#"%#",request.URL] substringWithRange:NSMakeRange(0, 26)] isEqualToString:#"http://m.youtube.com/watch"]) {
NSString *sUrl=[NSString stringWithFormat:#"%#",request.URL];
sUrl = [sUrl stringByReplacingOccurrencesOfString:#"http://m.youtube.com/watch?" withString:#"http://m.youtube.com/#/watch?"];
NSURL *url = [NSURL URLWithString:sUrl];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:requestObj];
return NO;
}
//NSLog(#"request:%#",request.URL);
return YES;
}
Which im sure could be better but basically i tell it to ignore the 2nd url, and make my own change to go to the 3rd url.

playing stream video using UIWebView issue

I'm currently developing an ipad application that will play video from wowza streamiong server.
Plainly saying, I load video into UIWebView as follows:
NSURL *videoURL = [NSURL URLWithString:#"http://fishki.tv:1935/vod/mp4:20100731194117.mp4/playlist.m3u8"];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:videoURL];
UIWebView * WView = [[UIWebView alloc] init];
WView.frame = CGRectMake(0,0,300,200);
[self.view addSubview:WView];
[WView loadRequest:requestObj];
It plays video ok (you can try yourself), BUT when i try to load another video (or even the same one) by changing videoURL and calling loadRequest method again, it plays only sound without video.
I'm starting to think that it is a bug in ipad simulator because I was unable to fix it anyhow.
I would be glad to hear any suggestions because I also was unable to find similar problem on the web. Thanks in advance.
The web view behaves erratically on Simulator. Can you try the same on a regular device? I believe it might actually be working.

Facebook in iPad: UIWebView - renders as touch instead of standard web

Up until a few days ago it worked fine: opening facebook.com in iPad/UIWebView rendered as standard web. Now Facebook is force-rendered as touch - as if the URL was http://touch.facebook.com. This happens regardless of the UIWebView frame size. Here is a simple code for the main view controller to see the problem:
UIWebView *wv = [[UIWebView alloc] initWithFrame:self.view.frame];
NSURLRequest *req = [NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.facebook.com"]];
[wv loadRequest: req];
[self.view addSubview: wv];
I tried changing the user agent as suggested here - no good.
Using http://www.facebook.com?m2w should resolve this. "m2w" sounds like it is short for "mobile 2 web", and it's the link that you arrive at when you click "full site" from the mobile site.

How to get UIWebView User-Agent

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);