Calling UIWebView in UIScrollView - objective-c

I'm trying to call the two UIWebViews which is declared in other views(AppleViewController & GoogleViewController) by calling UIScrollView in main view controller. ScrollView is displaying but the problem is the UIWebView is not loading to scroll view. What is the problem in my code?
ViewController.h file
- (void)viewDidLoad
{
[super viewDidLoad];
self.scrollView = [[[UIScrollView alloc] init] autorelease];
self.scrollView.delegate = self;
[self.view addSubview:self.scrollView];
CGSize scrollViewContentSize = CGSizeMake(640, 404);
[self.scrollView setContentSize:scrollViewContentSize];
[self.scrollView setPagingEnabled:YES];
self.scrollView.showsHorizontalScrollIndicator = YES;
pageControl = [[[UIPageControl alloc] init] autorelease];
pageControl.frame = CGRectMake(0,0,self.view.frame.size.width,self.view.frame.size.height);
pageControl.numberOfPages = 3;
pageControl.currentPage = 0;
[self.view addSubview:pageControl];
pageControl.backgroundColor = [UIColor blueColor];
AppleViewController *apple=[[AppleViewController alloc]init];
GoogleViewController *google=[[GoogleViewController alloc]init];
[self.scrollView addSubview:apple.view];
[self.scrollView addSubview:google.view];
[scrollView setPagingEnabled:YES];
}
AppleViewController.h
- (void)viewDidLoad
{
[super viewDidLoad];
webview2=[[UIWebView alloc]initWithFrame:CGRectMake(10, 500,750,350)];
[webview2 setBackgroundColor:[UIColor redColor]];
NSString *url2=#"http://www.apple.com";
NSURL *nsurl2=[NSURL URLWithString:url2];
NSURLRequest *nsrequest2=[NSURLRequest requestWithURL:nsurl2];
[webview2 loadRequest:nsrequest2];
[self.view addSubview:webview2];
[webview2 setScalesPageToFit:NO];
webview2.multipleTouchEnabled=YES;
[[webview2 layer] setCornerRadius:10];
[webview2 setClipsToBounds:YES];
[[webview2 layer] setBorderColor:
[[UIColor blackColor] CGColor]];
[[webview2 layer] setBorderWidth:2.75];
[[self view] addSubview:webview2];
[webview2 release];
}
////GoogleViewController.h
- (void)viewDidLoad
{
[super viewDidLoad];
webview=[[UIWebView alloc]initWithFrame:CGRectMake(10, 0,750,350)];
[[webview layer] setCornerRadius:10];
[webview setClipsToBounds:YES];
[[webview layer] setBorderColor:
[[UIColor blackColor] CGColor]];
[[webview layer] setBorderWidth:3];
[[self view] addSubview:webview];
[webview release];
NSString *url=#"http://www.google.com";
NSURL *nsurl=[NSURL URLWithString:url];
NSURLRequest *nsrequest=[NSURLRequest requestWithURL:nsurl];
[webview loadRequest:nsrequest];
[self.view addSubview:webview];
[webview setScalesPageToFit:YES];
webview.multipleTouchEnabled=YES;
[webview goBack];
[webview goForward];
webview.opaque = YES;
webview.backgroundColor = [UIColor clearColor];
}

Change this line's in your code and it will work:
first,create property of your webview inyour .h file
and write this code like this
[self.scrollView addSubview:apple.webview2];
[self.scrollView addSubview:google.webview];
And in your applevc put your webview code in initWithNibName method rather than viewdidload method
same in second vc.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
webview2=[[UIWebView alloc]initWithFrame:CGRectMake(0, 0,320,640)];
[webview2 setBackgroundColor:[UIColor redColor]];
NSString *url2=#"http://www.apple.com";
NSURL *nsurl2=[NSURL URLWithString:url2];
NSURLRequest *nsrequest2=[NSURLRequest requestWithURL:nsurl2];
[webview2 loadRequest:nsrequest2];
[self.view addSubview:webview2];
[webview2 setScalesPageToFit:NO];
webview2.multipleTouchEnabled=YES;
[[webview2 layer] setCornerRadius:10];
[webview2 setClipsToBounds:YES];
[[webview2 layer] setBorderColor:
[[UIColor blackColor] CGColor]];
[[webview2 layer] setBorderWidth:2.75];
[[self view] addSubview:webview2];
}
return self;
}

