如何在 iOS 8 系统范围的共享菜单中列出?
How to get listed in the iOS 8 system wide share menu?
我正在尝试让我的 iOS 8 应用程序在 iOS 8 系统范围的共享菜单中列出图像扩展名 .png、.jpg (.jpeg)、.gif(静态).
我需要在 info.plist
文件中添加什么?我从 iOS docs example 尝试了下面的代码,但没有用,我的应用程序没有出现在共享列表中。
<key>NSExtension</key>
<dict>
<key>NSExtensionAttributes</key>
<dict>
<key>NSExtensionServiceRoleType</key>
<string>NSExtensionServiceRoleTypeEditor</string>
</dict>
<key>NSExtensionPointIdentifier</key>
<string>com.apple.ui-services</string>
<key>NSExtensionPrincipalClass</key>
<string>ActionViewController</string>
</dict>
要说清楚,这是iOS 8 Share Menu
(也称为share sheet
)我的意思是:http://cdn1.tekrevue.com/wp-content/uploads/2014/09/ios8-share-sheets-customize.jpg
首先,您需要了解应用间通信。
您需要为您的应用程序自定义 url 方案。请参考:
https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/Inter-AppCommunication/Inter-AppCommunication.html
现在您有了自定义 url sheme 后,请执行以下操作:
要在系统范围的共享菜单中列出,您需要自定义 Activity。
制作一个 class 如下所示:
在 YourActivity.h 文件中
@interface YourActivity : UIActivity
-(void)someInitialisationMethod:(NSString*)string;
@end
并在 YourActivity.m 文件中
#import "YourActivity.h"
@implementation YourActivity
- (NSString *)activityType
{
return @"UIActivityCategoryShare";
}
- (NSString *)activityTitle
{ // here give the title of your app or the action name that it performs
return @"YourAppName";
}
+ (UIActivityCategory)activityCategory
{// there are two types of category- share and action
return UIActivityCategoryShare;
}
- (UIImage *)activityImage
{
// Note: These images need to have a transparent background and I recommend these sizes:
// iPadShare@2x should be 126 px, iPadShare should be 53 px, iPhoneShare@2x should be 100
// px, and iPhoneShare should be 50 px. I found these sizes to work for what I was making.
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
return [UIImage imageNamed:@"YourActivityImage.png"];
}
else
{
return [UIImage imageNamed:@"YourActivityImage.png.png"];
}
}
- (BOOL)canPerformWithActivityItems:(NSArray *)activityItems
{
NSLog(@"%s", __FUNCTION__);
return YES;
}
- (void)prepareWithActivityItems:(NSArray *)activityItems
{
NSLog(@"%s",__FUNCTION__);
}
- (UIViewController *)activityViewController
{
NSLog(@"%s",__FUNCTION__);
return nil;
}
- (void)performActivity
{
// This is where you can do anything you want, and is the whole reason for creating a custom
// UIActivity
NSURL *yourCustomURLMaybe = [NSURL URLWithString:someInitialisedString];
if ([[UIApplication sharedApplication] canOpenURL: yourCustomURL]) {
[[UIApplication sharedApplication] openURL: yourCustomURL];
}
else
{
NSLog(@"YourActivity not found");
}
[self activityDidFinish:YES];
}
@end
现在您的自定义 activity 已完成
你需要用这样的分享菜单启动它
NSString *yourCustomUrl = [NSString stringWithFormat:@"YourCustomURLScheme?..."];
NSString *escapeAdded = [yourCustomUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *finalCustomURL = [NSURL URLWithString:escapeAdded];
YourActivity *ca;
if ([[UIApplication sharedApplication] canOpenURL: finalCustomURL])
{
ca = [[YourActivity alloc]init];
}
else
{
ca = nil;
}
NSArray *applicationActivityArray = [[NSArray alloc] initWithObjects:ca,nil];
最后,当你展示分享框时,将上面的数组添加到应用程序活动中:
activityViewController = [[UIActivityViewController alloc] initWithActivityItems:@[...] applicationActivities:applicationActivityArray];
}
您缺少 NSExtensionActivationRule。请参阅 Apple 的扩展编程指南:Declaring Supported Data Types for a Share or Action Extension
For example, to declare that your Share extension can support up to
ten images, one movie, and one webpage URL, you might use the
following dictionary for the value of the NSExtensionAttributes key:
<key>NSExtensionAttributes</key>
<dict>
<key>NSExtensionActivationRule</key>
<dict>
<key>NSExtensionActivationSupportsImageWithMaxCount</key>
<integer>10</integer>
<key>NSExtensionActivationSupportsMovieWithMaxCount</key>
<integer>1</integer>
<key>NSExtensionActivationSupportsWebURLWithMaxCount</key>
<integer>1</integer>
</dict>
</dict>
我正在尝试让我的 iOS 8 应用程序在 iOS 8 系统范围的共享菜单中列出图像扩展名 .png、.jpg (.jpeg)、.gif(静态).
我需要在 info.plist
文件中添加什么?我从 iOS docs example 尝试了下面的代码,但没有用,我的应用程序没有出现在共享列表中。
<key>NSExtension</key>
<dict>
<key>NSExtensionAttributes</key>
<dict>
<key>NSExtensionServiceRoleType</key>
<string>NSExtensionServiceRoleTypeEditor</string>
</dict>
<key>NSExtensionPointIdentifier</key>
<string>com.apple.ui-services</string>
<key>NSExtensionPrincipalClass</key>
<string>ActionViewController</string>
</dict>
要说清楚,这是iOS 8 Share Menu
(也称为share sheet
)我的意思是:http://cdn1.tekrevue.com/wp-content/uploads/2014/09/ios8-share-sheets-customize.jpg
首先,您需要了解应用间通信。 您需要为您的应用程序自定义 url 方案。请参考: https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/Inter-AppCommunication/Inter-AppCommunication.html
现在您有了自定义 url sheme 后,请执行以下操作:
要在系统范围的共享菜单中列出,您需要自定义 Activity。
制作一个 class 如下所示: 在 YourActivity.h 文件中
@interface YourActivity : UIActivity
-(void)someInitialisationMethod:(NSString*)string;
@end
并在 YourActivity.m 文件中
#import "YourActivity.h"
@implementation YourActivity
- (NSString *)activityType
{
return @"UIActivityCategoryShare";
}
- (NSString *)activityTitle
{ // here give the title of your app or the action name that it performs
return @"YourAppName";
}
+ (UIActivityCategory)activityCategory
{// there are two types of category- share and action
return UIActivityCategoryShare;
}
- (UIImage *)activityImage
{
// Note: These images need to have a transparent background and I recommend these sizes:
// iPadShare@2x should be 126 px, iPadShare should be 53 px, iPhoneShare@2x should be 100
// px, and iPhoneShare should be 50 px. I found these sizes to work for what I was making.
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
return [UIImage imageNamed:@"YourActivityImage.png"];
}
else
{
return [UIImage imageNamed:@"YourActivityImage.png.png"];
}
}
- (BOOL)canPerformWithActivityItems:(NSArray *)activityItems
{
NSLog(@"%s", __FUNCTION__);
return YES;
}
- (void)prepareWithActivityItems:(NSArray *)activityItems
{
NSLog(@"%s",__FUNCTION__);
}
- (UIViewController *)activityViewController
{
NSLog(@"%s",__FUNCTION__);
return nil;
}
- (void)performActivity
{
// This is where you can do anything you want, and is the whole reason for creating a custom
// UIActivity
NSURL *yourCustomURLMaybe = [NSURL URLWithString:someInitialisedString];
if ([[UIApplication sharedApplication] canOpenURL: yourCustomURL]) {
[[UIApplication sharedApplication] openURL: yourCustomURL];
}
else
{
NSLog(@"YourActivity not found");
}
[self activityDidFinish:YES];
}
@end
现在您的自定义 activity 已完成 你需要用这样的分享菜单启动它
NSString *yourCustomUrl = [NSString stringWithFormat:@"YourCustomURLScheme?..."];
NSString *escapeAdded = [yourCustomUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *finalCustomURL = [NSURL URLWithString:escapeAdded];
YourActivity *ca;
if ([[UIApplication sharedApplication] canOpenURL: finalCustomURL])
{
ca = [[YourActivity alloc]init];
}
else
{
ca = nil;
}
NSArray *applicationActivityArray = [[NSArray alloc] initWithObjects:ca,nil];
最后,当你展示分享框时,将上面的数组添加到应用程序活动中:
activityViewController = [[UIActivityViewController alloc] initWithActivityItems:@[...] applicationActivities:applicationActivityArray];
}
您缺少 NSExtensionActivationRule。请参阅 Apple 的扩展编程指南:Declaring Supported Data Types for a Share or Action Extension
For example, to declare that your Share extension can support up to ten images, one movie, and one webpage URL, you might use the following dictionary for the value of the NSExtensionAttributes key:
<key>NSExtensionAttributes</key> <dict> <key>NSExtensionActivationRule</key> <dict> <key>NSExtensionActivationSupportsImageWithMaxCount</key> <integer>10</integer> <key>NSExtensionActivationSupportsMovieWithMaxCount</key> <integer>1</integer> <key>NSExtensionActivationSupportsWebURLWithMaxCount</key> <integer>1</integer> </dict> </dict>