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!!!");
}
Related
-(void) openPopUpWithUrl:(NSString*)urlString{
AppDelegate* appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
ExtraWebViewController* extraWebView = [[ExtraWebViewController alloc] init];
extraWebView.url = urlString;
[appDelegate.masterViewController presentViewController:extraWebView animated:YES completion:nil];
}
-(BOOL) webView:(WKWebView *)webView decidePolicyForNavigationAction:(NSURLRequest *)request navigationType:(WKNavigationResponsePolicy)navigationType decisionHandler:(nonnull void (^)(WKNavigationActionPolicy))decisionHandler {
if([(AKBannerWebView*)webView isFirstLoad]){
return YES;
}
else{
[self openPopUpWithUrl:[[request URL] absoluteString]];
return NO;
I want to open fullscreen with the wkwebview in the banner? objective-c
My cordova library was old and had to update it because of iOS11. I have updated it to latest version 4.4.0. CDVViewController is totally different now which enables webviewEngine that takes UIWebview or WKWebview. How do I initiate Cordova webview in my native app. Previously, I loaded it like this -
- (void) viewDidLoad {
[super viewDidLoad];
CDVViewController *viewController = [CDVViewController new];
viewController.wwwFolderName = #"www";
viewController.startPage = #"blank.html";
viewController.view.frame = self.webView.frame;
[self.webView removeFromSuperview];
self.webView = nil;
[self.view addSubview:viewController.view];
self.webView = viewController.webView;
viewController.webView.delegate = self;
self.cordovaWebViewController = viewController;
self.webView.scalesPageToFit = YES;
if (self.url)
[self.webView loadRequest: [NSURLRequest requestWithURL: self.url]];
}
And used webview delegates as
#pragma mark Webview Delegate
- (void) webViewDidStartLoad: (UIWebView *) webViewLocal {
[self.spinner startAnimating];
[self.cordovaWebViewController webViewDidStartLoad:webViewLocal];
}
- (void) webViewDidFinishLoad: (UIWebView *) webViewLocal {
[self.spinner stopAnimating];
[self.cordovaWebViewController webViewDidFinishLoad:webViewLocal];
}
- (void) webView: (UIWebView *) webView didFailLoadWithError: (NSError *) error {
[self.spinner stopAnimating];
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
if (navigationType == UIWebViewNavigationTypeLinkClicked) {
if ([request.URL.absoluteString hasSuffix: #"link_restart_movie_file"]) {
[self loadItem];
return NO;
}
[self.webView loadRequest: request];
return NO;
}
[self.cordovaWebViewController webView:self.webView shouldStartLoadWithRequest:request navigationType:navigationType];
return YES;
}
How do I use WebviewEngine to load in my viewcontroller? Is there any tutorial, that explains the usage of CDVViewController? Please help! Thanks!
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.
}
}
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
In my app I have 2 UIWebView's and 2 Adress Bar tat called WebView and WebView2, webAdress and webAdress2. I need to get url from WebView and put it to webAdress, and from WebView2 and put it to webAdress2.
When I use this code, the URL updates appears only in first webAdress, url from WebView2 apperas in first webAdress too. Moreover, all pages from WebView2 starts to be load in WebView.
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
//CAPTURE USER LINK-CLICK.
if (navigationType == UIWebViewNavigationTypeLinkClicked) {
NSURL *URL = [request URL];
if ([[URL scheme] isEqualToString:#"http"]) {
[webAdress setText:[URL absoluteString]];
[self gotoAddress:nil];
}
return NO;
}
return YES;
}
- (BOOL)webView2:(UIWebView*)webView2 shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
//CAPTURE USER LINK-CLICK.
if (navigationType == UIWebViewNavigationTypeLinkClicked) {
NSURL *URL = [request URL];
if ([[URL scheme] isEqualToString:#"http"]) {
[webAdress2 setText:[URL absoluteString]];
[self gotoAddress2:nil];
}
return NO;
}
return YES;
}
I guess you are in need of only one delegate method. Check which webview has triggered this delegate method and perform actions depend on this:
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
//CAPTURE USER LINK-CLICK.
if (navigationType == UIWebViewNavigationTypeLinkClicked) {
NSURL *URL = [request URL];
if ([[URL scheme] isEqualToString:#"http"]) {
if (webView == webView1)
[webAdress setText:[URL absoluteString]];
if (webView == webView2)
[webAdress2 setText:[URL absoluteString]];
[self gotoAddress2:nil];
}
return NO;
}
return YES;
}
Just set for all web views delegate as self, and all you can handle all actions in this method.