I'm trying to implement a UIWebViewDelegate in my application and I cannot for the life of me get it to work. I would really appreciate a second set of eyes on this.
I know the name MapViewController is confusing, but the WebView is controlling a map (not UIMapView).
Here's the bulk of the code:
MapViewController.h
#import <UIKit/UIKit.h>
#import <UIKit/UIWebView.h>
#interface MapViewController : UIViewController<UIWebViewDelegate> {
IBOutlet UIWebView *webView;
}
#property (nonatomic, retain) IBOutlet UIWebView *webView;
#end
MapViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
webView.delegate = self;
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"map" ofType:#"html"]isDirectory:NO]];
[webView loadRequest:request];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView{
NSLog(#"Done loading.");
}
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
NSLog(#"hi");
return NO;
}
- (void)dealloc {
webView.delegate = nil;
[webView release]
[super dealloc];
}
Thanks for any help!
Does the initial page you're loading actually display? Are you sure you connected your webview in Interface Builder?
go interface builder , connect the delegate to the file's owner , then it should work.
Open Interface Builder
Right click on the Web View object
Drag the delegate onto files owner.
Things to check:
if map.html is in the bundle. Put it on the root of the bundle, not on resources.
if it is, right click on it, and choose get info. Verify on TARGETS if the target is checked for the file.
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
NSLog(#"hi");
return YES; // for all supported oreintation.
}
Related
I have been programming an Web browser app for iOS and stumbled upon an issue. At first my search text field and web view where in the same view (Everything was fine :). When I put the web view in an different view the web view wouldn't load up the page and stay blank. So the issue is the web view will not load in a different view (will work if text field and web view are in the same view).
My Code:
#import "ViewController.h"
#import <QuartzCore/QuartzCore.h>
#interface ViewController ()
#end
#implementation ViewController
#synthesize searchButton;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib
}
-(IBAction)SearchButton:(id)sender {
NSString *query = [searchField.text stringByReplacingOccurrencesOfString:#" " withString:#"+"];
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:#"http://www.google.com/search?q=%#", query]];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
ViewController *WebView = [self.storyboard instantiateViewControllerWithIdentifier:#"WebView"];
[self presentViewController:WebView animated:YES completion:nil];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.view endEditing:YES];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
The SearchButton method refers to a webView in the current class, but it's the new ViewController (you call it WebView) which is about to be presented and that should contain a UIWebView. So the UIWebView on the presented view controller is never getting to loadRequest.
Create a subclass of UIViewController to contain your web view. (call it something like MyWebViewController) It should have a property called urlString. Make sure to change the class of the view controller you currently have painted in storyboard to MyWebViewController.
The SearchButton method should look like this:
// renamed to follow convention
- (IBAction)pressedSearchButton:(id)sender {
NSString *query = [searchField.text stringByReplacingOccurrencesOfString:#" " withString:#"+"];
NSString *urlString = [NSString stringWithFormat:#"http://www.google.com/search?q=%#", query];
// remember to change the view controller class in storyboard
MyWebViewController *webViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"WebView"];
// urlString is a public property on MyWebViewController
webViewController.urlString = urlString;
[self presentViewController:webViewController animated:YES completion:nil];
}
The new class can form the request from the urlString...
// MyWebViewController.h
#property (nonatomic, strong) NSString *urlString;
#property (nonatomic, weak) IBOutlet UIWebView *webView; // be sure to attach in storyboard
// MyWebViewController.m
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
NSURL *url = [NSURL URLWithString:self.urlString];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:request];
}
I'd like to create an iPhone App that allows people to listen to radio. Below is what I've done so far, but when I click on the button nothing happen, can someone please advise what I've done wrong here.
Thanks in advance.
#import <UIKit/UIKit.h>
#interface RadioViewController : UIViewController{
IBOutlet UIWebView *webView;
}
-(IBAction)radio;
#property(nonatomic,retain)UIWebView *webview;
#end
#import "RadioViewController.h"
#import <MediaPlayer/MediaPlayer.h>
#implementation RadioViewController
#synthesize webview;
-(IBAction)radio{
NSURL *url = [NSURL URLWithString:#"http://db5195728.tis.core.005.cdn.streamfarm.net:80/3212_erf_96_live.mp3"];
NSURLRequest *req = [NSURLRequest requestWithURL: url];
[webview loadRequest:req];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
I've tried your code. It works fine here. So:
Make sure your radio is called when you tap that button. You can set a break point there or add a NSLog to see whether it is called;
Make sure your webview is correctly add to your view:shown and of the correct size.
i open a view in AppDelegate
with:
AppDelegate
PushDetail *pushdtls = [[PushDetail alloc] initWithNibName:nil bundle:nil];
pushdtls.seminarurl = [NSURL URLWithString:resourcePathURL]; /passing URL
[self.window presentModalViewController:pushdtls animated:YES];
PushDetail.h
#property (nonatomic) NSURL *seminarurl;
PushDetail.m
- (void)viewDidLoad
{
NSURLRequest *requestObj = [NSURLRequest requestWithURL:seminarurl];
[pushDetails loadRequest:requestObj];
}
But the webview is empty... what I do wrong?
and the second question how to close the view I opened?
- (IBAction) closeNews{
//[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
//[self release];
[self dismissModalViewControllerAnimated:YES];
}
does not work :(
You may implement the UIWebViewDelegate protocol within your view controller and fetch the results/error messages yourself.
Namely didFailLoadWithError - see the UIWebViewDelegate Protocol References for more.
Maybe its just a typo on your URL.
Example:
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
NSLog(#"oops, failed with error: %#", error);
}
You've not specified what pushDetails is, but I'm assuming it's an IBOutlet to a UIWebview on your PushDetail controller. My guess is that you've forgotten to bind your pushDetails outlet to the webview in your nib file.
You could create lets say a file that holds all your global variables lets call it GlobalVariables.h and .m. In the GlobalVariables.h file add this
extern NSURL *seminarurl;
#interface GlobalVariables
#property (retain, nonatomic) NSURL *seminarurl;
#end
and in the GlobalVariables.m file add
#import "GlobalVariables.h"
#implements GlobalVariables
#synthesize seminarurl;
NSURL *seminarurl; // You could add what your URL is here as well you would just use '='
#end
So when you want to access it or assign a variable to it, it would just be like accessing or assigning any other variable. Just remember to import 'GlobalVariables.h' into which ever .m file you are using it in.
make your property retained. ex >> #property (nonatomic,retain).
if the url is a property in your app delegate .. you can access it like this
[UIApplication SharedApplication].delegate.yourpropertyname;
I am working on an app that receives a feed that I display in a UIWebView. The feed has embedded links that I want to open in Safari instead of thew WebView. I have looked over several other questions that are posted here. I am not sure what I am missing, but I think it is something simple. Here are my .h and .m files
#import
#class BlogRss;
#interface EnduranceDailyWorkoutViewController : UIViewController {
IBOutlet UIWebView * descriptionTextView;
BlogRss * currentlySelectedBlogItem;
}
#property (nonatomic, retain) UIWebView * descriptionTextView;
#property (readwrite, retain) BlogRss * currentlySelectedBlogItem;
#end
#import "EnduranceDailyWorkoutViewController.h"
#import "BlogRss.h"
#implementation EnduranceDailyWorkoutViewController
#synthesize descriptionTextView = descriptionTextView;
#synthesize currentlySelectedBlogItem = currentlySelectedBlogItem;
- (void)viewDidLoad {
[super viewDidLoad];
NSString *html = [NSString stringWithFormat:#"%# %#",currentlySelectedBlogItem.title, currentlySelectedBlogItem.contentEncoded];
[descriptionTextView loadHTMLString:html baseURL:[NSURL URLWithString:nil]];
}
-(BOOL)webView:(UIWebView *)descriptionTextView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
if (UIWebViewNavigationTypeLinkClicked == navigationType) {
[[UIApplication sharedApplication] openURL:[request URL]];
return NO;
}
return YES;
}
Using Interface Builder I have linked the IBOutlet and the UIWebView. Please let me know what I am missing. I have put break points in the webView section but the code never hits there so it is almost like something is not linked correctly in IB.
You need to ensure that the UIWebView's delegate is set to your controller. You can do this in interface builder, or you can modify your viewDidLoad method:
- (void)viewDidLoad {
[super viewDidLoad];
// add this line to set the delegate of the UIWebView
descriptionTextView.delegate = self;
NSString *html = [NSString stringWithFormat:#"%# %#",currentlySelectedBlogItem.title, currentlySelectedBlogItem.contentEncoded];
[descriptionTextView loadHTMLString:html baseURL:[NSURL URLWithString:nil]];
}
i know this is a very common question, i read most of the questions asked in stackoverflow but still couldn't figure out how to remove the uiactivityindicatorview from the view.
please find below the code
#implementation FeedBackViewController
#synthesize m_activity,webView;
-(void)viewDidLoad{
[super viewDidLoad];
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://equinox.library.pitt.edu/limesurvey/index.php?sid=87435&lang=en"]]];
}
- (void)dealloc {
[m_activity release];
[webView release];
[super dealloc];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
[m_activity stopAnimating];
[m_activity removeFromSuperview];
m_activity = nil;
}
- (void)webViewDidStartLoad:(UIWebView *)webView {
NSLog(#"in webViewDidFinishLoad")
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
m_activity.center = self.view.center;
[self.view addSubview: m_activity];
[m_activity startAnimating];
}
the header file
#interface FeedBackViewController : UIViewController<UIWebViewDelegate>{
IBOutlet UIWebView *webView;
IBOutlet UIActivityIndicatorView *m_activity;
}
#property (nonatomic, retain) IBOutlet UIWebView *webView;
#property (nonatomic, retain) UIActivityIndicatorView *m_activity;
Also, i have checked the box in the IB that says, hide when stopped. I put NSLog(#"in webViewDidFinishLoad") in the webViewFinishLoad method, seems like it does not hit that method for some reason.
and for
NSLog(#"m_activity = %#",m_activity);
in the viewDidLoad method, it gives a message as below in the console
> m_activity = <UIActivityIndicatorView:
> 0x4d3d920; frame = (150 79; 20 20);
> hidden = YES; opaque = NO; autoresize
> = RM+BM; layer = <CALayer: 0x4d35ce0>>
Your webViewDidFinishLoad function is probably not being called. It may happen if the web view fails to open the url. In any case, you should also implement webView:didFailLoadWithError: method and stop the activity indicator there too.
-edit-
To receive events from UIWebView you need to set the controller as the UIWebView's delegate. Just implementing the UIWebViewDelegate is not sufficient. You can do this by updating viewDidLoad method as follows:
-(void)viewDidLoad{
[super viewDidLoad];
webView.delegate = self;
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://equinox.library.pitt.edu/limesurvey/index.php?sid=87435&lang=en"]]];
}
You should add NSLog(#"in webViewDidFinishLoad") lines to your code and watch the console to see if the code hits those points. Since this is a very simple example, perhaps m_activity is not connected to the actual object? Try to see if you get an address or a null when you add this to the end of your viewDidLoad:
NSLog(#"m_activity = %#",m_activity);