I have a simple ViewController to load FB comments plugins inside a UIWebView
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
UIWebView * webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320.0f, 1505.0f)];
NSString * html = #"\
<!DOCTYPE html>\
<html xmlns:fb='http://ogp.me/ns/fb#'>\
<head>\
<meta name='viewport' content='width=device-width, initial-scale=1.0'>\
</head>\
<body style='background-color:red;'>\
\
<div id='fb-root'></div>\
<script>(function(d, s, id) {\
var js, fjs = d.getElementsByTagName(s)[0];\
if (d.getElementById(id)) return;\
js = d.createElement(s); js.id = id;\
js.src = 'http://connect.facebook.net/en_US/all.js#xfbml=1&appId=xxx';\
fjs.parentNode.insertBefore(js, fjs);\
}(document, 'script', 'facebook-jssdk'));</script>\
\
<fb:comments href='http://example.com' num_posts='10'></fb:comments>\
\
</body>\
</html>\
";
[webView loadHTMLString:html baseURL:nil];
[self.view addSubview:webView];
I can see the comments being loaded, but just the height is strange, seems auto resize failed? I can view the whole comments if I am using mobile safari.
you can use this code for displaying the fb comment in iOS UIWebview
UIWebView *fbWebview=[[UIWebView alloc]initWithFrame:CGRectMake(0, 0, 320, 1505)];
[fbWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"https://www.facebook.com/Levis"]]];
CGRect scrollViewFrame = CGRectMake(0, 0, 320, 1024);
UIScrollView *scrollViewMain = [[UIScrollView alloc] initWithFrame:scrollViewFrame];
CGSize scrollViewContentSize = CGSizeMake(320, 1505);
[scrollViewMain setContentSize:scrollViewContentSize];
[scrollViewMain setBounces:NO];
[scrollViewMain setScrollEnabled:YES];
[scrollViewMain setShowsVerticalScrollIndicator:YES];
[scrollViewMain setBackgroundColor:[UIColor redColor]];
[scrollViewMain addSubview:fbWebview];
[self.view addSubview:scrollViewMain];
instead of "https://www.facebook.com/Levis" use your FB URL
this may help you
You need to specify a baseURL
[webView loadHTMLString:html baseURL:[NSURL URLWithString:#"http://www.example.com"]];
#Howard you can use http://www.facebook.com/plugins/comments.php?href=http://www.google.com' scrolling='yes' profile='yes' frameborder='0' style='border:none; overflow:hidden; width:300px; height:30px;' data-href='http://www.google.com' allowTransparency='true'>"
Same problem here. The solution was not to use a local HTML file (or HTML string). Upload the HTML file to an server and use:
NSURL *url = [NSURL URLWithString:#"http://www.blablblab.com/yourfile.html"];
[self.webview loadRequest:[NSURLRequest requestWithURL:url]];
instead of:
[self.webview loadHTMLString:localString baseURL:nil];
I didn't discovered yet why there is a difference in the layout when the file is on a server, but when it is a local file, the height of my Facebook Comments View was always reduced to "height:160". Apparently it's because of the "all.js" facebook script (accordingly to this question).
Related
We have used the below lines of code to move to the web view:
[webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:#"window.location.hash=\'#-1\';"]];
[webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:#"window.location.hash=\'#%#\';", index]];
but somehow this code doesn't work only in iPad. Here is how I initialised the web view:
webView = [[UIWebView alloc] initWithFrame:self.view.bounds];
[webView setDelegate:self];
[webView.scrollView setDelegate:self];
[webView setBackgroundColor:WHITECOLOR];
[webView setScalesPageToFit:YES];
[webView setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.view addSubview: webView];
Could someone please guide me how to debug it.
In iOS11 location.hash seems to be broken, but it's a simple solution to this.
Instead using an usual combination:
location.hash = "";
location.hash = _some_hash_string_
use javascript below:
var hashElement=document.getElementsByName(_some_hash_string_)[0];
if(hashElement) {
hashElement.scrollIntoView();
} else {
// if the element is not found - scroll to top
document.documentElement.scrollIntoView();
}
UIWebView displays pdf on UIWebView splendidly.
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
CGFloat screenHeight = screenRect.size.height;
webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, screenWidth, screenHeight)];
webView.delegate = self;
NSURL *targetURL = [[NSBundle mainBundle] URLForResource:#"document" withExtension:#"pdf"];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[webView loadRequest:request];
[self.view addSubview:webView];
However, things starts to get a little irritating when it can't display a pdf with e-signature (I got it from http://www.tecxoft.com/samples/sample01.pdf):
However, in print preview it's able to show the e-signature at the top right corner (this only works on iOS 10 and not iOS9):
Question:
Any alternative to display pdf with signature?
I've tried both UIWebView and UIDocumentationInteractionController and it still doesn't display the signature.
Just need to flatten the pdf before displaying it on the webview.
Has anyone managed to successfully add a header or footer view to a WKWebView ScrollView?
I'm currently trying to do this using the method described here for a UIWebView Adding a header view to a UIWebView similar to Safari and Articles.
When this method is used in a WKWebView the content view origin.y is correctly changed but content is cut off at the bottom.
Using the scroll view content offset is also not possible as it breaks fixed positioned CSS elements in the web view.
In webView Delegate method
- (void)webViewDidFinishLoad:(UIWebView *)webView
add the following codebase,
mainWebViewObj.scrollView.contentInset = UIEdgeInsetsMake(headerView.frame.size.height,0.0,headerView.frame.size.height,0.0);
mainWebViewObj.scrollView.backgroundColor = [UIColor whiteColor];
if(![headerView superview])
{
[webView.scrollView addSubview:headerView];
[webView.scrollView bringSubviewToFront:headerView];
}
[mainWebViewObj.scrollView setContentOffset:
CGPointMake(0, -mainWebViewObj.scrollView.contentInset.top) animated:NO];
this worked perfect for me. Hope it solves your problem.
Here's an example that I think does as you describe. It offsets the web content by setting contentInset on the scrollView, and by offsetting the header view frame by a negative amount:
#implementation ViewController
{
WKWebView* _webView;
UIView* _headerView;
}
- (void)viewDidLoad
{
[super viewDidLoad];
_webView = [[WKWebView alloc] initWithFrame: self.view.bounds];
[self.view addSubview: _webView];
[_webView loadRequest: [NSURLRequest requestWithURL: [NSURL URLWithString: #"http://www.stackoverflow.com"]]];
[_webView.scrollView setContentInset: UIEdgeInsetsMake(100, 0, 0, 0)];
_headerView = [[UIView alloc] initWithFrame: CGRectMake(0, -100, 375, 100)];
_headerView.backgroundColor = [UIColor redColor];
[_webView.scrollView addSubview: _headerView];
}
- (void) viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];
_webView.frame = self.view.bounds;
CGRect f = _headerView.frame;
f.size.width = _webView.bounds.size.width;
_headerView.frame = f;
}
i am trying to develop a app that will load a iAd like popup view that will show a webview with a image in it. what i mean is that it will load a JPG or PNG url in it. i successfully created that popup but webview in not showing image in it can anyone please debug my code. i will post it here.
MainViewController.m
-(IBAction)openPopupWithURL
{
[MTPopupWindow showWindowWithHTMLFile:#"http://URL/ad.php" insideView:self.view];
}
PopUpWindow.h
#import <Foundation/Foundation.h>
#interface MTPopupWindow : NSObject
{
UIView* bgView;
UIView* bigPanelView;
}
+(void)showWindowWithHTMLFile:(NSString*)fileName insideView:(UIView*)view;
#end
PopUpWindow.m
#import "MTPopupWindow.h"
#define kShadeViewTag 1000
#interface MTPopupWindow(Private)
- (id)initWithSuperview:(UIView*)sview andFile:(NSString*)fName;
#end
#implementation MTPopupWindow
/**
* This is the only public method, it opens a popup window and loads the given content
* #param NSString* fileName provide a file name to load a file from the app resources, or a URL to load a web page
* #param UIView* view provide a UIViewController's view here (or other view)
*/
+(void)showWindowWithHTMLFile:(NSString*)fileName insideView:(UIView*)view
{
[[MTPopupWindow alloc] initWithSuperview:view andFile:fileName];
}
/**
* Initializes the class instance, gets a view where the window will pop up in
* and a file name/ URL
*/
- (id)initWithSuperview:(UIView*)sview andFile:(NSString*)fName
{
self = [super init];
if (self) {
// Initialization code here.
bgView = [[[UIView alloc] initWithFrame: sview.bounds] autorelease];
[sview addSubview: bgView];
// proceed with animation after the bgView was added
[self performSelector:#selector(doTransitionWithContentFile:) withObject:fName afterDelay:0.1];
}
return self;
}
/**
* Afrer the window background is added to the UI the window can animate in
* and load the UIWebView
*/
-(void)doTransitionWithContentFile:(NSString*)fName
{
//faux view
UIView* fauxView = [[[UIView alloc] initWithFrame: CGRectMake(10, 10, 200, 200)] autorelease];
[bgView addSubview: fauxView];
//the new panel
bigPanelView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, bgView.frame.size.width, bgView.frame.size.height)] autorelease];
bigPanelView.center = CGPointMake( bgView.frame.size.width/2, bgView.frame.size.height/2);
//add the window background
UIImageView* background = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"popupWindowBack.png"]] autorelease];
background.center = CGPointMake(bigPanelView.frame.size.width/2, bigPanelView.frame.size.height/2);
[bigPanelView addSubview: background];
//add the web view
int webOffset = 10;
UIWebView* web = [[[UIWebView alloc] initWithFrame:CGRectInset(background.frame, webOffset, webOffset)] autorelease];
web.backgroundColor = [UIColor clearColor];
if ([fName hasPrefix:#"http"]) {
//load a web page
web.scalesPageToFit = YES;
[web loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString: fName]]];
} else {
//load a local file
NSError* error = nil;
NSString* fileContents = [NSString stringWithContentsOfFile:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:fName] encoding:NSUTF8StringEncoding error: &error];
if (error!=NULL) {
NSLog(#"error loading %#: %#", fName, [error localizedDescription]);
} else {
[web loadHTMLString: fileContents baseURL:[NSURL URLWithString:#"file://"]];
}
}
[bigPanelView addSubview: web];
//add the close button
int closeBtnOffset = 10;
UIImage* closeBtnImg = [UIImage imageNamed:#"popupCloseBtn.png"];
UIButton* closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[closeBtn setImage:closeBtnImg forState:UIControlStateNormal];
[closeBtn setFrame:CGRectMake( background.frame.origin.x + background.frame.size.width - closeBtnImg.size.width - closeBtnOffset,
background.frame.origin.y ,
closeBtnImg.size.width + closeBtnOffset,
closeBtnImg.size.height + closeBtnOffset)];
[closeBtn addTarget:self action:#selector(closePopupWindow) forControlEvents:UIControlEventTouchUpInside];
[bigPanelView addSubview: closeBtn];
//animation options
UIViewAnimationOptions options = UIViewAnimationOptionTransitionFlipFromRight |
UIViewAnimationOptionAllowUserInteraction |
UIViewAnimationOptionBeginFromCurrentState;
//run the animation
[UIView transitionFromView:fauxView toView:bigPanelView duration:0.5 options:options completion: ^(BOOL finished) {
//dim the contents behind the popup window
UIView* shadeView = [[[UIView alloc] initWithFrame:bigPanelView.frame] autorelease];
shadeView.backgroundColor = [UIColor blackColor];
shadeView.alpha = 0.3;
shadeView.tag = kShadeViewTag;
[bigPanelView addSubview: shadeView];
[bigPanelView sendSubviewToBack: shadeView];
}];
}
/**
* Removes the window background and calls the animation of the window
*/
-(void)closePopupWindow
{
//remove the shade
[[bigPanelView viewWithTag: kShadeViewTag] removeFromSuperview];
[self performSelector:#selector(closePopupWindowAnimate) withObject:nil afterDelay:0.1];
}
/**
* Animates the window and when done removes all views from the view hierarchy
* since they are all only retained by their superview this also deallocates them
* finally deallocate the class instance
*/
-(void)closePopupWindowAnimate
{
//faux view
__block UIView* fauxView = [[UIView alloc] initWithFrame: CGRectMake(10, 10, 200, 200)];
[bgView addSubview: fauxView];
//run the animation
UIViewAnimationOptions options = UIViewAnimationOptionTransitionFlipFromLeft |
UIViewAnimationOptionAllowUserInteraction |
UIViewAnimationOptionBeginFromCurrentState;
//hold to the bigPanelView, because it'll be removed during the animation
[bigPanelView retain];
[UIView transitionFromView:bigPanelView toView:fauxView duration:0.5 options:options completion:^(BOOL finished) {
//when popup is closed, remove all the views
for (UIView* child in bigPanelView.subviews) {
[child removeFromSuperview];
}
for (UIView* child in bgView.subviews) {
[child removeFromSuperview];
}
[bigPanelView release];
[bgView removeFromSuperview];
[self release];
}];
}
#end
and this is output
My ad.php
<?php
include("../includes/opencon.php");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Mobile Ad</title>
</head>
<body>
<?php
$setting = mysql_fetch_object(mysql_query("select * from my_settings where id=1"));
?>
<img src="http://url.com/media/<?=$setting->image?>" style="width:100%; height:auto;"/>
</body>
</html>
Please provide additional information, whether this happens when loading a local or remote image. I think for local images you could adjust your base url of the webview.
If you want to load local images, please provide the base URL of the App bundle:
NSString *path = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:path];
[webView loadHTMLString:htmlString baseURL:baseURL];
So instead of:
[web loadHTMLString: fileContents baseURL:[NSURL URLWithString:#"file://"]];
use the app bundle url:
NSString *path = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:path];
[web loadHTMLString: fileContents baseURL:baseURL];
For further debugging, you could set the webview's delegate to your view and check, what the webview's content looks like:
web.delegate = self;
To now use the delegate implement the following:
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
NSString *html = [webView stringByEvaluatingJavaScriptFromString:#"document.documentElement.outerHTML"];
NSLog(#"%#",html);
}
Alternatively, go to Safari->Develop->iPhone Simulator/iOS Device->Webview
And take a look at the source code in the debug console of safari.
I've come across a rather strange problem. What I'm trying to do right now is only obtain the audio of a YouTube video for a given link. I've tried a couple of things but it either didn't work or wasn't efficient.
Thus I've decided to ask more experienced iOS developers in here. Thanks in advance.
#implementation ViewController
#synthesize web;
- (void)embedYouTube:(NSString*)url frame:(CGRect)frame{
NSString* embedHTML = #"<html><head> <style type=\"text/css\">body {background-color: transparent;color: white;}</style></head><body style=\"margin:0\"><embed id=\"yt\" src=\"%#\" type=\"application/x-shockwave-flash\" width=\"%0.0f\" height=\"%0.0f\"></embed></body></html>";
NSString* html = [NSString stringWithFormat:embedHTML, url, frame.size.width, frame.size.height];
UIWebView *webView =[[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
[webView loadHTMLString:html baseURL:nil];
[self.view addSubview:webView];
}
-(IBAction)play{
[self embedYouTube:#"http://www.youtube.com/watch?v=iw1aT5c1Lus" frame:CGRectMake(0, 0, 320, 480)];
}
just play the video with your media player, but dont give it a frame, this will play the audio, but not load the image