How to get Source URL from -(bool) application openURL?

对着背影说爱祢 提交于 2019-12-12 06:22:16

问题


I have an app that opens a PDF from an HTTP url using -(BOOL)application:openURL:. So I go to Safari, open PDF in Safari, and then use [Open in "MyAPP"]

I want to save the URL, so that when the user closes the app and opens it again, the PDF can be opened up again.

-(BOOL)application:(UIApplication *)application openURL:(NSURL *)url {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    // to store
    [defaults setObject:[url absoluteString] forKey:@"LastPDFURL"];
    [defaults synchronize];
    <...etc...>
    return YES;
}

When I load from Key LastPDFURL, instead of getting:

(NSString*) @"http://www.someWebsite.com/LastPDF.pdf"

I get the local URL after the pdf was imported into my app, something like:

(NSString*) @"file:///<..dir..>/Developer/CoreSimulator/Devices/<..deviceID..>/
data/Containers/Data/Application/<..appID..>/Documents/Inbox/LastPDF.pdf"

So my question is:

Is it possible in this file importing interaction to get the Source URL from which the importing was performed? i.e. "http://www.someWebsite.com/LastPDF.pdf"?


回答1:


You don't get the original URL. When you choose to launch an app from Safari or Mail (or other apps) to open some sort of file, the original file is copied to your app's sandbox and the URL you get is to the copied file in your sandbox. You can't change this.

There is no way to know what the original URL was.

What you should do is move the file from the supplied URL to a more appropriate location within your app's bundle (such as Documents) and keep a reference to it there. When your app restarts you can access the previous file from that local location.



来源:https://stackoverflow.com/questions/29175607/how-to-get-source-url-from-bool-application-openurl

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!