Can not open webpage using UIWebView - ios

I'm writing an app which lets users open up youtube exercise/fitness videos from a table view. So, I want to pass in a given website address for each cell in the table view. The code executes without errors but in the simulator does not open any webpage when I select a cell. Here is the code of the implementation file:
- (void)viewDidLoad
{
[super viewDidLoad];
//Create a URL object
NSURL *url = [NSURL URLWithString:urlAddress];
//Switch values based on exerciseNumber
switch (exerciseNumber) {
case 0:{
self.title = #"Straight Leg Raise";
urlAddress = #"http://www.youtube.com/watch?v=sb_2OVC4TuM";
}
break;
case 1:{
self.title = #"Supine Hip Abduction with Elastic Band";
urlAddress = #"http://www.youtube.com/watch?v=9gnmbjrjEuM";
break;
}
default:{
self.title = #"No Exercise Selected";
urlAddress = #"http://www.rehabview.com";
break;
}
}
//Create a URL request object
NSURLRequest *request = [NSURLRequest requestWithURL:url];
//load the request in the UIWebView
[webView loadRequest:request];
}
Any idea what I am doing wrong? Thanks in advance.

Create your NSURL object after your switch statement that sets the urlAddress string.

Related

Multiple UIButtons for One WebView

I am looking for a quicker method to build my application. As i have a series of buttons that are all going to open a UIWebView. But, I want to be able to use only One (1) WebView. I also want to be able to have the Buttons in a different View Controller.
So my question is, How would you get buttons to go from one view controller into another view controller that has a web viewer in it, and still be able to get different weblink's to load from the buttons?
I am still new to the programing community. I am doing this all as self teaching also.
Thank You guys!
Create one xib which will contain one UIWebView and buttons
and in target method of buttons, you assign the new url are reload the webView
As mentioned below
-(void)reloadWebVieWithUrl:(NSString *)strUrl
{
NSURL *url = [NSURL URLWithString:strUrl];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self.wbView loadRequest:request];
}
- (IBAction)buttonAction:(UIButton *)sender {
NSString *url = #"";
switch (sender.tag) {
case 100:
{
url = #"https://www.google.co.in/";
break;
}
case 101:
{
url = #"https://in.yahoo.com/";
break;
}
case 102:
{
url = #"https://www.facebook.com/";
break;
}
case 103:
{
url = #"http://stackoverflow.com/";
break;
}
default:
break;
}
[self reloadWebVieWithUrl:url];
}
In Swift
import UIKit
import WebKit
class ViewController: UIViewController {
#IBOutlet var wbView: UIWebView!
var strUrl = ""
#IBAction func buttonAction(sender: UIButton) {
switch (sender.tag){
case 100:
strUrl = "https://www.google.co.in/"
case 101:
strUrl = "https://in.yahoo.com/"
case 102:
strUrl = "https://www.facebook.com/"
case 103:
strUrl = "http://stackoverflow.com/"
default:
break;
}
reloadWebViewWithUrl(strUrl);
}
func reloadWebViewWithUrl(strUrl: NSString){
var url = NSURL(string: strUrl);
var request = NSURLRequest(URL: url);
wbView.loadRequest(request);
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
You don't need multiple webviews or VC's to do this.
You can just change the NSURL to reflect the website you want to navigate the user to.
So for example the action for button one could be :
NSString *webSite = "#http://google.com";
NSURL *url = [NSURL URLWithString:webSite];
NSURLRequest *request = [NSUrLRequest requestWithURL:url];
[webView loadRequest:request];
Then for button two - use the same code, using the same webView and just change the string to the website you want to navigate the user to.
Hope this helps.

UIWebView does not load full content from an url

I have a UIWebView where i load a url. But it does not load full content. I have checked all possible forums for solution but couldn't find a breakthrough yet. Any help would be really appreciated? You can try out the below url yourself. Basically, the url is supposed to contain html5 content/player.
-(void)loadPlayerUrl
{
activityIndicatorView.hidden = NO;
NSURL* nsUrl = [NSURL URLWithString:#"http://watch.nimbletv.com/tv"]
NSURLRequest* request = [NSURLRequest requestWithURL:nsUrl cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:30];
[self.playerWebView loadRequest:request];
}
However, this works perfectly in safari or any other browser.
You should create ViewControllerWebPage
Then
- (void)viewDidLoad
{
[super viewDidLoad];
self.screenName = #"Info Ekran";
NSURL *mobileCreaURL = [NSURL URLWithString:#"http://mobilecrea.com/"];
NSURLRequest *myRequest = [NSURLRequest requestWithURL:mobileCreaURL];
[mobileCrea loadRequest:myRequest];
}
Also create new view, select your new custom class (ViewControllerWebPage) and than create UIWebView

Completing NSURLRequest upon app launch

So I have a tabbed iOS app, and two of the view controllers in the app each have webViews in them. Nothing else. When these views are opened, they then call the NSURLRequest I have coded in the viewDidLoad method (as we all know). Very typical, basic, simple code.
What I am trying to do but haven't been able to figure out, is how to have these requests called and completed upon app launch, as opposed to being triggered when the view controller is viewed for the first time. It just takes too long to load.
I'm not very experienced with threads and blocks so any advice would help! I do know that all the UI stuff needs to be called on the main thread/queue. I have made an attempt at using another thread (commented out in view controller 2), so any further explanation as to what I already have would be great as well.
VC 1
- (void)viewDidLoad
{
[super viewDidLoad];
NSURL *url = [NSURL URLWithString:#"https://soundcloud.com/vanguardsf"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[_music loadRequest:request];
}
VC 2
- (void)viewDidLoad
{
[super viewDidLoad];
CGRect screen = [[UIScreen mainScreen]applicationFrame];
_webView = [[UIWebView alloc]initWithFrame:screen];
_webView.delegate = self;
[self.view addSubview:_webView];
NSString *netGym = #"http://www.netgym.com/login.asp";
NSURL *url = [NSURL URLWithString:netGym];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[_webView loadRequest:request];
NSLog(#"%#", request );
// if (!requestQueue) {
// requestQueue = dispatch_queue_create("come.requestNetgym.load", NULL);
// }
// dispatch_queue_t requestQueue = dispatch_queue_create("come.requestNetgym.load", NULL);
// dispatch_async(requestQueue, ^{
// NSString *netGym = #"http://www.netgym.com/login.asp";
// NSURL *url = [NSURL URLWithString:netGym];
// NSURLRequest *request = [NSURLRequest requestWithURL:url];
// [_webView loadRequest:request];
// NSLog(#"%#", request );
// });
}
You don't have to use GDC to send asynchrounus NSURLRequests - they are asynchronous if You don't use them synchronously on purpose.
You can start the requests in this method:
https://developer.apple.com/library/ios/documentation/uikit/reference/uiapplicationdelegate_protocol/Reference/Reference.html#//apple_ref/occ/intfm/UIApplicationDelegate/application:willFinishLaunchingWithOptions:
This is first place in application when You can execute a code just after launching is finished. You will save some time (little I suppose - this looks like a simple app).
I think that better solution is to cache the downloaded website data. In this case You will download the data first time You enter the app and the second time You will display downloaded content. In the background You will check if website has changed. If so You update the content.

iOS 7 UIWebView inconsistent Loading

In my project I have 2 UIWebViews that are IBOutlets and created in the main.storyboard.
One I load by calling a method on ViewDidAppear. It appears perfectly and hits all it's delegate methods. We will call this ViewA
The other is supposed to be Hidden and appear and load when the user taps a button. We will call this ViewB. But once I submit the request, nothing happens, none of the delegate methods get called.
ViewA loads perfectly. If I try to load ViewB on ViewDidAppear, it also loads.
EDIT: Moving the ViewA and ViewB load method calls to the ButtonPress method calls both successfully.
EDIT: Code
-(void)loadCarouselWebView //ViewA
{
NSString *urlString = [NSString stringWithFormat:#"%#%#",[[JSONReader sharedInstance] baseURLToLoad:environmentURLPublicKey],[[JSONReader sharedInstance] URLToLoad:kCarouselURLkey]];
NSURL *url = [NSURL URLWithString: urlString];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
self.carouselWebView.delegate = self;
[self.carouselWebView loadRequest: requestObj];
}
-(void)loadFullWebView:(int)index //ViewB
{
NSString *gameURL;
switch (index) {
case 1:
gameURL = game1Constant;
break;
case 2:
gameURL = game2Constant;
break;
case 3:
gameURL = game3Constant;
default:
break;
}
NSString *urlString = [NSString stringWithFormat:#"%#%#",[[JSONReader sharedInstance] baseURLToLoad:environmentURLKey],[[JSONReader sharedInstance] URLToLoad:gameURL]];
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
self.fullWebView.delegate = self;
[self.fullWebView loadRequest: requestObj];
}
- (IBAction)gameButtonPushed:(id)sender {
[[self fullWebView] setHidden:NO];
[[self backNavButton] setHidden:NO];
[self loadFullWebView:[sender tag]];
[self loadCarouselWebView];//Added for debugging
}
EDIT: ONLY Loading ViewB on button tap works. If I add Load ViewA on ViewDidAppear then ViewB will not load.
In a surprise twist, there is nothing wrong with the UIWebView.
It was my fault for not reading the code carefully enough.
Someone coded a one time counter into the shouldStartLoadingRequest delegate method that I missed. So only one request was allowed to load.
Thats like 2 days burned there, great stuff! >:E

UIWebView loadRequest reuse gives flickering effect

I have multiple pdf files. Based on user input i am loading the pdf using UIWebView. On first loadRequest it loads the pdf properly. From second call to LoadRequest onwards its showing some flickering effect while loading the new pdf.. Meaning that starts blur display of content and slowly display content properly in few secs.
Code snippet below:
- (void) loadDocument: (NSString *) documentName
{
NSString * path = [[NSBundle mainBundle] pathForResource: documentName ofType: self.docType];
NSURL * url = [NSURL fileURLWithPath: path];
request = [NSURLRequest requestWithURL: url];
[PdfWebView loadRequest: request];
}
- (void) loadNewDoc:(int)segIndex
{
switch (mPageIndex)
{
case 0:
[self loadDocument:#"PDF_0"];
break;
case 1:
[self loadDocument:#"PDF_1"];
break;
case 2:
[self loadDocument:#"PDF_2"];
break;
default:
break;
}
}
You can clear the web view before starting the new request by using
[yourwebview loadHTMLString:#"<html><head></head><body></body></html>" baseURL:nil];
Or even
[yourwebview stringByEvaluatingJavaScriptFromString:#"document.open();document.close();"];
This may remove the flickering effect
for me, this helped (its robovm java code):
UIView.transition(_wv, 0.5, UIViewAnimationOptions.TransitionCrossDissolve, new Runnable() {
#Override
public void run() {
_wv.loadData(new NSData(pdf), "application/pdf", "utf-8", null);
}
}, null);

Resources