如何在iOS中创建自定义UIActivity? [英] How can I create a custom UIActivity in iOS?

查看:167
本文介绍了如何在iOS中创建自定义UIActivity?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在iOS中创建自定义 UIActivity

How can I create a custom UIActivity in iOS?

我想要的原因是在我的一个应用程序中添加一个Review App按钮,将用户带到App Store的评论部分。如何创建这样的自定义 UIActivity

The reason I want this is to add a Review App button in one of my apps that takes the user to the review section in the App Store. How can I create such a custom UIActivity?

推荐答案

首先,创建文件。我选择命名我的ActivityViewCustomActivity

First, create the files. I chose to name mine ActivityViewCustomActivity

使ActivityViewCustomActivity.h看起来像这样:

Make ActivityViewCustomActivity.h look like this:

#import <UIKit/UIKit.h>

@interface ActivityViewCustomActivity : UIActivity

@end

使ActivityViewCustomActivity.m看起来像这样:

Make ActivityViewCustomActivity.m look like this:

#import "ActivityViewCustomActivity.h"

@implementation ActivityViewCustomActivity

- (NSString *)activityType
{
    return @"yourappname.Review.App";
}

- (NSString *)activityTitle
{
    return @"Review App";
}

- (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:@"iPadShare.png"];
    }
    else
    {
        return [UIImage imageNamed:@"iPhoneShare.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

    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"itms-apps://ax.itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?type=Purple+Software&id=yourappid"]];
    [self activityDidFinish:YES];
}

@end

这是我的图片所看到的喜欢:
这是我制作的.PSD: - 删除了恶意链接 -
这里是原来的250 px .p​​ng http://i.imgur.com/pGWVj.png

This is what my image looked like: Here is the .PSD I made: -- malicious link removed -- And here is the original 250 px .png http://i.imgur.com/pGWVj.png

现在在您的视图控制器中执行此操作:

Now in your view controller do this:

#import "ActivityViewCustomActivity.h"

现在,无论您想要显示 UIActivityViewController

And now wherever you want to display your UIActivityViewController:

   NSString *textItem = @"Check out the yourAppNameHere app: itunes http link to your app here";
   UIImage *imageToShare = [UIImage imageNamed:@"anyImage.png"];

   NSArray *items = [NSArray arrayWithObjects:textItem,imageToShare,nil];

   ActivityViewCustomActivity *aVCA = [[ActivityViewCustomActivity alloc]init];

   UIActivityViewController *activityVC =
   [[UIActivityViewController alloc] initWithActivityItems:items
                                                  applicationActivities:[NSArray arrayWithObject:aVCA]];

   activityVC.excludedActivityTypes = @[UIActivityTypePostToWeibo, UIActivityTypeAssignToContact, UIActivityTypePrint, UIActivityTypeCopyToPasteboard, UIActivityTypeSaveToCameraRoll];

   activityVC.completionHandler = ^(NSString *activityType, BOOL completed)
   {
        NSLog(@"ActivityType: %@", activityType);
        NSLog(@"Completed: %i", completed);
   };

   if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
   {
      self.popoverController = [[UIPopoverController alloc] initWithContentViewController:activityVC];

      CGRect rect = [[UIScreen mainScreen] bounds];

      [self.popoverController
                     presentPopoverFromRect:rect inView:self.view
                     permittedArrowDirections:0
                     animated:YES];
   }
   else
   {
       [self presentViewController:activityVC animated:YES completion:nil];
   }

这篇关于如何在iOS中创建自定义UIActivity?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