简体   繁体   中英

Obj-c - Opening PDF in UIWebView?

I'm trying to open a PDF in my UIWebView . For some reason, when I pull the URL from an NSString , and then use the NSString in my code, the webview returns empty (even though the string contains url data). However, if I type the URL into URLWithString IN FULL (eg example.com/file.pdf), the PDF appears as it should. That said, the link to the PDF displayed is constantly different, which is why I'm using NSString to populate the UIView 's PDF link. Any idea why this isn't working?

ViewController.m

NSString *browserLink = self.linkProgram[@"body"];
NSURL *url = [NSURL URLWithString:browserLink];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[_webView loadRequest:request];

Sometimes a URL is corrupted or has characters in it that a UIWebView will not recognize and thus will not show you the page/PDF that you are interested in. Try this, use stringByAddingPercentEscapesUsingEncoding to try to remove any spaces and replace them with percent escapes that may be in the URL.

NSURL *url = [NSURL URLWithString:
[browserLink stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

This typically works, however I had issues in the past myself where this did not fix the issue for me. Instead I used the stringByReplacingOccurrencesOfString method to replace the characters.

NSURL *url = [NSURL URLWithString: 
[browserLink stringByReplacingOccurrencesOfString: @" " withString:@"%20"]];

EDIT

Brittany the issue for you is with App Transport Security . Ensure that your info.plist file contains the following keys to apply your ATS settings:

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
    <key>NSExceptionDomains</key>
    <dict>
        <key>pdfpdf.com</key>
        <dict>
            <key>NSExceptionAllowsInsecureHTTPLoads</key>
            <true/>
            <key>NSIncludesSubdomains</key>
            <true/>
        </dict>
    </dict>
</dict>

在此处输入图片说明

With this change the following code will work for you.

NSString *browserLink = self.linkProgram[@"body"];
NSURL *url = [NSURL URLWithString:browserLink];
UIWebView * webView = [[UIWebView alloc] initWithFrame: self.view.frame];
[self.view addSubview:webView];
[webView loadRequest:[NSURLRequest requestWithURL:url]];

RESULT

在此处输入图片说明

在此处输入图片说明 This is how i use it -

#import <WebKit/WebKit.h>


NSString *urlString = _datadiction[@"doclink"];

    if(urlString != nil || [urlString isEqualToString:@""]){

        WKWebViewConfiguration *theConfiguration = [[WKWebViewConfiguration alloc] init];
        WKWebView *webView = [[WKWebView alloc] initWithFrame:self.view.frame configuration:theConfiguration];
        NSURLRequest *nsrequest=[NSURLRequest requestWithURL:[NSURL URLWithString:urlString]];

        [webView setBackgroundColor:[UIColor whiteColor]];

        [webView loadRequest:nsrequest];
        [self.view addSubview:webView];

    }
    else{

        NSLog(@"Error");
    }

#P.S -  Update info.plist with Transport Security... (allow arbitary loads = YES)

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