Attach object using iOS6 UIActivityViewController - ios6

I'm migrating to use the UIActivityViewController for sharing in iOS6, but I can't figure out how to create email attachment objects to be included when sharing by email.
The corresponding code in iOS5 is:
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
[picker addAttachmentData:data mimeType:#"application/XXX" fileName:fileName];

You have very limited control over UIActivityViewController, but if you're attaching well-know mime types, I found you can get it to work correctly by providing the associated file extension in a file URL. For example, if your attachment is a vCard, use the ".vcf" extension in the file URL:
NSString *docsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
// The file extension is important so that some mime magic happens!
NSString *filePath = [docsPath stringByAppendingPathComponent:#"vcard.vcf"];
NSURL *fileUrl = [NSURL fileURLWithPath:filePath];
[data writeToURL:fileUrl atomically:YES]; // save the file
// Now pass the file URL in the activity items array
UIActivityViewController *avc = [[UIActivityViewController alloc] initWithActivityItems:
#[#"Here's an attached vCard", fileUrl] applicationActivities:nil];
[vc presentModalViewController:avc animated:YES];

For anyone wondering why their files aren't being shared using UIActivityViewController to apps like DropBox and other generic file handling applications, what you really want is a UIDocumentInteractionController.
Use it something like this:
class ViewController {
var openInController:UIDocumentInteractionController!
init() {
openInController = UIDocumentInteractionController(URL: docURL)
}
func shareDoc {
openInController.presentOptionsMenuFromRect(CGRectZero, inView: self.view, animated: true)
}
}

From what I can tell you can't do this with the UIActivityViewController -- I can't even manage to make it present HTML content for the message body -- so you may be better off using SLComposeViewController.

Related

Must we have to download the remote file before using AirDrop?

I am sending the remote file (for example: image file, pdf file ... ) to Airdrop.
I have only the direct link of this file : http://example.com/xxx/png.
Currently, I am doing something like below code
NSString *filePath = self downloadFileFromServerWithURL
NSURL *urlFilePath = [NSURL fileURLWithPath:filePath];
NSArray *Items = [NSArray arrayWithObjects:
[NSNumber numberWithFloat:indexPath.row],
urlFilePath,nil];
UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:Items applicationActivities:[NSArray arrayWithObjects:xxx, yyy,nil];
[self presentViewController:activityViewController animated:YES completion:nil];
So, my question is, how can I share the remote file with Airdrop without downloading it before?
I show a lot of iOS softwares can do that, like DropBox
Thank you
you should look into using a subclass of UIActivityItemProvider which will let you asynchronously get/generate the actual item to be shared. The AirDrop sample code has an example of this.

requestAuthorizationToShareTypes method not displaying Permissions prompt in iOS 8 Xcode 6

-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
if ([HKHealthStore isHealthDataAvailable]){
NSSet *writeDataTypes = [self dataTypesToWrite];
NSSet *readDataTypes = [self dataTypesToRead];
[self.healthStore requestAuthorizationToShareTypes:writeDataTypes readTypes:readDataTypes completion:^(BOOL success, NSError *error) {
NSLog(#"%s",__func__);
if (!success) {
NSLog(#"You didn't allow HealthKit to access these read/write data types. In your app, try to handle this error gracefully when a user decides not to provide access. The error was: %#. If you're using a simulator, try it on a device.", error);
return;
}
dispatch_async(dispatch_get_main_queue(), ^{
// Update the user interface based on the current user's health information.
NSLog(#"=========================== %s",__func__);
});
}];
}
}
requestAuthorizationToShareTypes does not calling back completion method.
I had a similar issue with the permissions box not appearing and hadn't set up the HKHealthStore properly, putting this beforehand fixed it for me
self.healthStore = [[HKHealthStore alloc] init];
Here is a sample implementation that returns types instead of strings as described in the comment section.
-(NSSet*)datatypesToWrite {
NSArray *quantityTypes =
#[HKQuantityTypeIdentifierHeartRate,
HKQuantityTypeIdentifierBodyTemperature,
HKQuantityTypeIdentifierBloodPressureSystolic,
HKQuantityTypeIdentifierBloodPressureDiastolic,
HKQuantityTypeIdentifierRespiratoryRate];
NSMutableArray *hkTypes = [[NSMutableArray alloc] init];
for (NSString *identifier in quantityTypes) {
HKQuantityType *quantType =
[HKObjectType quantityTypeForIdentifier:identifier];
[hkTypes addObject:quantType];
}
// Make sure the types are of the correct style (Quantity, Category, Etc.)
HKCategoryType *catType =
[HKObjectType categoryTypeForIdentifier:HKCategoryTypeIdentifierSleepAnalysis];
[hkTypes addObject:catType];
return [[NSSet alloc] initWithArray:hkTypes];
}
Each time you request new types for the first time the modal permissions dialog will show up (but it won't show up again if you re-prompt for permissions not granted). Apple's guidelines are to prompt for everything you might need, but it feels a bit against best practices to me to request 12 types up front if I know somebody only asked to save into a few of them.

Different behaviour of [NSMutableString writeToFile] in iOS7 and iOS8

Here i have demo code for saving NSMutableString in to File (FileName.dat)
NSError* error = nil;
NSMutableString* dat = [[NSMutableString alloc] initWithCapacity:1];
BOOL result = [dat writeToFile:#"FileName.dat" atomically:YES encoding:NSUTF8StringEncoding error:&error];
but i have two different output while rung it in iOS7 and iOS8beta5
Output XCode5+iOS7
Output XCode5+iOS8
While running it in iOS7 it shows that there in an error in parsing file path,
but in iOS8beta5 it crash by saying that [NSFileManager fileSystemRepresentationWithPath:] have nil or empty path.
Question :
In both SDK iOS8 and iOS7 it take NSError as argument to return error, so i believe that it should return error instead of crashing application
Is apple mansion any changes regarding it, If yes then please give me reference link for the same.
The path you pass to [NSData writeToFile:atomically:] is not complete and should be a full path.
That is normally done by getting the path to the Documents folder and appending that filename.
The problem is your filename, #"FileName.dat", there is no path to the directory to save to. NSFileManager does not do this itself, you'll want to save to the Documents folder normally. Here is the code I usually use:
[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0] stringByAppendingPathComponent:myFileName];

Stream video using AFNetworking and cache video for playback

So I am trying to get my app to stream a video located on Amazon S3. So far, I can do that with the MediaPlayer Tutorial from Apple no problem: https://developer.apple.com/library/ios/#samplecode/MoviePlayer_iPhone/Introduction/Intro.html
However, I am using RestKit in my app and therefore AFNetworking as well. I see that AFNetworking is using caching, so I was wondering if it was possible to do the same with videos? I don't know how to stream a video using AFNetworking. So that would be a good start. I can download a movie using AFNetworking... but can I stream with it or I should just use the streaming system given by #import
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:#"https://s3.amazonaws.com/leclerc/videos/sop/IMG_0141.MOV"]];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [[paths objectAtIndex:0] stringByAppendingPathComponent:#"IMG_0141.MOV"];
operation.outputStream = [NSOutputStream outputStreamToFileAtPath:path append:NO];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"Successfully downloaded file to %#", path);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", error);
}];
[operation start];
For example, if I watch a music clip on Vevo and then rewatch 5 minutes later, do I have to redownload it on my iPad?! This is all new to me, so sorry for my ignorance!

Open Files from mail

I'm struggling with file handling on iOS.
I could already assign my file type to iOS and I can launch my app from mail with a special file.
My app is launching and I'm firing this method:
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
if([url isFileURL])
{
NSString *fileConts = [[NSString alloc] initWithContentsOfFile:[NSString stringWithFormat:#"%#", url] encoding:NSUTF8StringEncoding error:nil];
[self.viewController openFile:fileConts];
fileConts = nil;
}
return YES;
}
The openFile:(NSString) method is declared in the viewController and sets the value of a textView (for now). This method works fine. I tested it via [self.viewController openFile:#"test"];.
But when my application launches with file attached, the textView keeps empty.
It seems that it doesn't adopt the string value or that it can't read the string value.
handleOpenURL will be called only if application already running (in the background).
To make sure you correctly dispatch incoming files, you also need to check it on the app launch:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSURL *url = (NSURL *)[launchOptions valueForKey:UIApplicationLaunchOptionsURLKey];
// Process url here
}
It's good idea to have 1 URL dispatcher called both from handleOpenURL and didFinishLaunchingWithOptions.
I could solve my problem.
My mistake was to initWithContentsOfFile:(NSString *)
I updated my code with
NSString *fileConts = [[NSString alloc] initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error];
Now I'm happy!
Thanks for help.

Resources