In viewDidLoad, I'm using NSURLRequest and NSURLConnection:
NSURLRequest *site_request =
[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.google.com/"]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:10.0];
NSURLConnection *site_connection =
[[NSURLConnection alloc] initWithRequest:site_request delegate:self];
and then I use
-(void)connection:(NSURLConnection *)site_connection didReceiveData:(NSData *)data
{
site_response = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
}
and I have the whole HTML in site_response.
I want to create an invisible UIWebView which will "open" the page from the NSURLRequest in order to use JavaScript to get content like this:
NSString *myText = [my_webView stringByEvaluatingJavaScriptFromString:
#"document.documentElement......"];
In my .h I have:
UIWebView *my_webview;
#property (nonatomic, retain) UIWebView *my_webview;
and in my .m I have:
#synthesize torrents_webview;
My viewDidLoad after NSURLRequest has
[my_webview loadRequest:site_request];
and I use
-(void)webViewDidFinishLoad:(UIWebView *)webView
{
//an alertview here
}
in order to be sure it works. But nothing happens. It doesn't give an alert view.
What am I doing wrong?
webViewDidFinishLoad: is a method of the UIWebView delegate. You are not setting the delegate anywhere in the code you have shown.
#interface YourClass : UIViewController <UIWebViewDelegate>
...
- (void)loadView
{
self.webView.delegate = self;
}
...
- (void)dealloc
{
self.webView.delegate = nil;
}
Also if you use NSURLRequest you're going to get the page again. But there's no need to use NSURLConnection, just go straight to loading the UIWebVIew using the NSURLRequest.
Or if you must use NSURLConnection then when the file has downloaded save it to disk and use LoadHTMLString to load the contents.
ViewController.h
#interface TopTorrents_ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource,UIWebViewDelegate>
{
UIWebView *torrents_webview;
}
#property (nonatomic, retain) UIWebView *torrents_webview;
ViewController.m
#synthesize torrents_webview;
- (void)viewDidLoad
{
torrents_webview.delegate = self;
NSURLRequest *site_request = [NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.google.gr/"] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10.0];
[torrents_webview loadRequest:site_request];
[super viewDidLoad];
}
-(void)webViewDidFinishLoad:(UIWebView *)webView
{
NSString *myText = [torrents_webview stringByEvaluatingJavaScriptFromString:#"document.getElementsByTagName('body')[0]"];
UIAlertView *my_alert = [[UIAlertView alloc] initWithTitle:#"mytitle" message:myText delegate:nil cancelButtonTitle:#"my button" otherButtonTitles:nil,nil];
[my_alert show];
}
this is my updated code... thanks
Related
I have developed the webView in method viewDidLoad in ViewController
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
id <UIWebViewDelegate> delegate =[[MyDelegate alloc] init];
webView.delegate = delegate;
NSError *error;
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:#"index" ofType:#"html"];
NSString *htmlContent = [[NSString alloc] initWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:&error];
[webView loadHTMLString:htmlContent baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]];
[self.view addSubview:webView];
I set the delegate on instance of class MyDelegate.
In MyDelegate Class:
#import <UIKit/UIKit.h>
#interface MyDelegate : NSObject <UIWebViewDelegate>
#end
#import "MyDelegate.h"
#implementation MyDelegate
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
return YES;
}
#end
But app my crash during start the loasding.
If I not load html content, but url ('google.com' for example) crash happens.
When I comment this 'webView.delegate = delegate;' crash doesn't happens.
I know that I can use this in ViewController.h:
#interface ViewController : UIViewController<UIWebViewDelegate>
and this in viewDidLoad:
webView.delegate = self;
but I need use other class as delegate (not ViewController), but webview must be located in ViewController.
How I can make this?
Help me!
Let me point to the root cause.
UIWebView delegate attribute is a weak reference ("unowned(unsafe)" in Swift source code), which means its memory can be freed at any time.
So to solve this, you have to keep a reference into your controller as a class attribute.
Example of solution tested successfully in Swift:
class MyUIViewController : UIViewController{
let leftDelegate:MyWebViewDelegate = MyWebViewDelegate()
...
}
- (void)viewDidLoad {
[super viewDidLoad];
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
id<UIWebView> delegate =(id)self;
webView.delegate = delegate;
NSError *error;
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:#"index" ofType:#"html"];
NSString *htmlContent= [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:&error];
[webView loadHTMLString:htmlContent baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]];
[self.view addSubview:webView];
}
MyDelegate.h
#import <UIKit/UIKit.h>
#protocol UIWebView <UIWebViewDelegate>
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;
#end
#interface MyDelegate : NSObject
#end
In your ViewController itself you can implement UIWebViewDelegate.
-(void)viewDidLoad
{
[super viewDidLoad];
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
webView.delegate = self;
NSError *error;
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:#"index" ofType:#"html"];
NSString *htmlContent= [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:&error];
[webView loadHTMLString:htmlContent baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]];
[self.view addSubview:webView];
}
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
return YES;
}
I'm posting a separate answer because I think there's a need to provide explicit Objective-C solution, since OP might miss David GONZALEZ's answer because it's in Swift, although he absolutely nailed it.
The problem is that by the time web view calls its delegate, that delegate has been deallocated. So the solution would be to add property of MyDelegate type to ViewController private extension declaration:
#interface ViewController ()
#property (nonatomic, strong) MyDelegate* webViewDelegate;
#end
And then to store MyDelegate instance created in -viewDidLoadin that property:
...
id <UIWebViewDelegate> delegate =[[MyDelegate alloc] init];
webView.delegate = delegate;
self. webViewDelegate = delegate;
...
I want to initialize a UIWebview object inside of my WebViewController. I'm having a bit of trouble initializing it, and directing it to the URL that is sent to it from the previous ViewController.
In the viewDidLoad of my WebViewController, I attempt to initialize it with the following:
UIWebView *tempWebview = [[UIWebView alloc]initWithFrame:theFrame];
NSURL *url = [NSURL URLWithString:_itemURL];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
self.myWebView = tempWebview;
[tempWebview loadRequest:urlRequest];
_myWebView.delegate=self;
[WebViewController loadRequest:urlRequest];
However, this gives me errors stating:
use of undeclared identifier'theFrame', and no known class method for selector loadRequest.
Full code is below:
WebViewController.h:
#import <UIKit/UIKit.h>
#import "MatchCenterViewController.h"
#interface WebViewController : UIViewController
#property (strong, nonatomic) NSURL *itemURL;
#property (weak, nonatomic) IBOutlet UIWebView *myWebView;
#end
WebViewController.m:
#import "WebViewController.h"
#interface WebViewController ()
#end
#implementation WebViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
UIWebView *tempWebview = [[UIWebView alloc]initWithFrame:theFrame];
NSURL *url = [NSURL URLWithString:_itemURL];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
self.myWebView = tempWebview;
[tempWebview loadRequest:urlRequest];
_myWebView.delegate=self;
[WebViewController loadRequest:urlRequest];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
#end
First you need to define that you are going to use the UIWebView Delegate in your .h file as:
#interface WebViewController : UIViewController <UIWebViewDelegate>
When you already have a webView defined in your Xib file, why are you creating another UIWebView and assigning it to the IBOutlet's webView? You could simply do:
self.myWebView.delegate = self; //Note that I have set the delegate first before calling LoadRequest
NSURL *url = [NSURL URLWithString:#"http://www.google.com"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self.myWebView setScalesPageToFit:YES];
[self.myWebView loadRequest:request];
Hi, I have a slight issue. I have tried all types of solutions I could find, minus the outdated codes, on this topic of getting a UIWebView link to pop open Safari and load it there.
So far I can get the specific size to load in simulator, but every time I click it, it loads right there. I have to be missing a major step or I have the AppDelegate .h .m and ViewController .h .m completely messed up.
I was big into coding for devices up to 3rd Gen iPod/iPhones. I know that Xcode likes to update a lot and I have the 5.0.2 version. I am basically a No0b again, since I have been out of the game for some time.
Please let me know if you have any tips. Besides to give it up. lol. I know it can be done. Here is what I have...
#import "WIAppDelegate.h"
#implementation WIAppDelegate
- (BOOL)webview:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request
navigationType:(UIWebViewNavigationType)navigationType {
// This practically disables web navigation from the webView.
if (navigationType == UIWebViewNavigationTypeLinkClicked) {
[[UIApplication sharedApplication] openURL:[request URL]];
return FALSE;
}
return TRUE;
}
#import <UIKit/UIKit.h>
#interface WIViewController : UIViewController
#property (strong, nonatomic) IBOutlet UIWebView *webview;
#end
#import "WIViewController.h"
#interface WIViewController ()
#end
#implementation WIViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSString *fullURL = #"http://THESITE.com";
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[_webview loadRequest:requestObj];
}
#end
You need to implement the webview:shouldStartLoadWithRequest:navigationType: method on the class that acts as the UIWebViewDelegate
This should most likely live in your WIViewController class
#implementation WIViewController
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *fullURL = #"http://THESITE.com";
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[_webview loadRequest:requestObj];
_webview.delegate = self;
}
- (BOOL)webview:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
if (UIWebViewNavigationTypeLinkClicked == navigationType) {
[[UIApplication sharedApplication] openURL:[request URL]];
return NO;
}
return YES;
}
#end
You will also need to ensure that you actually set this class as the UIWebViewDelegate I've down this as the last line of the viewDidLoad but you could hook this up in the xib if you prefer
I want to open links of uiwebview in to the safari browser my code is working perfectly if I implement shouldStartLoadWithRequest method in viewController but when I implement shouldStartLoadWithRequest in same class and set UIWebView's delegate to self it doesn't work it get halt in between and shows assembly level code with error EXC_BAD_ACCESS(code=2, address=0x9) my files are as follows
//content of ShowView.h file
#import <UIKit/UIKit.h>
#interface ShowView : UIView <UIWebViewDelegate> {
}
- (void) showViewFunction;
#property (nonatomic, assign) UIViewController *mainViewContObj;
#end
//content of ShowView.m file is :
#import "ShowView.h"
#implementation ShowView
- (void) showViewFunction {
UIWebView *aWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 150)];
aWebView.autoresizesSubviews = YES;
aWebView.autoresizingMask = (UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth);
[aWebView setDelegate:self];
NSString *urlAddress = #"http://localhost/test/index.php";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
aWebView.delegate = self;
[aWebView loadRequest:requestObj];
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
[[[self mainViewContObj] view] addSubview:aWebView];
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request
navigationType:(UIWebViewNavigationType)navigationType {
NSLog(#"In shouldStartLoadWithRequest method");
if ([[[request URL] absoluteString] isEqual:#"http://localhost/test/index.php"])
return YES;
[[UIApplication sharedApplication] openURL:[request URL]];
return NO;
}
#end
// Content of ViewController.h
#import "ViewController.h"
#import "ShowView.h"
#interface mnetViewController ()
#end
#implementation mnetViewController
- (void)viewDidLoad {
[super viewDidLoad];
MNETMobAd *bannerObj = [[MNETMobAd alloc] init];
bannerObj.mainViewContObj = self;
[bannerObj showAd];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
Some times html page shows but when I click on link it opens in same UIWebview window, and not even going into the shouldStartLoadWithRequest method, am I doing anything wrong?
Your code isn't clear, might need more info's, but from what i see, the ShowView class is never instanciated, so it shouldn't even show.
you should make something like this i guess :
//mnetViewController.m
#import "mnetViewController.h"
#import "ShowView.h"
#interface mnetViewController ()
#end
#implementation mnetViewController
- (void)viewDidLoad {
[super viewDidLoad];
ShowView* theShowView = [[ShowView alloc] initWithFrame:CGRectMake(insert the frame you want your webview to have)];
theShowView.autoresizesSubviews = YES;
[self.view addSubview:theShowView];
[theShowView release];
MNETMobAd *bannerObj = [[MNETMobAd alloc] init];
bannerObj.mainViewContObj = self;
[bannerObj showAd];
}
now for the ShowView class, try something like this :
//ShowView.h
#import <UIKit/UIKit.h>
#interface ShowView : UIView <UIWebViewDelegate> {
}
#end
//ShowView.m
#import "ShowView.h"
#implementation ShowView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
UIWebView *aWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
aWebView.scalesPageToFit = YES;
[aWebView setDelegate:self];
[self addSubview:aWebView];
NSString *urlAddress = #"http://localhost/test/index.php";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[aWebView loadRequest:requestObj];
[aWebView release];
}
return self;
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request
navigationType:(UIWebViewNavigationType)navigationType {
NSLog(#"In shouldStartLoadWithRequest method for URL : %#",[request [URL absolutString]]);
if ([[[request URL] absoluteString] isEqual:#"http://localhost/test/index.php"])
return YES;
[[UIApplication sharedApplication] openURL:[request URL]];
return NO;
}
This should work, i didn't try it, i'll comeback tomorrow to try it if necessary.
I recently started an Xcode project, and its mostly web-based, in the sense it mostly works with UIWebView. I am a total noob at this. I would like to make an "No Internet Connection Alert". Basically An Alert that comes up if you have no internet connection. I have tried using the Reachability but since I'm such a noob, I didn't manage to figure ANYTHING out.. Heres my view controller:
This is my .h file: P.S: My WebView is called 'webone'.
#import <UIKit/UIKit.h>
#interface FirstViewController : UIViewController
-(IBAction)refreshClicks:(id)sender;
#property (weak, nonatomic) IBOutlet UIWebView *webone;
#end
And This is My .m file:
#import "FirstViewController.h"
#interface FirstViewController ()
#end
#implementation FirstViewController
#synthesize webone;
-(void)viewDidLoad {
NSURL *url = [NSURL URLWithString:#"http://www.lostcraft.net/mobile"];
NSURLRequest *req = [NSURLRequest requestWithURL:url];
[webone loadRequest:req];
[super viewDidLoad];
}
-(void)awakeFromNib{ //IGNORE
[self refreshClicks:self]; //IGNORE
}
-(IBAction)refreshClicks:(id)sender{//IGNORE
[webone loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.lostcraft.net/mobile"]]];//IGNORE
}
- (void)viewDidUnload
{
[self setWebone:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
} else {
return YES;
}
}
Place this in your .m file
Under Your IBAction:
NSString *web = #"http://YOUR WEB ADRESS HERE";
NSURL *url = [NSURL URLWithString:web];
NSURLRequest *requestUrl = [NSURLRequest requestWithURL:url];
[webdone loadRequest:requestUrl];
Then somewhere in your .m
-(void)webView:(UIWebView *)webdone didFailLoadWithError:(NSError *)error {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Error" message:#"Please check your internet connection" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
}
go into connections tab in interface builder in xcode
Right click from the delegate as shown in the picture (the top option) and drag it to your view contoller where you have your web view (webdone)