uiwebview not loading any content - uiwebview

I am not able to load any webpages.
Any idea why this could be?
The frame for the webview (printed from the debugger)
<UIWebView: 0x8a49840; frame = (0 0; 320 241); autoresize = TM+BM; layer = <CALayer: 0x8a498f0>>
Here is my code:
#import "ViewController.h"
#interface ViewController () <UIWebViewDelegate>
#end
#implementation ViewController
#synthesize webview;
- (void)viewDidLoad
{
[super viewDidLoad];
self.webview.delegate = self;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)viewDidAppear:(BOOL)animated
{
}
- (IBAction)buttonPress:(id)sender {
NSLog(#"pressed");
NSURL *theUrl = [NSURL URLWithString:#"www.google.com"];
NSError *e ;
// just for test - ALSO returning nil!!!
NSString *str = [NSString stringWithContentsOfURL:theUrl encoding:NSUTF8StringEncoding error:&e];
NSLog(#"%#",str);
NSURLRequest *theRequest = [NSURLRequest requestWithURL:theUrl];
[self.webview loadRequest:theRequest];
}
- (void)webViewDidStartLoad:(UIWebView *)webView {
NSLog(#"page is loading");
}
// this method is never called
-(void)webViewDidFinishLoad:(UIWebView *)webView {
NSLog(#"finished loading");
}
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
return YES;
}
#end

use this one it'l work.
problem: in your url http:// was missed
NSURL *theUrl = [NSURL URLWithString:#"http://www.google.com"];

You don't have proper URL.
Because +URLWithString: expects a protocol (e.g. http://, https://),
if you have www.google.com in that case it cannot build a URL.So please try to use like this..
NSURL *url = [NSURL URLWithString:#"http://www.google.com"];

You may also check if your link doesn't contain the "http://" or "https://" and add it.
NSString *website = #"www.google.com";
NSRange textRangeHTTP = [[website lowercaseString] rangeOfString:#"http://"];
NSRange textRangeHTTPS = [[website lowercaseString] rangeOfString:#"https://"];
if((textRangeHTTP.location == NSNotFound) && (textRangeHTTPS.location == NSNotFound))
website = [NSString stringWithFormat:#"http://%#",website];
NSURL *theUrl = [NSURL URLWithString:website];

just use this in your buttoned pressed method
NSURL *theUrl = [NSURL URLWithString:#"http://www.google.com"];
NSURLRequest *theRequest = [NSURLRequest requestWithURL:theUrl];
[self.webview loadRequest:theRequest];
always make sure that using the protocol type that is "http://" is prefixed to the web url string

(Xcode 5 iOS 7) Needed to update an Universal App for iOS 7 and Xcode 5. It is an open source project / example located here: Link to SimpleWebView (Project Zip and Source Code Example)

Related

Why is shouldStartLoadWithRequest only being invoked the first time through?

I'm trying to display a list of web pages the user can switch between in my app. I've done this successfully with documents. however, I don't want the user to be able to navigate from these pages. I found out how to do this by looking it up. The problem is that it only executes on the first page. When I switch to another page shouldStartLoadWithRequest stops working even when I go back to the first page. (This is all done with my specially designed back and forward buttons.) How can I fix this so that it will be invoked every single time and prevent the user from clicking links and navigating away from my set pages? I'm new to objective-c. All the relevant code is below. Some things were declared globally in the .h. Sorry it's kind of rough. I tabbed over only what was needed to make it into code formatting. Thanks!
My .h file:
#import <UIKit/UIKit.h>
#interface learnViewController : UIViewController <UINavigationControllerDelegate>
{
IBOutlet UIWebView *web;
UIButton *backButton;
UIButton *forButton;
}
#end
My .m file
#import "learnViewController.h"
#import "ViewController.h"
static NSString* links[] =
{
#"http://www.nlm.nih.gov/medlineplus/ency/article/000141.htm",
#"http://www.nlm.nih.gov/medlineplus/ency/article/000065.htm",
#"http://www.nlm.nih.gov/medlineplus/ency/article/001087.htm",
#"http://www.nlm.nih.gov/medlineplus/ency/article/000091.htm",
#"http://www.nlm.nih.gov/medlineplus/ency/article/007270.htm",
#"http://www.nlm.nih.gov/medlineplus/ency/article/000145.htm",
#"http://www.nlm.nih.gov/medlineplus/ency/article/000093.htm",
#"http://www.nlm.nih.gov/medlineplus/ency/article/000087.htm",
#"http://www.nlm.nih.gov/medlineplus/ency/article/000140.htm",
#"http://www.nlm.nih.gov/medlineplus/ency/article/000132.htm"
};
int numlinks = 10;
int i = 0;
#interface learnViewController ()
#end
#implementation learnViewController
- (void)viewDidLoad
{
[super viewDidLoad];
float viewWidth = self.view.frame.size.width;
float viewHeight = self.view.frame.size.height;
self.view.multipleTouchEnabled = true;
//Background
UIImageView *backgroundView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, viewWidth, viewHeight)];
backgroundView.image = [UIImage imageNamed:#"Background.png"];
[self.view addSubview:backgroundView];
//Show Portal
web = [[UIWebView alloc]initWithFrame:CGRectMake(0.0, viewHeight/34.0, viewWidth, (viewHeight - viewHeight/6.4))];
self->web.delegate = self;
web.scrollView.scrollEnabled = TRUE;
web.scalesPageToFit = TRUE;
[self loadDocument:links[i] inView:web];
[self.view addSubview:web];
//Buttons
UIButton *homeButton = [[UIButton alloc] initWithFrame:CGRectMake((viewWidth/2.0 - viewWidth/6.0/2.0), (viewHeight - viewHeight*280.0/2208.0/2.0 - viewWidth/6.0/2.0), (viewWidth/6.0), (viewWidth/6.0))];
[homeButton
setBackgroundImage:[UIImage imageNamed:#"Home.png"]
forState:UIControlStateNormal];
[homeButton addTarget:self action:#selector(homePressed:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:homeButton];
backButton = [[UIButton alloc] initWithFrame:CGRectMake((viewWidth/2.0/3.5 - viewWidth/9.0/2.0), (viewHeight - viewHeight*280.0/2208.0/2.0 - viewWidth/9.0/2.0), (viewWidth/9.0), (viewWidth/9.0))];
[backButton
setBackgroundImage:[UIImage imageNamed:#"Back.png"]
forState:UIControlStateNormal];
backButton.enabled = FALSE;
[backButton addTarget:self
action:#selector(backPressed:)
forControlEvents:UIControlEventTouchDown];
[self.view addSubview:backButton];
forButton = [[UIButton alloc] initWithFrame:CGRectMake((viewWidth/2.0/3.5*2.0 - viewWidth/9.0/2.0), (viewHeight - viewHeight*280.0/2208.0/2.0 - viewWidth/9.0/2.0), (viewWidth/9.0), (viewWidth/9.0))];
[forButton
setBackgroundImage:[UIImage imageNamed:#"Forward.png"]
forState:UIControlStateNormal];
[forButton addTarget:self
action:#selector(forPressed:)
forControlEvents:UIControlEventTouchDown];
[self.view addSubview:forButton];
UIButton *refButton = [[UIButton alloc] initWithFrame:CGRectMake((viewWidth - viewWidth/2.0/3.5*2.0 - viewWidth/9.0/2.0), (viewHeight - viewHeight*280.0/2208.0/2.0 - viewWidth/9.0/2.0), (viewWidth/9.0), (viewWidth/9.0))];
[refButton
setBackgroundImage:[UIImage imageNamed:#"Scale.png"]
forState:UIControlStateNormal];
[refButton addTarget:self
action:#selector(refPressed:)
forControlEvents:UIControlEventTouchDown];
[self.view addSubview:refButton];
UIButton *webButton = [[UIButton alloc] initWithFrame:CGRectMake((viewWidth - viewWidth/2.0/3.5 - viewWidth/9.0/2.0), (viewHeight - viewHeight*280.0/2208.0/2.0 - viewWidth/9.0/2.0), (viewWidth/9.0), (viewWidth/9.0))];
[webButton
setBackgroundImage:[UIImage imageNamed:#"Web.png"]
forState:UIControlStateNormal];
[webButton addTarget:self
action:#selector(webPressed:)
forControlEvents:UIControlEventTouchDown];
[self.view addSubview:webButton];
}
//Button presses
-(void)homePressed:(id)sender
{
ViewController *home = [[ViewController alloc] initWithNibName:nil bundle:nil];
[self presentViewController:home animated:YES completion:NULL];
}
- (void) refPressed:(id)sender
{
[self loadDocument:links[i] inView:web];
}
-(void)backPressed:(id)sender
{
float viewWidth = self.view.frame.size.width;
float viewHeight = self.view.frame.size.height;
if (i > 0)
{
[web removeFromSuperview];
i--;
web = [[UIWebView alloc] initWithFrame:CGRectMake(0.0, viewHeight/34.0, viewWidth, (viewHeight - viewHeight/6.4))];
[self loadDocument:links[i] inView:web];
web.scrollView.scrollEnabled = TRUE;
web.scalesPageToFit = TRUE;
[self.view addSubview:web];
forButton.enabled = TRUE;
}
if (i == 0)
{
backButton.enabled = FALSE;
}
}
-(void)forPressed:(id)sender
{
float viewWidth = self.view.frame.size.width;
float viewHeight = self.view.frame.size.height;
if (i < numlinks - 1)
{
[web removeFromSuperview];
i++;
web = [[UIWebView alloc] initWithFrame:CGRectMake(0.0, viewHeight/34.0, viewWidth, (viewHeight - viewHeight/6.4))];
[self loadDocument:links[i] inView:web];
web.scrollView.scrollEnabled = TRUE;
web.scalesPageToFit = TRUE;
[self.view addSubview:web];
backButton.enabled = TRUE;
}
if (i == numlinks - 1)
{
forButton.enabled = FALSE;
}
}
-(void)loadDocument:(NSString*)documentName inView:(UIWebView*)webView
{
NSString *url = links[i];
NSURL *nsurl = [NSURL URLWithString:url];
NSURLRequest *nsrequest = [NSURLRequest requestWithURL:nsurl];
[webView loadRequest:nsrequest];
}
-(void)webPressed:(id)sender
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:links[i]]];
}
-(BOOL) webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSURL *url = request.URL;
NSString *urlString = url.absoluteString;
NSRange range = [urlString rangeOfString:links[i]];
if (range.location != NSNotFound)
{
return YES;
}
else
{
return NO;
}
}
- (void)viewDidUnload
{
[self->web stopLoading];
self->web.delegate = nil;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
Okay, I figured it out through trial and error since no one wanted to help me. I tried looking for answers to similar questions, but nothing actually worked for me. If you're reading this and having the same problem, this is how you fix it. All you have to do is reset the web delegate to nil then back to self as so:
self.webView.delegate = nil;
self.webView.delegate = self;
I don't know why everyone else's code was so complex, and I can only speculate as to why this works. I just got lucky honestly, but I think it maybe clears the cache when you do this, thus making each page fresh to the program. That's what other people were talking about. However, their methods did not work for me either because of my lack of experience or because they do not fit my particular case. This is much simpler and is guaranteed to work as long as you make sure it does this each time you load one of your designated web pages. You're welcome if you're reading this. If anyone would so kindly as to look at my post and explain why this works in better detail, please feel free to do so!

GPUImageVideoCamera save video on ios 7

I'm trying to achieve the square video recording like 300*300 so I choose GPUImage but its not working on IOS 7 and giving errors like [UIView nextAvailableTextureIndex]: unrecognized selector sent to instance the error starts when we build the even the sample code
when trying to save the GPUImageVideoCamera
some times its stucks at [movieWriter startRecording]; 
is the GPUImage compatible with ios 7 or we have made some changes ?
here is the code
- (void)viewDidLoad
{
[super viewDidLoad];
videoCamera = [[GPUImageVideoCamera alloc] initWithSessionPreset:AVCaptureSessionPreset640x480 cameraPosition:AVCaptureDevicePositionBack];
videoCamera.outputImageOrientation = UIInterfaceOrientationPortrait;
videoCamera.horizontallyMirrorFrontFacingCamera = NO;
videoCamera.horizontallyMirrorRearFacingCamera = NO;
filter = [[GPUImageSepiaFilter alloc] init];
initWithRotation:kGPUImageRotateRightFlipVertical];
[videoCamera addTarget:filter];
GPUImageView *filterView = (GPUImageView *)self.view;
[filter addTarget:filterView];
sharing
NSString *pathToMovie = [NSHomeDirectory() stringByAppendingPathComponent:#"Documents/Movie.m4v"];
unlink([pathToMovie UTF8String]); // If a file already exists, AVAssetWriter won't let you record new frames, so delete the old movie
NSURL *movieURL = [NSURL fileURLWithPath:pathToMovie];
movieWriter = [[GPUImageMovieWriter alloc] initWithMovieURL:movieURL size:CGSizeMake(480.0, 640.0)];
[filter addTarget:movieWriter];
}
- (IBAction)stopRecording:(id)sender {
[filter removeTarget:movieWriter];
videoCamera.audioEncodingTarget = nil;
[movieWriter finishRecording];
}
- (IBAction)startRecording:(id)sender {
videoCamera.audioEncodingTarget = movieWriter;
[movieWriter startRecording];
[videoCamera startCameraCapture];
}
My guess is that you modified the .xib or storyboard and didn't set the class of the view that is showing the camera preview to GPUImageView.

Mail composer didn't display under my UIWebView

I have a MainController and detailedcontroller. When I popover the Book selection and select a Book, the detailcontroller display an UIWebView with the book articles :
#interface IpadBooksViewController : UITableViewController {
SearchResult *searchResult;
IpadArticleViewController *detailController;
IpadMainViewController *mainController;
UIPopoverController *popover;
}
Into the UIWebView, I display an Email icon and catch the scheme :
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
NSURL *url = [request URL];
NSString *scheme = [url scheme];
NSString *host = [url host];
if ([[url description] hasSuffix:#"next"]) {
NSLog(#"next Show");
}
BOOL isShareLinks = [host isEqualToString:#"displayShareLinks"];
BOOL isFavoriteLinks = [host isEqualToString:#"displayFavoriteLinks"];
if ([#"myappurl" isEqualToString:scheme] && (isShareLinks || isFavoriteLinks)) {
self.selectedArticleNumber = [url.path lastPathComponent];
if (isShareLinks) {
[self sendMailArticleNumber:selectedArticleNumber];
} else if (isFavoriteLinks) {
NSLog(#"ipad favorite clicked");
[self toggleFavorite:selectedArticleNumber];
Broker *broker = [[Broker alloc] init];
[broker loadProjects:self];
[broker release];
}
}
return [super webView:webView shouldStartLoadWithRequest:request navigationType:navigationType];
}
Action is supposed to display the MFMailController under my UIWebView, but nothing is displayed without error message :
- (void) sendMailArticleNumber:(NSString *)articleNumber {
MFMailComposeViewController* composer = [[MFMailComposeViewController alloc] init];
composer.mailComposeDelegate = self;
[composer setSubject:#"Article"];
NSString *messageBody = [Article fetchBody:articleNumber bookId:bookId];
[composer setMessageBody:messageBody isHTML:YES];
[self presentModalViewController:composer animated:YES];
[composer release];
}
Any help will be welcomed. I did try creating a popover, addView atIndex without success ... Let me know if you need more code.
David
I did solve the issue by loading the controller into the delegate as following :
[((PublilexAppDelegate*)[[UIApplication sharedApplication] delegate]) sendMailArticleNumber:selectedArticleNumber bookId:self.bookId];
And then into the Delegate :
- (void) sendMailArticleNumber:(NSString *)articleNumber bookId:(NSString*)bookId {
MFMailComposeViewController* composer = [[MFMailComposeViewController alloc] init];
composer.mailComposeDelegate = self;
[composer setSubject:#"Article"];
NSString *messageBody = [Article fetchBody:articleNumber bookId:bookId];
[composer setMessageBody:messageBody isHTML:YES];
[self->navigationController presentModalViewController:composer animated:YES];
[composer release];
}
And to dismiss and comeback to the previous view :
-(void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
[self->navigationController dismissModalViewControllerAnimated:YES];
// [self dismissViewControllerAnimated:YES completion:nil];
// [((PublilexAppDelegate*)[[UIApplication sharedApplication] delegate]) navigateToIpadMain];
}

Loading Text from a .rtf

Hei guys,
I'm trying to load a long text from a .rtf file and I want to show this text in a UITextView.
I store all the .rtf files in a folder called "rtf" into the "Supporting Files" folder.
This is my code.
- (void)setDetailItem:(id)newDetailItem
{
if (_detailItem != newDetailItem) {
_detailItem = newDetailItem;
// Update the view.
[self configureView];
}
}
- (void)configureView
{
// Update the user interface for the detail item.
if (self.detailItem)
{
self.textView.text = [self setTextForTextView:[self.detailItem description]];
}
}
-(NSString *)setTextForTextView:(NSString *)description
{
NSString *path = [NSString stringWithFormat:#"rtf/%#.rtf" ,description];
NSLog(#"%#" ,path);
NSString *myText = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
return myText;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.title = #"Text";
// Do any additional setup after loading the view, typically from a nib.
[self configureView];
}
But It doesn't show me the text and I don't understand why...
Thanks!
I just solved in this way:
-(NSString *)setTextForTextView:(NSString *)description
{
NSString *filePath = [[NSBundle mainBundle] pathForResource:description ofType:#"txt"];
if (filePath)
{
NSString *myText = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
if (myText)
{
return myText;
}
}
}
I hope this will help some people in the future! :D

Google Chart URL can not be loaded by WebView (NSUrl remains nil)

I try to load an URL from Google Chart in a UIWebView. Here is the interesting code:
NSString *urlAddress = #"http://chart.apis.google.com/chart?chxr=0,5,398.333&chxs=0,676767,11.167,0.833,l,676767&chxt=x&chs=300x225&cht=lc&chco=3D79FF&chd=s:ilowy0zvvzrligikqsrl&chg=14.3,-1,0,0&chls=2&chm=B,C5D4EABB,0,0,0|R,FF0000,0,0,0.03|R,000000,0,0.3,0.33";
NSURL *url = [NSURL URLWithString:urlAddress];
if (url == nil) {
NSLog(#"URL error occured");
} else {
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
}
The message "URL error occured" is logged.
Thanks in advance!
NSString *str =#"http://chart.apis.google.com/chart?chf=bg,s,00000000&chs=300x225&cht=p3&chco=E91616|0000FF&chd=t:50,21&chp=2.9&chma=0,0,0,7";
str = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *chartURL = [NSURL URLWithString:str];
Use like this.
I've found the solution on my own:
The NSUrl object does not accept pipes (|) within an URL. One has to escape the pipes by replacing them with %7c

Resources