简体   繁体   中英

how to open window.open method in ios objective c safari using webview

I'm new to the iOS development

I have develop an application to open url in UIWebView but that web site working fine in safari web browser but when I taped a link coding as window.open() it will load same webview . here is my code

ViewController.h file

//
//  ViewController.m
//  
//
//  Created by Code Kadiya on 5/18/17.
//  Copyright © 2017 Code Kadiya. All rights reserved.
//

#import "ViewController.h"

@interface ViewController () <UIWebViewDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.webView setDelegate:self];
    // Do any additional setup after loading the view, typically from a nib.

    NSURL *websiteUrl = [NSURL URLWithString:@"http://some.url"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:websiteUrl cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:15.0];
    [_webView loadRequest:request];
}

- (BOOL) webView:(UIWebView *)webView shouldStartLoadWithRequest: (NSURLRequest *)request navigationType: (UIWebViewNavigationType)navigationType {
    if (navigationType == UIWebViewNavigationTypeLinkClicked) {
        NSURL *url = [request URL];
        [[UIApplication sharedApplication] openURL: url options:@{} completionHandler:nil];
        return NO;
    }
    return YES;
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}





@end

and I found this code from loading web application source

<a onclick="open_pdf('office')" target="_blank">


function open_pdf(type) {
    window.open("http://some.url?type=" + type );            
 }

anyhow I need to open safari browser when I click that link but I cannot change the web application source code

I think

[[UIApplication sharedApplication] openURL: url options:@{} completionHandler:nil];

not executing for that type of links

I'm found solution we can override javascript injecting javascript as a string or file here is the example code I was added to my code as well as It's properly working to me

- (BOOL) webView:(UIWebView *)webView shouldStartLoadWithRequest: (NSURLRequest *)request navigationType: (UIWebViewNavigationType)navigationType {
    if(navigationType == UIWebViewNavigationTypeLinkClicked){
        [[UIApplication sharedApplication] openURL: [request URL] options:@{} completionHandler:nil];
        return NO;
    } else {
        [self.webView stringByEvaluatingJavaScriptFromString:@"window.close = function () { window.history.back(); }"];
    }
    return YES;
}

it is not to window.open() method anyhow we can override any method like this example

Thanks!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM