Problem: Create an Instance Object of the uiactivityviewcontroller class and share the content through this class.
It is easy to share data in IOS. What you need to do is to use the method initwithactivityitems: applicationactivities of the uiactivityviewcontroller class to instantiate an object. The parameters of this method are as follows:
Initwithactivityitems
This items array is the content you want to share. It can be nsstring or uiimage, or any custom class that complies with the uiactivityitemsource protocol.
Applicationactivities
This is an array of uiactivity instances, representing the activities supported by the program.
Example:
Create text box and button:
- (void)createTextField{ self.textField = [[UITextField alloc]initWithFrame:CGRectMake(20, 35, 280, 30)]; //? //self.textField.translatesAutoresizingMaskIntoConstraints = NO; self.textField.borderStyle = UITextBorderStyleRoundedRect; self.textField.placeholder = @"输入分享内容"; self.textField.delegate = self; [self.view addSubview:self.textField]; }- (void)createButton{ self.buttonShare = [UIButton buttonWithType:UIButtonTypeRoundedRect]; //self.buttonShare.translatesAutoresizingMaskIntoConstraints = NO; self.buttonShare.frame = CGRectMake(20, 80, 280, 44); [self.buttonShare setTitle:@"Share`" forState:UIControlStateNormal]; [self.buttonShare addTarget:self action:@selector(handleShare:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:self.buttonShare];}
Call two methods in viewdidload.
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. self.view.backgroundColor = [UIColor whiteColor]; [self createButton]; [self createTextField];}
Click return on the keyboard to hide the keyboard.
- (BOOL)textFieldShouldReturn:(UITextField *)textField{ [textField resignFirstResponder]; return YES;}
Method called by clicking the share button: Share the content through uiactivityviewcontroller:
- (void)handleShare:(id)paramSender{ if ([self.textField.text length]== 0) { NSString *message = @"Please enter a text and then press Share"; UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:nil message:message delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; [alertView show]; return; }//重要 self.activityViewController = [[UIActivityViewController alloc]initWithActivityItems:@[self.textField.text] applicationActivities:nil]; [self presentViewController:self.activityViewController animated:YES completion:^{ }];}
UI: Use uiactivityviewcontroller to display sharing options