EEC-492/693/793 iPhone Application Development Lecture 21 Wenbing Zhao & Nigamanth Sridhar 4/11/2019 4/11/2019 EEC492/693/793 - iPhone Application Development EEC492/693/793 - iPhone Application Development 1
Outline Accessing build-in applications Assignment: Build the Email and PhotoLibrary apps 4/11/2019 4/11/2019 EEC492/693/793 - iPhone Application Development EEC492/693/793 - iPhone Application Development 2
Invoking Build-in Applications [[UIApplication sharedApplication] openURL:url] Email: NSString *emailString = @”mailto:?to=user@email.com&subject=Subject&body=Body”; url = [NSURL URLWithString:emailString]; Safari: url = [NSURL URLWithString: @”http://www.apple.com”]; Phone: url = [NSURL URLWithString: @”tel:96924065”]; SMS: url = [NSURL URLWithString: @”sms:96924065”]; Drawback: calling app will be put to background, user must manually switch back to the app 4/11/2019 EEC492/693/793 - iPhone Application Development
Invoking the Build-in App without Leaving the Current App Invoking the Mail composer UI Use MFMailComposeViewController Invoking the Message composer UI Use MFMessageComposeViewController -(IBAction) btnComposeEmail: (id) sender { MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init]; picker.mailComposeDelegate = self; [picker setSubject:@”Email subject here”]; [picker setMessageBody:@”Email body here” isHTML:NO]; [self presentModalViewController:picker animated:YES]; [picker release]; } - (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error { [controller dismissModalViewControllerAnimated:YES]; 4/11/2019 EEC492/693/793 - iPhone Application Development
The Image Picker Interface UIImagePickerController Handles all user and device interactions UIViewController subclass UIImagePickerControllerDelegate Implemented by your delegate object Steps for using the image picker Check the source availability: isSourceTypeAvailable: class method Assign a delegate object Present the controller modally 4/11/2019 EEC492/693/793 - iPhone Application Development
Displaying the Image Picker Called from a view controller if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]) { UIImagePickerController* picker = [[UIImagePickerController alloc] init]; picker.sourceType = UIImagePickerControllerSourceTypeCamera; picker.delegate = self; // picker.allowsEditing = YES; [self presentModalViewController:picker animated:YES]; } enum { UIImagePickerControllerSourceTypePhotoLibrary, UIImagePickerControllerSourceTypeCamera, UIImagePickerControllerSourceTypeSavedPhotosAlbum }; typedef NSUInteger UIImagePickerControllerSourceType; 4/11/2019 EEC492/693/793 - iPhone Application Development
Defining Your Delegate Object Accept case - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { // Check media type, and save or use the media here [self dismissModalViewControllerAnimated:YES]; // Dismiss the image picker [picker release]; } Cancel case - (void)imagePickerControllerDidCancel: (UIImagePickerController*)picker // Dismiss the image picker. [self dismissModalViewControllerAnimated:YES]; 4/11/2019 EEC492/693/793 - iPhone Application Development
EEC492/693/793 - iPhone Application Development More on the Accept Case If an image was picked, a dictionary containing the image is passed in If a movie was picked, a filesystem URL for the movie is passed in - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { UIImage *image; NSURL *mediaUrl = (NSURL *)[info valueForKey:UIImagePickerControllerMediaURL]; if (mediaUrl == nil) { image = (UIImage *) [info valueForKey:UIImagePickerControllerEditedImage]; if (image == nil) { //---original image selected--- image = (UIImage *) [info valueForKey:UIImagePickerControllerOriginalImage]; //---display the image--- } else //---edited image picked--- { // handle the image here else { //---video picked--- 4/11/2019 EEC492/693/793 - iPhone Application Development
Accessing the Videos in the Photo Library Specify media types before launch the image picker (default is image only) NSArray *mediaTypes = [NSArray arrayWithObjects:kUTTypeImage, kUTTypeMovie, nil]; imagePicker.mediaTypes = mediaTypes; To play the video picked (works only in ios 4) #import <MediaPlayer/MediaPlayer.h> #import <MobileCoreServices/MobileCoreServices.h> MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:mediaUrl]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(movieFinishedCallback:) name:MPMoviePlayerPlaybackDidFinishNotification object:player]; //---play partial screen--- player.view.frame = CGRectMake(0, 0, 320, 460); [self.view addSubview:player.view]; [player play]; 4/11/2019 EEC492/693/793 - iPhone Application Development
Accessing the Videos in the Photo Library Cleaning up when video is done playing - (void) movieFinishedCallback:(NSNotification*) aNotification { MPMoviePlayerController *player = [aNotification object]; [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:player]; [player.view removeFromSuperview]; [player autorelease]; } 4/11/2019 EEC492/693/793 - iPhone Application Development