[iOS] Sharing text as a string to another app

You could use a custom URL scheme. Apps like Facebook and WhatsApp generally have their own schemes that you can use to send data into those apps. See WhatsApp’s info here: Link

Alternatively, you could use a UIActivityViewController. This also supports other data types, not just strings (see this SO question).

    
NSString *textToShare = @"要分享的文本内容"; 
NSArray *activityItems = @[textToShare];
UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
[self presentViewController:activityVC animated:YES completion:nil];

Here’s a nice blog post on this method: Link


 

範例2號:

NSString *textToShare = @"要分享的文本内容"; 
UIImage *imageToShare = [UIImage imageNamed:@"iosshare.jpg"]; 
NSURL *urlToShare = [NSURL URLWithString:@"http://blog.csdn.net/hitwhylz"]; 
NSArray *activityItems = @[textToShare, imageToShare, urlToShare]; 



@property(nonatomic,copy) UIActivityViewControllerCompletionHandler completionHandler; // set to nil after call

typedef void (^UIActivityViewControllerCompletionHandler)(NSString *activityType, BOOL completed);

 


UIActivityViewController *activityVC = [[UIActivityViewController alloc]initWithActivityItems:activityItems applicationActivities:nil]; 

//给activityVC的属性completionHandler写一个block。 
//用以UIActivityViewController执行结束后,被调用,做一些后续处理。 
UIActivityViewControllerCompletionHandler myBlock = ^(NSString *activityType,BOOL completed) 
{ 
 NSLog(@"activityType :%@", activityType); 
 if (completed) 
 { 
 NSLog(@"completed"); 
 } 
 else 
 { 
 NSLog(@"cancel"); 
 } 
 
 //放回上一级界面 
 [self.navigationController dismissModalViewControllerAnimated:YES]; 
 
}; 
 
// 初始化completionHandler,当post结束之后(无论是done还是cancell)该blog都会被调用 
activityVC.completionHandler = myBlock;

 

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *