Launching a different app to open file on server - ios

The Apple Docs say that UIDocumentInteractionController "provides in-app support for managing user interactions with files in the local system". Is there a similar setup for viewing files on a server? I tried just sending the link to the file as the NSURL for interactionControllerWithURL:, but that didn't work. I guess the alternative would be to download the file, then open it once it has downloaded, then delete the file. That seems like a lot of extra coding work though, if there is an easier way that's already available.
Edit: I already know the name of the file I want to view/download, so I'm not really looking for the "file list" aspect of UIDocumentInteractionController. On the server, there are many Google Earth .kml files. The user isn't going to directly select which file to open from a list - I select the file to open programmatically based on actions taken by the user previously in the session.
As I understand it, presentOpenInMenuFromRect:inView:animated: will show a popover something like "Would you like to open the file 'myFile.kml' in Google Earth?". If the user selects 'Yes', the UIDocumentInteractionController launches Google Earth and opens myFile.kml. So I guess I'm not really looking for a file list viewer, just a way to trigger an "Open With" kinda functionality, where the file I'm opening exists on my server.

UIDocumentInteraction controller can't be used to view list of files present on server.
UIDocument interaction controller takes local file present in source application and shows list of applications that can open that file.Once use clicks on file then it is transferred to target app.
If you want to get file from server then you can use NSURLConnection class to download file form server and store it on device.

I think I figured this out. I don't need to use a UIDocumentInteractionController at all for what I'm looking for. I can simply
NSURL *url = [NSURL URLWithString:#"comgoogleearth://www.mysite.com/myfile.kml"];
BOOL canOpen = [[UIApplication sharedApplication] canOpenURL:url];
if (canOpen)
{
[[UIApplication sharedApplication] openURL:url];
}
else
{
NSLog (#"Open failed. Make sure Google Earth is installed and the URL is correct.");
}
This will launch Google Earth with my file just like I'm looking for.
FYI, I got the scheme for Google Earth from elpsk's answer to this question. It looks like a pretty comprehensive list of a lot of file types and the apps that open them. Just replace the comgoogleearth in my URL with the scheme for whatever app you are trying to open a file with.

Related

App not opened programmatically?

I installed Meeseva app on my device. When I try to open it programmatically it's not opening.
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:#"Meeseva App://location?id=1"]]) {
NSString *mystr=[[NSString alloc] initWithFormat:#"Meeseva App://location?id=1"];
NSURL *myurl=[[NSURL alloc] initWithString:mystr];
[[UIApplication sharedApplication] openURL:myurl];
}
When I opened fb, twitter, google+ and etc... all are opening successfully.
Can any solve this this issue?
App link is
https://itunes.apple.com/in/app/meeseva-app/id1121539928?mt=8
Is there any another way to open installed app programatically?
If your app can receive specially formatted URLs, you should register the corresponding URL schemes with the system. Apps often use custom URL schemes to vend services to other apps.
Hence "Meeseva" app might not have created a custom url for their app. So you can't do anything for it.
As mentioned above the apps which want to provide support for url schema then they have to create a custom URL schemes.
You need to find out what the correct URL scheme of the Meeseva App is. Meeseva App:// does not seem to be a valid URL scheme as it has a space in the middle.
For example the Google Maps URL scheme is comgooglemaps://, not Google Maps://
Usually developers make their URL scheme public in their documentation. However this is a feature that needs to be implemented and apps don't support this "out of the box". If the developer did not implement this, the app can't be opened via link.
Alternatively it is possible that the app reacts on "universal links". That means if there is a website for the app, iOS might ask you whether to open that website in the app or in Safari. In that case you could simply link to the website and let the user decide how the link should be opened. However, again, this needs to be implemented by the developer. If the app does not support universal links either, there's no way for you to open the app at all.

NSURL URLByResolvingBookmarkData seems to fail randomly

My app is an image viewer for iOS. I allow users to pick files from the camera roll or cloud file providers. For each file chosen, I create a bookmark so that the user to come back to view the file later without having to go through all of the steps of the camera roll/cloud file pickers again. The camera roll bookmarks work consistently, but the cloud file provider bookmarks fail randomly by returning nil from the call to URLByResolvingBookmarkData. The error returned is:
"File Not Found"
This happens with files from iCloud, DropBox, Box, Google Drive, etc. Here is how I create and retrieve the bookmarks:
// bookmark created from url returned by UIDocumentPickerViewController
NSData *bookmark = [url bookmarkDataWithOptions:options includingResourceValuesForKeys:nil relativeToURL:url error: nil];
// retrieving the URL from this bookmark works for a short time, then returns nil
url = [NSURL URLByResolvingBookmarkData:bookmark options:NSURLBookmarkResolutionWithoutUI relativeToURL:nil bookmarkDataIsStale:&isStale error:&error];
Anyone can shed some light as to why a bookmark will work for a while, then fail? Limited storage/RAM?
I have a feeling that the answer is to keep a local copy of files from cloud storage since they're not guaranteed to be accessible more than once.
You should use startAccessingSecurityScopedResource on the URL, making sure to call stopAccessingSecurityScopedResource when you are finished.
Alternatively, Apple highly recommend using FileCoordinator or UIDocument to manage access to files. Especially now you may access files that other apps are also accessing.

Add "Edit in Excel" or "Edit photo" extension

I checked the latest Dropbox and Excel for iOS. In Dropbox we get an edit button. On click it opens Excel's extension where you can edit the file.
After save, changes are reflected in the Dropbox file too.
I want to add such a button. Also I'd like to add such a button to images to open them in available "photo editing" apps.
How to check if file (image, xls, doc or any other) can be opened to edit?
Code so far:
UIDocumentPickerViewController *documentPicker = [[UIDocumentPickerViewController alloc] initWithURL:url inMode:UIDocumentPickerModeExportToService];
documentPicker.delegate = self;
documentPicker.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentViewController:documentPicker animated:YES completion:nil];
Also tried changing mode to UIDocumentPickerModeMoveToService...
As per apple docs
Move a local document. The user selects an external destination. The document picker moves the document; however, you can still access the document as an external document, letting the user edit the document in place.
But I tried all four modes. Did not show excel option.
UIDocumentPickerModeImport,
UIDocumentPickerModeOpen,
UIDocumentPickerModeExportToService,
UIDocumentPickerModeMoveToService
The only way to communicate with other iOS apps "locally" is using what is called URLSchemes.
This is the documentation to use URLScheme with the MSOffice apps.
https://msdn.microsoft.com/en-us/library/office/dn911482.aspx
Answering the specific question:
How to check if file (image, xls, doc or any other) can be opened to edit?
You can use the UIApplication method called canOpenURL to check if the current device responds to a specific URLScheme and if it does, you can call the app to edit you file. The same can be applied to other apps that you want to open. You just need to see if the app have URLScheme support.
Remembering that in iOS 9 you need to add the URLs you want to call during the app life in the Info.plist. Otherwise, the canOpenURL method will always returno NO.
This code illustrates the approach. However, it is to search some navigation apps. Just like tapping a shared friend location in WhatsApp.
https://snipt.net/wallaaa/using-url-schemes/
The result:
it's 2017 and iOS 11 and it's still not possible to edit files in place.
Dropbox is using MS Office API to provide http links to download and upload changed document.
Direct edit of the file is only possible if FileProvider extension is implemented and file was opened in editor app via document picker (which grants access to the file in sandbox).
Photo editing is allowed because editor is made by Apple (Photos app).

Locate a directory listing of an iOS app

I'm looking for a way to uncover directory paths in an iOS app, for the purposes of deep linking.
Right now, I know that I can unpack an app's info.plist file and look for the "CFBundleURLScheme." This parameter usually looks like this:
CFBundleURLTypes = (
{ CFBundleURLName = "BUNDLEID";
CFBundleURLSchemes = ( "scheme", "fb1234567891011" );
},
);
From here, I can construct a basic URL scheme to launch the app to the home page, by appending "://" to the end of one of the "CFBundleURLSchemes."
If I enter scheme://
into Safari's URL bar and hit enter, then the app will launch on my iPhone to the app's homepage.
However, there is another deep link I know works - scheme://upgrade/pro- This links to a specific in-app directory, and when launched in Safari, opens the app directly to the upgrade page.
I want to be able to find a list of in-app directories that I can append to the basic URL scheme to be able to link to actual in-app pages.
Is there a file somewhere in an app's bundle or Payload folder that has a list of defined directory paths?
I hope that made sense, thanks in advance for any guidance!
No. The URL goes to an entry-point in the app that is parsed by code. Each app can do it how it wants. It could just be a bunch of if/else statements.
Best bet is to ask the developer -- if they made these entry-points, they probably want people to know about them.

Open Local PDF in Safari in iOS

Let's say that myURL pointed to a PDF which was obtained by doing:
myURL = [[NSBundle mainBundle] URLForResource:#"my" withExtension:#"PDF"];
I can call [[UIApplication sharedApplication] canOpenURL:myURL] and see that it returns YES.
However, when I call [[UIApplication sharedApplication] openURL:myURL], nothing happens and it returns NO.
Is there a way to have this work?
Yes, I know I could use a UIWebView to have it open the PDF, but in this instance, I need the Safari App to open it, if possible. I am aware the natural consequence of this will be for it to exit my application. This behavior would be acceptable in this case.
Thank you.
This is not currently possible.
As far as I know it is not possible to hand a local PDF file off to MobileSafari.
But you can easily hand off a local PDF file to any locally installed application that acts as a PDF viewer. i.e., iBooks, GoodReader, etc. If this is sufficient, you should take a look at UIDocumentInteractionController. It will present a menu of apps, and let the user choose one to open the file in. Unfortunately, Safari itself does not register as a PDF viewer.
(If you did decide to present PDFs yourself, the QLPreviewController will display the file and give you the "send to other app" functionality.)

Resources