Related

How to open a youtube private link

NSURL *url=[NSURL URLWithString:#"https://m.youtube.com/watch?v=E9wVJJhDOYQ&t=0s"];
NSLog(#"url is %#", url);
MPMoviePlayerController * _moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
[[_moviePlayer view]setFrame:CGRectMake(50, 100, 200, 200)];
[_moviePlayer setFullscreen:NO animated:YES];
_moviePlayer.scalingMode = MPMovieScalingModeAspectFit;
_moviePlayer.shouldAutoplay= YES;
[[self view] addSubview: [_moviePlayer view]];

Jssocials not working in UIWebView/WKWebView

I want to use jssocial plugin in iOS,there is no response when user clicks on buttons be it a Facebook, twitter or Instagram from UIWebView and also not from WKWebView so, if anybody have implemented it in iOS then please guide me about it.
Any help will be appreciable. Thank You
Set WKWebView properties and delegates as follow
WKPreferences *prefs = [WKPreferences new];
prefs.javaScriptEnabled = YES;
prefs.javaScriptCanOpenWindowsAutomatically = YES;
WKWebViewConfiguration *theConfig = [[WKWebViewConfiguration alloc] init];
theConfig.preferences = prefs;
webView = [[EasyJSWKWebView alloc] initWithFrame:self.view.bounds
configuration:theConfig
withJavascriptInterfaces:interfaces];
webView.UIDelegate = self;
webView.navigationDelegate = self;
[webView setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
if (#available(iOS 11.0, *)) {
webView.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
}
[webView loadRequest:requestObj];
[self.view addSubview:webView];
- (WKWebView *)webView:(WKWebView *)webView createWebViewWithConfiguration:(WKWebViewConfiguration *)configuration forNavigationAction:(WKNavigationAction *)navigationAction windowFeatures: (WKWindowFeatures *)windowFeatures
{
ViewPopUp = [[UIView alloc] initWithFrame:self.view.frame];
[ViewPopUp setBackgroundColor:[UIColor whiteColor]];
[self.view addSubview:ViewPopUp];
UIButton *btnBack = [[UIButton alloc] initWithFrame:CGRectMake(0, 20, 50, 50)];
[btnBack setImage:[UIImage imageNamed:#"back"] forState:UIControlStateNormal];
[btnBack addTarget:self action:#selector(backButtonClicked) forControlEvents:UIControlEventTouchUpInside];
[ViewPopUp addSubview:btnBack];
PopUpView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height-64) configuration:configuration];
PopUpView.allowsBackForwardNavigationGestures = YES;
PopUpView.navigationDelegate = self;
[self.view addSubview:PopUpView];
return PopUpView;
}

Adding autolayout constraint programmatically turns uiview to black

I need a view controller without xib. There should be a webview with fully filled to the view. For that I've added the following code to loadView method.
- (void)loadView {
CGRect applicationFrame = [[UIScreen mainScreen] applicationFrame];
UIView *view = [[UIView alloc] initWithFrame:applicationFrame];
view.translatesAutoresizingMaskIntoConstraints = NO;
[view setBackgroundColor:[UIColor greenColor]];
[self setView:view];
// //create webview
self.webview = [[UIWebView alloc] initWithFrame:CGRectZero];
self.webview.translatesAutoresizingMaskIntoConstraints = NO;
[view addSubview:self.webview];
[self.webview setBackgroundColor:[UIColor orangeColor]];
[self.webview setDelegate:self];
NSDictionary *viewBindings = NSDictionaryOfVariableBindings(view,_webview);
// //add constraints
[view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|[_webview]|" options:0 metrics:nil views:viewBindings]];
[view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:|[_webview]|" options:0 metrics:nil views:viewBindings]];
}
But this turns the entire view to black. If I coment [view addConstraints:... method calls its showing a green view. What's wrong with my code?
I believe that the problem is that the parent view should not set translatesAutoresizingMaskIntoConstraints to false. The only view that must set that attribute to false is the one you are applying autolayout to, in this case webView. If you set view.translatesAutoresizingMaskIntoConstraints to false then you have to add constraints to view.
You don't need to change the root view of the UIViewController manually, maybe that's why it does not work.
My suggestion would be to try this out:
- (void) viewDidLoad {
[super viewDidLoad];
self.webview = [[UIWebView alloc] init];
self.webview.translatesAutoresizingMaskIntoConstraints = NO;
[view addSubview:self.webview];
[self.webview setBackgroundColor:[UIColor orangeColor]];
[self.webview setDelegate:self];
NSDictionary *viewBindings = NSDictionaryOfVariableBindings(view,_webview);
[view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|-0-[_webview]-0-|" options:0 metrics:nil views:viewBindings]];
[view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:|-0-[_webview]-0-|" options:0 metrics:nil views:viewBindings]];
// put a breakpoint after this line to see the frame of your UIWebView.
// It should be the same as the view
[self.view layoutIfNeeded];
}
This should work and your UIWebView should be full screen. Good luck!

add UINavigationController onto a UIScrollView

I'm putting a NavigationController as a child VC on a ScrollView. Here's the code:
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
self.automaticallyAdjustsScrollViewInsets = YES;
_scrollView = [UIScrollView newAutoLayoutView];
_scrollView.alwaysBounceVertical = YES;
_scrollView.delaysContentTouches = NO;
[self.view addSubview:_scrollView];
[_scrollView autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
_contentView = [UIView newAutoLayoutView];
UIView *yellowView = [UIView newAutoLayoutView];
yellowView.backgroundColor = [UIColor yellowColor];
[_contentView addSubview:yellowView];
[yellowView autoPinEdgeToSuperviewEdge:ALEdgeTop];
[yellowView autoSetDimensionsToSize:CGSizeMake(kScreenWidth, kScreenHeight)];
[_scrollView addSubview:_contentView];
_settingsNavigationController = [UINavigationController new];
[self addChildViewController:_settingsNavigationController];
[_contentView addSubview:_settingsNavigationController.view];
[_settingsNavigationController didMoveToParentViewController:self];
[_settingsNavigationController.view autoSetDimensionsToSize:CGSizeMake(kScreenWidth, kScreenHeight)];
[_settingsNavigationController.view autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:yellowView];
[_settingsNavigationController.view layoutSubviews];
[_contentView autoPinEdge:ALEdgeBottom toEdge:ALEdgeBottom ofView:_settingsNavigationController.view];
[_contentView autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
[_contentView autoPinEdge:ALEdgeLeading toEdge:ALEdgeLeading ofView:self.view];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
self.navigationController.navigationBarHidden = NO;
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
PCConfigViewController *configViewController = [PCConfigController new];
configViewController.morning = YES;
configClockViewController.delegate = self;
[_settingsNavigationController setViewControllers:[NSArray arrayWithObject:configViewController] animated:YES];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
I can scroll to the NavigationController and see its root view controller but it doesn't respond to touches, even though the NavigationController was added as a child view controller. Any ideas why, or how to make it responsive to touch?

ScrollsToTop is not working when UIVeiwController is presented from ios8 extension

ScrollsToTop is not working when UIVeiwController is presented from ios8 extension.
But it is working if we presented it from inside the app. Can anyone help? sample code is shared here.
UIScrollView * scrollview = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, 300) ];
[scrollview setContentSize:CGSizeMake(320, 800)];
scrollview.layer.borderWidth = 1.0;
scrollview.backgroundColor =[UIColor redColor];
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"slide1"]];
[scrollview addSubview:imageView];
scrollview.showsVerticalScrollIndicator =YES;
scrollview.scrollsToTop = YES;
scrollview.delegate = self;
UIViewController *a = [[UIViewController alloc] init];
a.view.backgroundColor = [UIColor greenColor];
[a.view addSubview:scrollview];
[self presentViewController:a animated:YES completion:^{
}];
I had same issues & resolved using code bellow.
Some change has done in xcode6. Try this code to make scrollview to top.
[self.ScrollView setContentOffset:CGPointMake(0.0, 0.0) animated:YES];