ios app hits straight shouldstartloadwithrequest after coming from background instead - ios

I have a webview in my mainviewcontroller and i am laoding a webpage in viewdidload method as below:
- (void)viewDidLoad
{
[super viewDidLoad];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[_loginWebView loadRequest:requestObj];
}
and in the shouldstartloadwithrequest method, i check if the url contains "itunes" and i have the following code for it:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
NSRange textRange4;
textRange4 =[[[request URL] absoluteString] rangeOfString:#"itunes.apple.com"];
if(textRange4.location != NSNotFound)
{
UIApplication *app = [UIApplication sharedApplication];
NSString *newPath = [[request URL] absoluteString] ;
if ([newPath rangeOfString:#"insider"].location != NSNotFound) {
NSURL *myURL = [NSURL URLWithString:#"insider://"];
if ([app canOpenURL:myURL]) {
[app openURL:myURL];
NSLog(#"insider");
}
else
{
[app openURL:[NSURL URLWithString:newPath]];
}
}
else if ([newPath rangeOfString:#"reuters-news-pro-for-ipad"].location != NSNotFound) {
[app openURL:[NSURL URLWithString:newPath]];
}
else if ([newPath rangeOfString:#"thomson-reuters-marketboard"].location != NSNotFound) {
NSURL *myURL = [NSURL URLWithString:#"marketboard://"];
if ([app canOpenURL:myURL]) {
[app openURL:myURL];
NSLog(#"marketboard");
}
else
{
[app openURL:[NSURL URLWithString:newPath]];
}
}
else
{
[app openURL:[NSURL URLWithString:newPath]];
}
return NO;
}
return YES;
}
the above code works it opens the apps i desire but when I go back to my app from ipad, instead of going to the mainviewcontroller, it reopens the previous opened app.
for example if I opened marketboard app, it reopens it when i tap the app icon from ipad home.
But the above only happens in ios5.0, it does not happen in 6.0 which is really weird

It is the default behaviour when you have webview loadrequest in viewdidload method in ios5.0
and they have fixed it in ios6.0

Related

iOS 12.2 update stopped WebView from opening links in new browser window

The recent 12.2 update stopped some functionality in our WebView from working, strangely only on real devices--it works fine in simulators. When a link is clicked in the webview, some code determines whether the link should open in the webview, or open up a new browser window. The links are standard href links with target="_blank". The code to determine whether or not to open a new browser window looks like this:
#pragma UIWebView delegate
- (BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType
{
BOOL shouldOpenBrowser = NO;
if ((inRequest.URL.absoluteString.length > 0) && (![inRequest.URL.absoluteString isEqualToString:#"about:blank"])) {
self.strUrl = [NSString stringWithFormat:#"%#", [inRequest URL]];
if ([inRequest.URL.absoluteString containsString:#"google.com/recaptcha"]) {
shouldOpenBrowser = NO;
} else if (![inRequest.URL.host hasSuffix:#"mywebviewdomain.com"]) {
shouldOpenBrowser = YES;
} else {
NSString *strMatchedUrl = [NSString stringWithFormat:#"https://mywebviewdomain.com/link/%#/%#/%#",self.var1, self.var2,self.var3];
if (![inRequest.URL.absoluteString containsString:strMatchedUrl]) {
shouldOpenBrowser = NO;
} else {
shouldOpenBrowser = YES;
}
}
}else if([inRequest.URL.absoluteString hasPrefix:#"newtab:"])
{
NSURL *url = [NSURL URLWithString:[inRequest.URL.absoluteString substringFromIndex:7]];
[[UIApplication sharedApplication] openURL:url];
return true;
}
if (shouldOpenBrowser == YES) {
[[UIApplication sharedApplication] openURL:[inRequest URL]];
return NO;
}
return YES;
}
As you can see, all links that don't go to mywebviewdomain.com should open a new browser window. On real devices, nothing happens when the links are clicked. It works fine in iOS 12.2 simulators.
What's the problem here? And is there any solution I can do in the webpage, to avoid having to do an app update?

SFSafariViewController does not reopen a webview inline URL link

I have a Webview with inline URL links which are opened with SFSafariViewController, as shown below:
-(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType {
if ( inType == UIWebViewNavigationTypeLinkClicked ) {
if ([SFSafariViewController class] != nil) {
NSString *inR = [[inRequest URL] absoluteString];
NSURL *inReq = [NSURL URLWithString:inR];
SFSafariViewController *safariVC = [[SFSafariViewController alloc] initWithURL:inReq entersReaderIfAvailable:YES];
safariVC.delegate = self;
[self presentViewController:safariVC animated:YES completion:nil];
} else {
[[UIApplication sharedApplication] openURL:[inRequest URL]];
return NO;
}
}
return YES;
}
#pragma mark - SFSafariViewController delegate methods
-(void)safariViewController:(SFSafariViewController *)controller didCompleteInitialLoad:(BOOL)didLoadSuccessfully {
// Load finished
}
-(void)safariViewControllerDidFinish:(SFSafariViewController *)controller {
// Done button pressed
NSLog(#"DONE PRESSED!!!");
}
When I press the DONE button correctly returns to my Webview. The problem is that if I will press again on the same inline link, it does not open with SFSafariViewController but in Webview which is not what i desire. I tried to force Webview reload in safariViewControllerDidFinish but without success.
Could you please help? Thanks!
The code corrected as below (following proposal of beyowulf) and now is working OK:
-(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType {
if ( inType == UIWebViewNavigationTypeLinkClicked ) {
if ([SFSafariViewController class] != nil) {
NSString *inR = [[inRequest URL] absoluteString];
NSURL *inReq = [NSURL URLWithString:inR];
SFSafariViewController *safariVC = [[SFSafariViewController alloc] initWithURL:inReq entersReaderIfAvailable:YES];
safariVC.delegate = self;
[self presentViewController:safariVC animated:YES completion:nil];
return NO;
} else {
[[UIApplication sharedApplication] openURL:[inRequest URL]];
return NO;
}
} else {
return YES;
}
}
#pragma mark - SFSafariViewController delegate methods
-(void)safariViewController:(SFSafariViewController *)controller didCompleteInitialLoad:(BOOL)didLoadSuccessfully {
// Load finished
}
-(void)safariViewControllerDidFinish:(SFSafariViewController *)controller {
// Done button pressed
NSLog(#"DONE PRESSED!!!");
}

Open safari view controller from table view on iOS 9 and open in safari on iOS 8 or 7

Hi I would like to open my website from my table view cell in the safari view controller if the user is on iOS 9 or above. If the user is on iOS 7 or 8 the website should open up in the standard safari app.
This is the code I currently use which opens safari.
case 3: { // Follow us section
switch (indexPath.row) {
case 0: { //Website
NSURL *url = [NSURL URLWithString:#"http://www.scanmarksapp.com"];
if (![[UIApplication sharedApplication] openURL:url]) {
NSLog(#"%#%#",#"Failed to open url:",[url description]);
}
}
break;
default:
break;
}
}
break;
I believe this code should open the safari view controller with my website. But I am unsure how to combine both sets of code.
- (void)openLink:(NSString *)url {
NSURL *URL = [NSURL URLWithString:[NSString stringWithFormat:#"http://www.scanmarksapp.com", url]];
if (URL) {
SFSafariViewController *sfvc = [[SFSafariViewController alloc] initWithURL:URL];
sfvc.delegate = self;
[self presentViewController:sfvc animated:YES completion:nil];
}
#pragma Safari View Controller Delegate
- (void)safariViewControllerDidFinish:(nonnull SFSafariViewController *)controller {
[controller dismissViewControllerAnimated:YES completion:nil];
}
I understand this is the code used to determine what iOS version it is
if ([[[UIDevice currentDevice] systemVersion] floatValue] < 9.0) {
I have followed your advice
- (void)openLink:(NSString *)url {
NSURL *URL = [NSURL URLWithString:[NSString stringWithFormat:#"http://www.scanmarksapp.com", url]];
if (URL) {
SFSafariViewController *sfvc = [[SFSafariViewController alloc] initWithURL:URL];
sfvc.delegate = self;
[self presentViewController:sfvc animated:YES completion:nil];
} else {
// will have a nice alert displaying soon.
}
if ([SFSafariViewController class] != nil) {
// Use SFSafariViewController
} else {
NSURL *url = [NSURL URLWithString:#"http://www.scanmarksapp.com"];
if (![[UIApplication sharedApplication] openURL:url]) {
NSLog(#"%#%#",#"Failed to open url:",[url description]);
}
}
Then added this code under my table view cell didSelectRowAtIndexPath
case 3: { // Follow us section
switch (indexPath.row) {
case 0: { //Website
NSURL *URL = [NSURL URLWithString:[NSString stringWithFormat:#"http://www.scanmarksapp.com", url]];
if (URL) {
SFSafariViewController *sfvc = [[SFSafariViewController alloc] initWithURL:URL];
sfvc.delegate = self;
[self presentViewController:sfvc animated:YES completion:nil];
} else {
// will have a nice alert displaying soon.
}
if ([SFSafariViewController class] != nil) {
// Use SFSafariViewController
} else {
NSURL *url = [NSURL URLWithString:#"http://www.scanmarksapp.com"];
if (![[UIApplication sharedApplication] openURL:url]) {
NSLog(#"%#%#",#"Failed to open url:",[url description]);
}
}
}
break;
default:
break;
}
}
break;
I am getting the error "Use of undeclared identifier url" on this line of code
NSURL *URL = [NSURL URLWithString:[NSString stringWithFormat:#"http://www.scanmarksapp.com", url]];
Removing url at the end of NSStringWithFormat makes the Safari view controller work. However on iOS below 9.0 e.g. 8.4 the app crashes.
The standard and recommended approach is to check for capability, not OS version. In this instance, you can check for the existence of the SFSafariViewController class.
if ([SFSafariViewController class] != nil) {
// Use SFSafariViewController
} else {
// Open in Mobile Safari
}
edit
Your implementation of openLink: is wrong.
- (void)openLink:(NSString *)url {
NSURL *URL = [NSURL URLWithString:url];
if (URL) {
if ([SFSafariViewController class] != nil) {
SFSafariViewController *sfvc = [[SFSafariViewController alloc] initWithURL:URL];
sfvc.delegate = self;
[self presentViewController:sfvc animated:YES completion:nil];
} else {
if (![[UIApplication sharedApplication] openURL:url]) {
NSLog(#"%#%#",#"Failed to open url:",[url description]);
}
}
} else {
// will have a nice alert displaying soon.
}
}

How can I link a local pdf file on html file - the html file is showing on UIWebView

I am showing a local html file on my UiWebView using following code.
NSString *path = [[NSBundle mainBundle] pathForResource:#"test.html"];
NSString* htmlString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
NSString *bundlePath = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:bundlePath];
[_webView loadHTMLString:htmlString baseURL:baseURL];
[_webView setBackgroundColor:[UIColor clearColor]];
App is showing the html correctly. In the test.html there is a link to local pdf file hello.pdf. This pdf is added to the project. But when I click on the link nothing happens. I want to load pdf on my web view when user clicks on the link.
I want to use the UIWebView delegate to send requests for internet hyperlinks (e.g. http://www.google.com) to the safari app.
-(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType
{
NSLog(#"%#",inRequest.URL.absoluteString);
if([inRequest.URL.absoluteString rangeOfString:#"hello.pdf"].location == NSNotFound)
{
if ( inType == UIWebViewNavigationTypeLinkClicked )
{
[[UIApplication sharedApplication] openURL:[inRequest URL]];
return NO;
}
return YES;
}
else
{
return NO;
}
}
You should have stated more clearly that you wanted internet hyperlinks to load in Safari and local hyperlinks to load in your UIWebView.
I've just tested this and it does what you want:
-(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType
{
if (inType == UIWebViewNavigationTypeLinkClicked) {
if ([inRequest.URL.absoluteString rangeOfString:#"http://"].location == NSNotFound) {
return YES;
}
else {
[[UIApplication sharedApplication] openURL:[inRequest URL]];
return NO;
}
}
else {
return YES;
}
}
This works by only loading links in your UIWebView that don't contain the hyper text type protocol prefix (http://).
Tip: Make sure you've set the delegate for the UIWebView in the viewDidLoad method.
- (void)viewDidLoad
{
[super viewDidLoad];
webView.delegate = self;
}

Open links in Safari instead of UIWebVIew?

I have an app with a UIWebView inside a UIViewController. I load HTML from a web service as a string like this:
self.webView loadHTMLString:_string baseURL:nil
Is it possible for the HTML links in this string to be opened in the browser and not in the UIWebView in my app? How can I do this?
I have tried this in the UIViewController that "hosts" the UIWebVIew:
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
if (navigationType == UIWebViewNavigationTypeLinkClicked) {
[[UIApplication sharedApplication] openURL:[request URL]];
return NO;
}
return YES;
}
It doesn't seem to be working....
Any ideas?
Have you set the delegate of the UIWebView to your UIViewController? There's nothing obviously wrong with your code as far as I can see, so it's likely to be something small like that.
Add this in class..
#interface yourViewController : UIViewController
<UIWebViewDelegate>
Add this in View did load
- (void)viewDidLoad
{
[description loadHTMLString:string baseURL:nil];
description.delegate = self;
}
Add this in your .m file
-(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType {
if ( inType == UIWebViewNavigationTypeLinkClicked ) {
[[UIApplication sharedApplication] openURL:[inRequest URL]];
return NO;
}
return YES;
}
Note:
UIWebView *description;
#synthesize description;
Then It will work perfectly the way you deserve..!! :)
Set the delegate of the UIWebView to your UIViewController after that use this UIWebview method and check the condition, for example now webview current url is google.com, if suppose you clicked on gmail the url contains the string with gmail. Use the below method it opened a safari browser and automatically loaded that url.
-(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType {
if ( inType == UIWebViewNavigationTypeLinkClicked ) {
NSURL *url = [inRequest URL];
if ([[url absoluteString] rangeOfString:#"gmail"].location == NSNotFound) {
[[UIApplication sharedApplication] openURL:[inRequest URL]];
return NO;
}
}
return YES;
}
If you already setup properly the UIWebViewDelegate, simply doing
self.webView loadHTMLString:_string baseURL:nil
self.webView.delegate = self;
should work
Add this line (
self.webview.delegate = self;
)
For Example in viewController.m
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:#"index" ofType:#"html"];
NSString* htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil];
[self.webview loadHTMLString:htmlString baseURL:nil];
self.webview.delegate = self;
Apple introduced a Safari View Controller on iOS 9. Safari View Controller brings all of the features user expect from Safari over to your app without ever leaving it.
First we need to import Safari Services
#import <SafariServices/SafariServices.h>
For Objective C:-
NSString *yourUrl = #"http://yourURL";
SFSafariViewController *svc= [[SFSafariViewController alloc] initWithURL:[NSURL URLWithString: yourUrl]];
[self presentViewController:svc animated:YES completion:nil];
For Swift:-
let svc = SFSafariViewController(URL: NSURL(string: self.urlString)!)
self.presentViewController(svc, animated: true, completion: nil)
Yes, in your hyperlink tag add
target="blank"
And one question mark will be fine, thank you

Resources