I want to modify my code in order to open the external links on Safari instead of the in-app browser of my app. The html files are loaded from an internal path, so I don't know how to make the app understand that all the external links have to be opened in safari.
This is my ViewController.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSString *fullURL = [[NSBundle mainBundle] pathForResource:#"index" ofType:#"html"];
NSURL *url = [NSURL fileURLWithPath:fullURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[_viewWeb loadRequest:requestObj];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
How should I modify it?
Thanks!
You need to handle this in the UIWebView delegate method and check with the UIWebViewNavigationType.
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
if(navigationType == UIWebViewNavigationTypeLinkClicked){
[MAIN_APPLICATION_DELEGATE openURL:request.URL];
}
}
Related
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 am newer to coding in general! I am trying to create a webview that doesnt allow navigation to viewers. I have most of the coding typed up, but i have two undeclared errors and i am not sure how to solve this issue.
What i am trying to do is create a webView, but i would like to make it where the viewer of the webView cant navigate to other pages. If he/she does attempt too it does not fulfill the request.So basically it stays specifically on one page.
If i just need declare them in a really simple way then please show me and help. Thank you.
The Errors are shown below:
#import "ViewController.h"
#interface ViewController()
<UIWebViewDelegate>
#end
#implementation ViewController
#synthesize webView;
- (void)viewDidLoad
{
[super viewDidLoad];
webview:delegate = nil;
NSURL *url = [NSURL URLWithString:#"https://twitter.com/u_bett"];
NSURLRequest *urlrequest = [NSURLRequest requestWithURL:url];
[webView loadRequest:urlrequest];
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
return NO;
}
Your method contains lot of issues:
Change that like:
- (void)viewDidLoad
{
[super viewDidLoad];
webview.delegate = self;
NSURL *url = [NSURL URLWithString:#"https://twitter.com/u_bett"];
NSURLRequest *urlrequest = [NSURLRequest requestWithURL:url];
[webView loadRequest:urlrequest];
}
And implement shouldStartLoadWithRequest: like:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
if(navigationType == UIWebViewNavigationTypeLinkClicked)
{
return NO;
}
return YES;
}
Check this tutorial for UIWebView implementation.
I would like to suggest you to learn Objective C thoroughly before starting the development. (Some investment at this point will help you to save a lot of time in future)
Hey guys, i'm trying to make the buttons on my project to open a different webview url. I'm new to iOS programming, but i've used andriod programming. Is this possible? I've a ready created another webview view that sits in the supporting files folder.
Here is my code below
Viewcontroller.m
#import "ViewController.h"
#implementation ViewController
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
-(void)viewDidLoad
{
NSString *urlString = #"http://www.athletic-profile.com/Application";
//Create a URL object.
NSURL *url = [NSURL URLWithString:urlString];
//URL Requst Object
NSURLRequest *webRequest = [NSURLRequest requestWithURL:url];
//Load the request in the UIWebView.
[webView loadRequest:webRequest];
[super viewDidLoad];
// 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
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
#synthesize webView;
#end
Add a button to your View then give a connection to it. In the action method just write your code
-(IBAction*)yourButtonActionMethod:(id)sender
{
[UIWebView *aWebView =[[UIWebView alloc] initWithFrame:CGRectMake(x,y,width,height)];
aWebView.delegate=nil;
NSString *urlString = #"http://www.athletic-profile.com/Application";
//Create a URL object.
NSURL *url = [NSURL URLWithString:urlString];
//URL Requst Object
NSURLRequest *webRequest = [NSURLRequest requestWithURL:url];
//Load the request in the UIWebView.
[aWebView loadRequest:webRequest];
[self.view addSubView:aWebView];
}
Firstly you can drag & drop UIButton in First Class & create action of that Button then create a new class for UIWebView in which you want to open you url named your choice which is subclass of UIViewController then into your .Xib or Storyboard you just drag & drop the UIWebView then create outlet of UIWebView after you write this code in first class of that button's action :-
- (IBAction *) firstButton:(id) sender
{
NSString *urlString = #"http://www.athletic-profile.com/Application";
//Create a URL object.
NSURL *url = [NSURL URLWithString:urlString];
//URL Request Object
NSURLRequest *webRequest = [NSURLRequest requestWithURL:url];
//Load the request in the UIWebView.
[webView loadRequest:webRequest];
}
So am very new to xCode and it would help if someone could help me with this!
I'm creating an app that is very simple for the most part. I have a UIWebView taking me to a mobile page. This page has a log-in for Facebook. The original problem I've been having, is that since it's a mobile site I am getting a blank screen after the login is complete. I need the UIWebView to take me back to the original sign I clicked log-in at. I have copied some code which I think will work, but I I'm getting at error that says
"No visible #interface for 'forum' declares the selector 'backToLastPage'"
Could someone please tell me what I need to do to fix this problem? It's probably something simple but I need some help.
#import "forum.h"
#import "ViewController.h"
#interface forum ()
#end
#implementation forum
-(IBAction)switchback:(id)sender {
[self dismissViewControllerAnimated:YES completion:nil];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSURL *myURL = [NSURL URLWithString:#"http://www.moot.it/yopyipcommunity"];
NSURLRequest *myRequest = [NSURLRequest requestWithURL:myURL];
[myWebView loadRequest:myRequest];
}
- (BOOL)webView:(UIWebView *)webView
shouldStartLoadWithRequest:(NSURLRequest *)request
navigationType:(UIWebViewNavigationType)navigationType
{
NSString *url = [[request URL] absoluteString];
//when user status == connected
//(has a access_token at facebook oauth response)
if([url hasPrefix:#"https://m.facebook.com/dialog/oauth"] &&
[url rangeOfString:#"access_token="].location != NSNotFound)
{
[self backToLastPage];
return NO;
}
return YES;
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
NSString *url = [[webView.request URL] absoluteString];
if([url hasPrefix:#"https://m.facebook.com/dialog/oauth"])
{
NSString *bodyHTML =
[webView stringByEvaluatingJavaScriptFromString:#"document.body.innerHTML"];
//Facebook oauth response dead end:
// is a blank body and a head with a script that does
//nothing. But if you got back to your last page,
// the handler of authResponseChange
//will catch a connected status
// if user did his login and auth app
if([bodyHTML isEqualToString:#""])
{
[self backToLastPage];
}
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
I think forum.h is separate class, backToLastPage method is declared in that class.
#property (nonatomic, strong) forum *forum;
You need to create allocation for that class,
self.forum = [forum alloc]init];
and you call the method like this, [self.forum backToLastPage];
instead of this, [self backToLastPage];
I am building a tab bar application with a different pdf in each tab. I have two sets of view controllers. Each have this relative code
#import "SecondViewController.h"
#interface SecondViewController ()
#end
#implementation SecondViewController
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *path = [[NSBundle mainBundle] pathForResource:#"Winter_Newsletter"
ofType:#"pdf"];
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
[webview setScalesPageToFit:YES];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
I keep receiving an error "undeclared identifier 'web view'" What am I doing Wrong?
You are showing in the code two versions of webView(capital V) and webview(small v)
[webView loadRequest:request];
[webview setScalesPageToFit:YES];
And if you are using auto-synthesize then it sould be
self.webView or _webView
And also, don't forget to make a property of the web view and control drag to hook it up!