Create Separate UIwebViews - ios

I have two UIwebViews. I coded each one to go to a different webpage url. But both of them go to the first url (http://test.bithumor.co/test26.php) Here's the code from the view controller.m
//
// ViewController.m
// BitHumor
//
// Created by danny rodriguez on 7/26/15.
// Copyright (c) 2015 BitDeveloping. All rights reserved.
//
#import "ViewController.h"
#interface ViewController ()
#property (strong, nonatomic) IBOutlet UIWebView *webView;
#property (weak, nonatomic) IBOutlet UIWebView *webView2;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIWebView *webview=[[UIWebView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,self.view.frame.size.height)];
NSString *url=#"http://test.bithumor.co/test26.php";
NSURL *nsurl=[NSURL URLWithString:url];
NSURLRequest *nsrequest=[NSURLRequest requestWithURL:nsurl];
[webview loadRequest:nsrequest];
[self.view addSubview:webview];
// Do any additional setup after loading the view, typically from a nib.
UIWebView *webview2=[[UIWebView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,self.view.frame.size.height)];
NSString *url2=#"http://google.com";
NSURL *nsurl2=[NSURL URLWithString:url2];
NSURLRequest *nsrequest2=[NSURLRequest requestWithURL:nsurl2];
[webview2 loadRequest:nsrequest2];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
Both web views are linked to the #property, how do I make each of them go to their designated webpage urls? (Please tell me step by step, as I am new to Objective-C coding)

Looks like you already created IBOutlets for UIWebViews make sure to define them as weak, you defined one of them as Strong and other one as weak, the mistake that you have did is creating third UIWebView that you have added as subview. This will cover whole view above your two uiwebviews.
//Not required, remove this code
UIWebView *webview=[[UIWebView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,self.view.frame.size.height)];
[self.view addSubview:webview];
Now just create two requests and load them to UIWebViews
//For webview 1
NSString *url=#"http://test.bithumor.co/test26.php";
NSURL *nsurl=[NSURL URLWithString:url];
NSURLRequest *nsrequest=[NSURLRequest requestWithURL:nsurl];
[self.webView loadRequest:nsrequest];
//For webview 2
NSString *url2=#"http://google.com";
NSURL *nsurl2=[NSURL URLWithString:url2];
NSURLRequest *nsrequest2=[NSURLRequest requestWithURL:nsurl2];
[self.webView2 loadRequest:nsrequest2];

Related

UIWebView Screen Fitting Issue

I have developed a website in Twitter Bootstrap, I am now using a UIWebView to display that site inside of an IOS app. I am getting it to display fine, but the screen is off and it is running off of the side of the screen... I feel like I might have my UIWebView configured wrong, the site looks like if I go to it in Safari on my mobile device, so I know the site is working properly.
It even displays Google wrong in the UIWebView.... I will display my code and 2 screen shots below(The first screenshot is what it currently looks like, the second is what it should look like and what it looks like in safari mobile :
ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
#property (strong, nonatomic) IBOutlet UIWebView *webView;
#end
ViewController.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSURL *url = [NSURL URLWithString:#"http://www.google.com"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[_webView loadRequest:request];
_webView.scalesPageToFit = YES;
_webView.frame=self.view.bounds;
[_webView loadRequest:request];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
AS-IS Screen Shot
TO-BE Screen Shot
No need to use loadRequest: two time, Use:
- (void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
[_webView reload];
}
To me it looks like the UIWebView is not set up correctly in the Storyboard or Interface Builder. Do you use Auto Layout? If so, are the constraints set correctly?
A simple way to make sure the UIWebView is displayed full screen would be to add 4 constraints that align it with the top, bottom, leading and trailing edges of its superview, e.g. like so:
EDIT: Here are some rough instructions on how to add those constraints.
You should add the following code after [_webView loadRequest:request]; :
_webView.scalesPageToFit = YES;
and If your UIWebView is not located properly, add the following code too :
_webView.frame=self.view.bounds;
In the Utilities pane, select "Scales Pages to Fit":
In the view controller in which the web view resides, make sure you can view the boxes (or bounds) of the view (note: the bottom doesn't appear because I couldn't fit it within the image):
Code in view controller:
#import "SomeViewController.h"
#interface SomeViewController ()
#end
#implementation SomeViewController
#synthesize webView;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
NSString *urlLink = #"www.google.com";
NSURL *url = [NSURL URLWithString:urlLink];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[self.webview loadRequest:urlRequest];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
Photo of the other settings in the view controller:

ios UIButton not loading url in UIWwebview

I have made an iOS app to load a simple UIWebView *mWeb.
Once the app starts it loads my originalURL.
At the bottom of the UI, I have a UIButton placed *btnHome.
When I click this UIButton I want it load another URL *loadNextURL.
However something's not working on my end... HELP!
Here's the code for my myViewController.h file.
#import "myViewController.h"
#interface myViewController ()
#property (weak, nonatomic) IBOutlet UIButton *btnHome;
#property (weak, nonatomic) IBOutlet UIWebView *mWeb;
#end
#implementation myViewController
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *originalURL = #"http://myOriginalLink";
NSURL *url = [NSURL URLWithString:originalURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[_mWeb loadRequest:requestObj];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (IBAction)btnHome:(id)sender
{
NSString *loadNextURL = #"http://link2";
NSURL *url2 = [NSURL URLWithString:loadNextURL];
NSURLRequest *reqObj = [NSURLRequest requestWithURL:url2];
[_mWeb loadRequest:reqObj];
}
#end
Well there can be two problems, first check that your action method is bound to the button or not. Second the url in stringVariable loadNextUrl is valid or not.
Now how to check the url is valid or not, just copy the url and paste into the browser. If the link does not open then the url is not valid. So for that pass the valid url in the string variable and then try.
Check your Url it may be invalid. If it works fine in your browser then check your method. It may not be linked with your button with touchUpInside event. Then try using [yourWebView loadRequest:[NSURLRequest requestWithURL:#"yourLink"]]; . Also do check whether your web view is not nil. It won't be nil as you are able to load it in viewDidLoad. But do check it.

WebView shows only white although delegate is set and methods are being called

I have a viewcontroller that loads a webView in the view did load
#synthesize webView = _webView;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
self.webView = [[UIWebView alloc] init];
[self.webView setDelegate:self];
NSString *string = #"http://www.google.com";
NSURL *url = [NSURL URLWithString:string];
NSLog(#"The URL is %#", url);
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSLog(#"The request is %#", request);
NSLog(#"webview delegate is %#", self.webView.delegate);
NSLog(#"webview is %#", self.webView);
[self.webView loadRequest:request];
}
Everything is working fine when i debug meaning that nothing is returning nil. I declared the property in the .h file like this
#property (nonatomic, retain) IBOutlet UIWebView *webView;
I have also tried
#property (nonatomic, strong) IBOutlet UIWebView *webView;
and the WebView in IB is hooked up to delegate and this property. The problem is that no matter what url I try (I have tried them all in the browser), the web view only returns a white screen. All the delegate methods have been called, I know because I logged to the console. Does anyone know how to fix this or what I am doing wrong.
From your code -
#property (nonatomic, retain) IBOutlet UIWebView *webView;
Since you are connected webview to an outlet, the instance of webview is created after the unarchiving of your Nib.
So no need to instantiate webview again -
self.webView = [[UIWebView alloc] init];
Remove the above line from your code and test it. Should work!
Well, the fix for this issue seems to me pretty straightforward.
You're not setting any frame for your web view. Try the following:
self.webView = [[UIWebView alloc] initWithFrame:CGRectMake(0,0,500,500)];
Of course, the actual dimensions for your web view may vary. You'll have to adjust it yourself.
Update
I'm seeing that you're not adding the web view as a subview. Add the following line after the call to loadRequest:
[self.view addSubview:self.webView];
Hope this helps!

Scrolling PDF programmatically in UIWebView

I wanna to scroll(down) on a UIWebView(iPad Application), where I will display a local pdf file, programmatically.
I have added a UIWebView with the interface builder and linked it.
#interface ViewController : UIViewController <UIWebViewDelegate>
#property (weak, nonatomic) IBOutlet UIWebView *webView;
#end
This is my code for getting the pdf file and scrolling. The webViewDidFinishLoad function gets called.
#synthesize webView = _webView;
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *path = [[NSBundle mainBundle] pathForResource:#"test" ofType:#"pdf"];
NSURL *targetURL = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
self.webView.delegate = self;
[self.webView loadRequest:request];
}
#pragma mark - UIWebViewDelegate
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
NSLog(#"didLoad");
self.webView.scrollView.contentOffset = CGPointMake(0, 500);
}
I am using the iOS SDK 6.1, the iOS deployment target is also 6.1.
Heres the log:
2013-04-23 09:23:08.742 PDFViewer[3834:c07] DiskImageCache: Could not resolve the absolute path of the old directory.
2013-04-23 09:23:08.913 PDFViewer[3834:c07] didLoad
The UIWebView isn't scrolling. The pdf is still at the first page.
already answered here --- how to set scroll position on uiwebview , try calling content offset code snippet after a delay
I don't think you need to set the webView.scrollView.contentOffset.
try this
[webView setScalesPageToFit:YES];

UIWebView show blank to iOS dev beginner

I'm new for iOS develop. I tried to make a very simple app just show a UIWebView which display a web page:
ViewController.m:
#import "ViewController.h"
#implementation ViewController
#synthesize webView = _webView;
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSString *urlAddress = #"http://jsfiddle.net/emj365/qaQnF/embedded/result/";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:requestObj];
}
...
ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController {
UIWebView * _webView;
}
#property (nonatomic, retain) IBOutlet UIWebView * webView;
#end
UIWebView show blank. What's wrong?
There are a few different things that could go wrong. A few options you should check:
Did you check to see if the outlet was actually connected to the webview in the xib?
Implement the UIWebView delegate and check for (networking) errors

Resources