I have a UIWebView which is adapted to a mobile style of a forum I have. In Safari, it looks and works great.
However, inside my UIWebView there is a major problem.
When quoting or editing a post, HTML is displayed inside of BBcode. Furthermore, posting code results in ignored line breaks. This problem will render my app completely unusable.
The webview is loaded like this:
//display the webview
NSString *fullURL = #"http://www.mysite.com";
NSURL *url = [NSURL URLWithString:fullURL];
NSMutableURLRequest *requestObj = [NSMutableURLRequest requestWithURL:url];
[_webTest loadRequest:requestObj];
This is how it looks inside of mobile Safari (correctly)
And finally, how it looks inside of UIWebView (incorrectly)
What could possible be changing the way it renders between mobile safari vs UIWebView? I thought the point was their supposed to look the same!
This is because mobile safari and a webview have different user agent values.
Check out this SO post which is about a similar issue: Change User Agent in UIWebView (iPhone SDK)
Code from accepted solution:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)req navigationType:(UIWebViewNavigationType)navigationType {
NSMutableURLRequest *request = (NSMutableURLRequest *)req;
if ([request respondsToSelector:#selector(setValue:forHTTPHeaderField:)]) {
[request setValue:[NSString stringWithFormat:#"%# Safari/528.16", [request valueForHTTPHeaderField:#"User-Agent"]] forHTTPHeaderField:#"User_Agent"];
}
return YES;
}
Related
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.
In my iPad application I have a UIWebView with a youtube video embedded in it. I have matched the UIWebView size and the size of the embedded video as well as made sure that the aspect ratio was correct. I have set the UIWebView background color to clear color with no luck. How can I center the embedded video inside my UIWebView. I also used content mode: center and it doesn't seem to respect it. See the screenshot below:
http://cl.ly/1N3c0d343r3v1i0J1h0d
NSString *video_ID = #"7Ek1QwGp9Tw?rel=0";
NSString *htmlStr = [NSString stringWithFormat:#"<iframe class=\"youtube-player\" type=\"text/html\" width=\"453\" height=\"255\" src=\"http://www.youtube.com/embed/%#\" frameborder=\"0\"></iframe>",video_ID];
[webView loadHTMLString:htmlStr baseURL:nil];
Any help would be appreciated.
just a wild guess: you need to reset the margins and paddings of your html documents body and the youtube iframe.
edit add something like <html><head><title>.</title><style>body,html,iframe{margin:0;padding:0;}</style></head><body>[YOUR YOUTUBE CODE]</body></html>
Don't use an iframe, just load the video directly in the webview (a webview is basically like a "native" iframe anyway):
NSString *URLString = [NSString stringWithFormat:#"http://www.youtube.com/embed/%#",video_ID];
NSURL *URL = [NSURL URLWithString:URLString];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
[webView loadRequest:request];
I am trying to display url in UIWebView but I can not display, webview just call the fail with error method. My url is this "http://www.tekniknoktamarket.com/index.php?route=feed/google_base",
So please help me how to display this url in UIWebView *?*
In order to have a UIWebView download and display the content from a URL, you need to go through a couple of layers of abstraction, the first being an instance of NSURL, which contains your URL string. Then, you hand the NSURL to an instance of NSURLRequest. Finally, you hand the NSURLRequest to the webView. It all looks something like this:
NSURL *url = [NSURL URLWithString:#"www.your-url.com"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[yourWebView loadRequest:request];
That should do it.
This is the code loading a URL into a webview:
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.theurl.com"]]];
If you're using this code and are encountering any problems, please provide the code you were using and the exact error
is it possible to show a PDF in a UIWebView with the option Open in Books and Open With at the top of the WebView? I don't know how to do that.
Thanks
Put a UIDocumentInteractionController on top of the web view, it will allow the user to open the pdf in iBooks.
NSString * path = [[NSBundle mainBundle] pathforResource:#"ResourceName" ofType:#"pdf"] ;
or path can be any http url too.
NSURL * url = [NSURL fileURLWithPath:path];
NSURLRequest * request = [NSURLRequest requestWithURL:url];
UIWebView * webView = ....;
[webView loadRequest:request];
This should show the pdf in webview. But seems you are asking to open the pdf in some other app, which part I didn't understand completely. Can you explain?
I am trying to display a web page in UIWebView so that when i click a button the webpage should be displayed in uiWebview.How could this be possible Can anybody help in solving this or give me some hint
You should have a look at the documentation for UIWebView.
There are two ways of doing this. The method you choose depends on where the data is coming from. If you're loading the data from a string, use:
NSURL* base = [NSURL URLWithString: #"http://..."];
[myWebView loadHTMLString: #"some html here" baseURL: base];
When loading HTML from a string the UIWebView needs to know the base URL for any relative URLs within the document, hence the baseURL: parameter.
If the data is accessible via a URL, use:
NSURL* url = [NSURL URLWithString: #"http://..."];
NSURLRequest* request = [NSURLRequest requestWithURL: url];
[myWebView loadRequest: request];