簡體   English   中英

從UIWebView在Safari中打開URL文件(.pdf)

[英]Opening a URL files (.pdf) in Safari from UIWebView

我需要我的應用程序識別.PDF擴展名,並在Safari中而不是在UIWebView打開它,但是如果我調用了http://google.com之類的域,我希望它在UIWebView打開它。

請指教

您也可以檢查MIME類型,以獲取更長的旅程:

@property (nonatomic, strong) NSString *mime;

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
{
   mime = [response MIMEType];
}

- (BOOL)isDisplayingPDF {
    NSString *extension = [[mime substringFromIndex:([mime length] - 3)] lowercaseString];

    return ([[[self.webView.request.URL pathExtension] lowercaseString] isEqualToString:@"pdf"] || [extension isEqualToString:@"pdf"]);
}

您應該使用以下打開URL文件(.pdf):

NSURL *url = [NSURL URLWithString:@"http://www.stackoverflow.com/dd.pdf"];

if (![[UIApplication sharedApplication] openURL:url])

NSLog(@"%@%@",@"Failed to open url:",[url description]);

試試這個,它將在Safari中打開而無需使用webview

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
if (url != nil && [url isFileURL]) {
    if ([[url pathExtension] isEqualToString:@"pdf"]) {
        NSLog(@"URL:%@", [url absoluteString]);
    }
}
return YES;
}


- (void)viewDidLoad
{
[super viewDidLoad];

NSURL *url = [NSURL URLWithString:@"your pdf url"];

if (![[UIApplication sharedApplication] openURL:url])
    NSLog(@"%@%@",@" open url:",[url description]);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM