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
Related
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];
}
}
I am making an app for iOS, use UIwebview to load my website in the app. I want all links (besides my website) open in Safari. Read many articles and tried many different suggestion still can't make it.
I am not familiar in coding, use Xcode 6.3.2
Here is my code of ViewController.m:
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *fullURL = #"http://www.mywebsite.com";
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[_viewWeb loadRequest:requestObj];
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
// Check if this was a click event and then some other criteria for determining if you want to launch Safari.
if (navigationType == UIWebViewNavigationTypeLinkClicked
&& [ [ request.URL scheme ] isEqualToString: #"http" ] ) {
[[UIApplication sharedApplication] openURL:request.URL];
// Return false to indicate to the UIWebView to not navigate to the linked target
return false;
}
// Return true so that the UIWebView loads the link target
return true;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
please help
Update your method
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
in something like:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
NSString *requestURL = [[request URL] absoluteString];
if ([requestURL rangeOfString:#"mydomain.com"].location != NSNotFound) {
[[UIApplication sharedApplication] openURL:[request URL]];
return NO;
}
return YES;
}
I have a UIViewController class named WebScreenViewController, which is having an UIWebView. Here is the class implementation file:
#implementation WebScreenViewController
#synthesize urlString;
- (void)viewDidLoad
{
[super viewDidLoad];
self.webView.delegate = self;
// Do any additional setup after loading the view.
[self gotoUrl:self.urlString];
//[self gotoUrl:#"http://www.google.com"];
}
#pragma mark - UIWebView Delegates
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSURL* url = [request URL];
NSString* urlStr = url.absoluteString;
NSLog(#"shouldStartLoadWithRequest. URLStr = %#", urlStr);
return YES;
}
-(void)webViewDidStartLoad:(UIWebView *)webView
{
NSLog(#"webViewDidStartLoad");
}
-(void)webViewDidFinishLoad:(UIWebView *)webView
{
NSString* html = [webView stringByEvaluatingJavaScriptFromString:#"document.body"];
NSLog(#"webViewDidFinishLoad: HTML string = %#", html);
}
-(void) webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
NSLog(#"didFailLoadWithError. ERROR: %#", error);
}
#pragma mark - Member Methods
-(void)gotoUrl:(NSString*)urlStr
{
NSLog(#"Calling website %# in UIWebView...", urlStr);
NSURL* url = [NSURL URLWithString:urlStr];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:urlRequest];
}
#end
The above code work well on one of my testing device but did not work on another. It worked on my iPod Touch with the latest iOS 8.1.2. But it didn't work on my iPad3 with iOS version 7.0. When I run it on my iPad3, it will call the gotoUrl: method and the log "Calling website %# in UIWebView..." will be displayed. And that's it. I receive no more logs from the delegates. To check the internet connections, I tried using Safari on the same device and it was working.
Check whether you've connected the web view's outlet correctly in iPad storyboard if you're using different storyboards for iPhone & iPad.
Also check that the web view is not nil.
Hope this helps..
NSString * strURL = [NSString stringWithFormat:#"http://www.google.com/"];
NSURL *loadURL=[NSURL URLWithString:strURL];
NSURLRequest *loadRequest = [NSURLRequest requestWithURL:loadURL];
[webView loadRequest:loadRequest];
webView.delegate=self;
Try this way in your view did load or if you have URL you can replace first line like this
NSString * strURL = [NSString stringWithFormat:self.urlString];
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)
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