Iphone WebView links won't open in Safari - iphone

I have followed many tutorials and my links in my web view are still not opening up in safari.
I am a noob, this is my first IPhone App, so I hope I'm just missing something simple.
//
// ViewController.m
// FirstIphoneApp
//
// Created by admin on 12/2/11.
// Copyright (c) 2011 __MyCompanyName__. All rights reserved.
//
#import "ViewController.h"
#implementation ViewController
#synthesize webView;
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)navigationType
{
if (navigationType == UIWebViewNavigationTypeLinkClicked)
{
[[UIApplication sharedApplication] openURL:[inRequest URL]];
return NO;
}
return YES;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://somewebsite.com/iphone.html"]]];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
} else {
return YES;
}
}
#end
And here is my Header File
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController<UIWebViewDelegate,UIAlertViewDelegate>{
IBOutlet UIWebView *webView;
}
#property(nonatomic, retain) IBOutlet UIWebView *webView;
#end

Does the shouldStartLoadWithRequest method fire? It may be that you haven't connected the delegate in your nib file. Usually this will be connected to your view controller, or whatever file the shouldStartLoadWithRequest method is found in.

Related

Application tried to push a nil view controller on target <UINavigationController

In my Iphone app I have this pb :Application tried to push a nil view controller on target <UINavigationController:
I have a view controller that contains a tableview and I want that if one row is selected an other view controller to be displayed.
Here is my code :
in RecherchePartenaireViewController.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row==3){
NSLog(#"RecherchePartenaireDistanceViewController...");
recherchePartenaireDistanceViewController = [[RecherchePartenaireDistanceViewController alloc] init];
self.recherchePartenaireDistanceViewController=recherchePartenaireDistanceViewController.recherchePartenaireViewController;
[self.navigationController pushViewController:recherchePartenaireDistanceViewController animated:YES];
[recherchePartenaireDistanceViewController release];
}
}
In RecherchePartenaireDistanceViewController.h :
#class RecherchePartenaireViewController;
#interface RecherchePartenaireDistanceViewController : UIViewController {
RecherchePartenaireViewController *recherchePartenaireViewController;
}
#property (nonatomic, retain) IBOutlet RecherchePartenaireViewController *recherchePartenaireViewController;
#end
and in RecherchePartenaireDistanceViewController.m
#import "RecherchePartenaireDistanceViewController.h"
#import "RecherchePartenaireViewController.h"
#implementation RecherchePartenaireDistanceViewController
#synthesize recherchePartenaireViewController;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)dealloc
{
[super dealloc];
[recherchePartenaireViewController release];
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[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 from its nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#end
Where is the problem? I really can't see it. Please heeelp....
Try to use this:
- (void)dealloc
{
[recherchePartenaireViewController release];
[super dealloc];
}
instead of
- (void)dealloc
{
[super dealloc];
[recherchePartenaireViewController release];
}
I think I see what you're doing but I don't understand what you're trying to achieve! You have a ViewController that contains another view controller?
i.e. RecherchePartenaireDistanceViewController contains a RecherchePartenaireViewController
I assume that as it's an IBOutlet you're populating it using a xib file.
The only thing I can think of is that something is going wrong with the creation of your RecherchePartenaireDistanceViewController in the xib file.
What do you see in the console if you do this :
NSLog(#"controller : %#", recherchePartenaireDistanceViewController);
just before adding it to the navigation controller?
PS
What's the purpose of this line?
self.recherchePartenaireDistanceViewController=recherchePartenaireDistanceViewController.recherchePartenaireViewController;
if RecherchePartenaireDistanceViewController is with xib interface than use following statement while initializing,
recherchePartenaireDistanceViewController = [[RecherchePartenaireDistanceViewController alloc] initWithNibName:#"RecherchePartenaireDistanceViewController" bundle:nil];
than push it to navigation controller.

Showing ads on every view automatically in iPhone

I'm implementing Mobclix in my app, and when following the instructions, it tells us to properly release and dealloc the adview. It also tells us that when the view appears, you should call refreshAds and when it disappears you should pause refresh ads:
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self.adView resumeAdAutoRefresh];
}

- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[self.adView pauseAdAutoRefresh];
}
#implementation RootViewController
- (void)viewDidUnload {
[self.adView cancelAd];
self.adView.delegate = nil;
self.adView = nil;
}
- (void)dealloc {
[self.adView cancelAd];
self.adView.delegate = nil;
self.adView = nil;
[super dealloc];
}
#end
My question is that I have a lot of view controllers, so it would be inconvenient to implement these things on each view. Is there anyway I can call these methods in one place and have it work for all views?
What you can do is subclass UIViewController and have all your viewControllers to inherit these things.
So basically you could have AdsViewController.h wich will look like
#import <Foundation/Foundation.h?
#interface AdsViewController: UIViewController {
}
#property (nonatomic, retain) <#AdsViewType#> *adsView;
#end
and your AdsViewController.m will have to look pretty much like this
#import "AdsViewController.h"
#implementation AdsViewController
#synthesize adsView = _adsView;
- (id)init {
self = [super init];
if (self) {
_adsView = [[<#AdsViewType#> alloc] init];
_adsView.delegate = self;
}
return self;
}
- (void)viewDidUnload {
[self.adView cancelAd];
self.adView = nil;
[super viewDidUnload];
}
- (void)dealloc {
[self.adView cancelAd];
self.adsView = nil;
[super dealloc];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self.adView resumeAdAutoRefresh];
}

- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[self.adView pauseAdAutoRefresh];
}
#end
After this have all your viewControllers to subclass this instead of UIViewController. Hope this helps.

