问题
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