Reputation: 4100
I have activated the "Open in" feature for my app so that users can open their docs in my app. The app appears under the open in list, and when the user presses the icon, my app opens. The problem is that I can't seem to understand how to then go on saving the file. This is what I learned from the numerous StackOverflow questions:
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url sourceApplication:(NSString *)source annotation:(id)annotation
{
if ([[DBChooser defaultChooser] handleOpenURL:url]) {
return YES;
}
if (url != nil && [url isFileURL]) {
NSError *error = nil;
NSData *data = [NSData dataWithContentsOfURL:url options:NSDataReadingUncached error:&error];
if (error) {
NSLog(@"%@", [error localizedDescription]);
} else {
NSLog(@"Data has loaded successfully.");
}
return YES;
}
return NO;
}
I am trying to catch the data in the following way, but there is an error. Furthermore I was wondering if this method will cause any interference with my already existing Dropbox importer... I have read that the files are automatically saved in the documents/Inbox path.
As file types I can open I set public.content (all the files the Documents interaction controller can open, I believe).
Upvotes: 2
Views: 547
Reputation: 208
when you finish opening the document,the document gets saved to your app directory.The thing is you have to fetch the url and do necessary operations.
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
// your code
return YES;
}
use this delegate method in the Appdelegate.m.this method is called ,when you open any document in your app.
Download iExplorer,using which you can see through the documents and files in your application.After opening file using open-in feature
,your app's document directory gets updated.
Upvotes: 0
Reputation: 1915
Use the below code
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{
if(url !=nil)
{
NSData *urlData = [NSData dataWithContentsOfURL:url];
[urlData writeToFile:YourDocumentsDirectoryPath atomically:YES];
}
return YES;
}
Upvotes: 2
Reputation: 707
This is my open in code that works. should be noted that these are pdf's, and it is possible that the problem is with the type of file you are opening. Also note that it is important to clean out the inbox file if you are copying the files locally. Otherwise you are double saving the data and using up space.
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
if ([url isFileURL])
NSData *data = [NSData dataWithContentsOfURL:inboxPath];
[data writeToFile:scorePath atomically:YES];
[[NSFileManager defaultManager] removeItemAtURL:inboxPath error:nil];
Upvotes: 0
Reputation: 171
Use Following Code :
#define DocumentsDirectory [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES) objectAtIndex:0]
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
NSLog(@"URL : %@", url);
NSArray *tempArray = [[NSString stringWithFormat:@"%@",url] componentsSeparatedByString:@"/"];
NSString *aStrPath = [NSString stringWithFormat:@"%@/%@",DocumentsDirectory, [tempArray lastObject]];
NSData *aData = [NSData dataWithContentsOfURL:url];
[aData writeToFile:aStrPath atomically:YES];
return YES;
}
This Code is working...!!!
Upvotes: 1