Dismiss popover using UIbutton

I can't figure out why it doesn't work as it should when I try to dismiss a popover by clicking on a UIButton which itself is on a the popover to be dismissed, my project crashes...
- (IBAction) cancelButton: (id) sender{
//[self dismissPopoverAnimated:YES];
}
Above is my code for my UIButton
Dont dismiss the popover from within its self. Make a protocol that sends a message to its delegate to then dismiss it. For example, your popover view controller might look like this..
// MyPopoverViewController.h
#protocol MyPopoverDelegate <NSObject>
-(void)didClickCancelButton;
#end
#interface MyPopoverViewController : UIViewController {
}
#property (nonatomic, assign) id<MyPopoverDelegate> delegate;
-(IBAction)cancelButton;
#end
// MyPopoverViewController.m
#import "MyPopoverViewController.h"
#implementation MyPopoverViewController
#synthesize delegate;
// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
}
*/
-(IBAction)cancelButton {
[self.delegate didClickCancelButton];
}
#pragma mark -
#pragma mark Rotation support
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Overriden to allow any orientation.
return YES;
}
#pragma mark -
#pragma mark Memory Management
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc. that aren't in use.
}
- (void)viewDidUnload {
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
}
#end
And then you could use..
// ClassImplementingPopoverController.h
#import <UIKit/UIKit.h>
#import "MyPopoverViewController.h"
#interface ClassImplementingPopoverController : UIViewController <UIPopoverControllerDelegate, MyPopoverDelegate> {
UIPopoverController *myPopoverController;
}
#property (nonatomic, retain) UIPopoverController *myPopoverController;
#end
// ClassImplementingPopoverController.m
#import "ClassImplementingPopoverController.h"
#import "MyPopoverViewController.h"
#implementation ClassImplementingPopoverController
#synthesize myPopoverController;
#pragma mark -
#pragma mark View lifecycle
- (void)viewDidLoad {
[super viewDidLoad];
}
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations.
return YES;
}
#pragma mark -
#pragma mark MyPopover delegate
-(void)didClickCancelButton {
if ([myPopoverController isPopoverVisible]) {
[myPopoverController dismissPopoverAnimated:YES];
[myPopoverController release];
}
}
#pragma mark -
#pragma mark UIPopoverController delegate
-(void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController {
if (popoverController == myPopoverController) {
[myPopoverController release];
}
}
/* Use something like this to create your popover, just make sure you set the delegate to self so you can receive the messages
NSLog(#"Displaying Popover!");
MyPopoverViewController *detailViewController = [[MyPopoverViewController alloc] initWithNibName:#"MyPopoverViewController" bundle:nil];
[detailViewController setDelegate:self];
// Pass the selected object to the new view controller.
myPopoverController = [[UIPopoverController alloc] initWithContentViewController:detailViewController];
[detailViewController release];
myPopoverController.popoverContentSize = CGSizeMake(500.0, 150.0);
[myPopoverController setDelegate:self];
*/
#pragma mark -
#pragma mark Memory management
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Relinquish ownership any cached data, images, etc. that aren't in use.
}
- (void)viewDidUnload {
// Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
// For example: self.myOutlet = nil;
self.myPopoverController = nil;
}
- (void)dealloc {
[myPopoverController release];
[super dealloc];
}
#end
you can use KVC accessing "popoverController" like
[self valueForKey:#"popoverController"]
But it is potentially rejected by AppStore if they detect your code is using their's "private API".
dismissPopoverAnimated is a property on the PopoverController itself, not your custom VC. You are showing an IBAction on 'self', which suggests you are inside your own class here. (Please post more surrounding code if that isn't correct.)
A VC can't get to its enclosing popover directly; you just need to keep a reference to it in the VC that created it so you can hit it with dismiss from there.
You can dismiss a Popover from inside itself, that doesn't crash.

UIImageView subclass with CGRectIntersectsRect - help!

The title of this question should be pretty self explanatory. I am making an app that involves multiple UIImageViews that serve the same purpose. They are merely different sizes. Anyway, I decided that the best solution was to make UIImageView subclasses, link the subcalsses in IB, and work from there. My code should explain this better -
#define kPausedStatePaused 1
#define kPausedStatePlay 2
#import "Game.h"
#import "ScoreSystem.h"
#interface Doctor : UIImageView
{
}
#end
#interface Ground : UIImageView
{
}
#end
#interface Wall : UIImageView
{
}
#end
#interface Electric_Wire : UIImageView
{
}
#end
#implementation Game
/*
// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
// Custom initialization
}
return self;
}
*/
/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
}
*/
- (IBAction)pause {
UIAlertView *pause = [[UIAlertView alloc] initWithTitle:#"Pause" message:nil delegate:self cancelButtonTitle:#"Quit" otherButtonTitles:#"Play", nil];
[pause show];
[pause release];
pauseint = kPausedStatePaused;
}
- (void)viewDidAppear {
pauseint = kPausedStatePlay;
}
- (void)loop {
Doctor *doctorview;
Ground *groundview;
if (CGRectIntersectsRect(doctorview, groundview)) {
}
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if ([alertView.title isEqual:#"Pause"]) {
if(buttonIndex == 0)
[self dismissModalViewControllerAnimated:YES];
pauseint = kPausedStatePlay;
}
}
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
}
#end
Unsurprisingly, Xcode gave me an "incompatible type for CGRectIntersectsRect" error.
You must pass view's frame, not the view's themselves to that function:
CGRectIntersectsRect(doctorview.frame, groundview.frame)

Save UIwebview contents to photo gallery

There's a video tutorial on u tube that shows how to perform this.It consists of a UIwebview and toolbar button to save the contents.Haven't had any luck making this work.Could someone have a look and see they can make it work.Many thanks in advance.
http://www.youtube.com/watch?v=gDPca3JIc_s&feature=player_embedded#
///////////////////////////////////////////////////////////////////
//
// SaveWebViewController.h
// SaveWeb
//
//
// Copyright __MyCompanyName__ 2010. All rights reserved.
//
#import <UIKit/UIKit.h>
#interface SaveWebViewController : UIViewController {
IBOutlet UIWebView *webview;
}
#property (nonatomic, retain) IBOutlet UIWebView *webview;
- [IBAction]saveWeb:(id)sender;
#end
////////////////////////////////////////////////////////////////////////////////
//
// SaveWebViewController.m
// SaveWeb
//
//
// Copyright __MyCompanyName__ 2010. All rights reserved.
//
#import "SaveWebViewController.h"
#implementation SaveWebViewController
- (IBAction)saveWeb:(id)sender {
UIGraphicsBeginImageContext(webView.frame.size);
[self.view.layer renderInContext: UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil);
}
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
// Custom initialization
}
return self;
}
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
//Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://google.com"]]];
}
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
}
#end
-(IBAction)saveWeb:(id)sender
{
UIGraphicsBeginImageContext(webview.frame.size);
[self.webview.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage =UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(viewImage, nil,nil,nil);
} ////make sure u add the quartzcore frame work to get rid of the renderincontext problem flag