iOS에서 사용자 지정 UIActivity를 생성해야합니까?
UIActivity
iOS 에서 사용자 지정 을 생성 해야합니까?
내가 원하는 이유는 사용자를 App Store의 리뷰 섹션으로 이동하는 앱 중 하나에 앱 리뷰 버튼을 추가하기 위해서입니다. 최상위 사용자 지정을 UIActivity
어떻게 만들 수 있습니까?
먼저 파일을 만듭니다. 내 이름을 ActivityViewCustomActivity로 선택했습니다.
ActivityViewCustomActivity.h를 다음과 같이 만드십시오.
#import <UIKit/UIKit.h>
@interface ActivityViewCustomActivity : UIActivity
@end
ActivityViewCustomActivity.m을 다음과 같이 만드십시오.
#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
내 이미지는 다음과 달라집니다. -악성 링크가 제거되었습니다 .- 원본은 250px .png입니다. http://i.imgur.com/pGWVj.png
이제 뷰 컨트롤러에서 다음을 수행하십시오.
#import "ActivityViewCustomActivity.h"
이제 표시하려는 위치 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];
}
여기에 내 Swift 버전이 있습니다. 여러 사용자 지정 작업이 필요 때문에이 클래스를 만들었습니다. 대표자 나 프로토콜이 필요 없습니다.
1. 사용자 지정 클래스 추가
class ActivityViewCustomActivity: UIActivity {
var customActivityType = ""
var activityName = ""
var activityImageName = ""
var customActionWhenTapped:( (Void)-> Void)!
init(title: String, imageName:String, performAction: (() -> ()) ) {
self.activityName = title
self.activityImageName = imageName
self.customActivityType = "Action \(title)"
self.customActionWhenTapped = performAction
super.init()
}
override func activityType() -> String? {
return customActivityType
}
override func activityTitle() -> String? {
return activityName
}
override func activityImage() -> UIImage? {
return UIImage(named: activityImageName)
}
override func canPerformWithActivityItems(activityItems: [AnyObject]) -> Bool {
return true
}
override func prepareWithActivityItems(activityItems: [AnyObject]) {
// nothing to prepare
}
override func activityViewController() -> UIViewController? {
return nil
}
override func performActivity() {
customActionWhenTapped()
}
}
2 View Controller에서 사용
다음을 호출하는 UIBarButtonItem에 연결되었습니다.
@IBAction func actionButtonPressed(sender: UIBarButtonItem) {
var sharingItems = [AnyObject]() // nothing to share...
let myCustomActivity = ActivityViewCustomActivity(title: "Mark Selected", imageName: "removePin") {
println("Do something")
}
let anotherCustomActivity = ActivityViewCustomActivity(title: "Reset All", imageName: "reload") {
println("Do something else")
}
let activityViewController = UIActivityViewController(activityItems:sharingItems, applicationActivities:[myCustomActivity, anotherCustomActivity])
activityViewController.excludedActivityTypes = [UIActivityTypeMail, UIActivityTypeAirDrop, UIActivityTypeMessage, UIActivityTypeAssignToContact, UIActivityTypePostToFacebook, UIActivityTypePrint, UIActivityTypeCopyToPasteboard, UIActivityTypeSaveToCameraRoll]
activityViewController.popoverPresentationController?.barButtonItem = sender
self.presentViewController(activityViewController, animated: true, completion: nil)
}
DogCoffee를 기반으로 한 내 Swift 3 구현 :
class ActivityViewCustomActivity: UIActivity {
// MARK: Properties
var customActivityType: UIActivityType
var activityName: String
var activityImageName: String
var customActionWhenTapped: () -> Void
// MARK: Initializer
init(title: String, imageName: String, performAction: @escaping () -> Void) {
self.activityName = title
self.activityImageName = imageName
self.customActivityType = UIActivityType(rawValue: "Action \(title)")
self.customActionWhenTapped = performAction
super.init()
}
// MARK: Overrides
override var activityType: UIActivityType? {
return customActivityType
}
override var activityTitle: String? {
return activityName
}
override class var activityCategory: UIActivityCategory {
return .share
}
override var activityImage: UIImage? {
return UIImage(named: activityImageName)
}
override func canPerform(withActivityItems activityItems: [Any]) -> Bool {
return true
}
override func prepare(withActivityItems activityItems: [Any]) {
// Nothing to prepare
}
override func perform() {
customActionWhenTapped()
}
}
다음은 UIActivity의 -activityViewController 메소드를 사용하여 이메일 작성기 인터페이스를 작성하는 예입니다. 이것은 어떤 목적을 선택하든 UIKit viewController 또는 사용자 정의 viewController를 설정하는 방법을 보여줍니다. -performActivity 메소드를 대체합니다.
#import <MessageUI/MessageUI.h>
#import <UIKit/UIKit.h>
@interface EPSuggestionsActivity : UIActivity <MFMailComposeViewControllerDelegate>
@end
@implementation EPSuggestionsActivity
....
- (UIViewController *)activityViewController{
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
NSString *emailAddress = @"developer@apple.com";
NSArray *toRecipients = @[emailAddress];
[picker setToRecipients:toRecipients];
[picker setSubject:@"Suggestions"];
return picker;
}
#pragma mark - MFMailComposeViewControllerDelegate Method
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
[self activityDidFinish:YES]; // Forces the activityViewController to be dismissed
}
@end
-activityDidFinish는 사용자가 이메일 인터페이스를 닫은 후 메일 작성기에서 호출됩니다. UIActivityViewController 인터페이스를 사라지게하는 데 필요합니다. 자신만의 viewController를 작성하는 경우 완료시 호출하는 델리게이트 메서드가 필요하며 UIActivity의 하위 클래스를 델리게이트로 이동합니다.
참고 URL : https://stackoverflow.com/questions/12766300/how-can-i-create-a-custom-uiactivity-in-ios
'ProgramingTip' 카테고리의 다른 글
UIImage를 CIImage로 또는 그 변환하는 방법 (0) | 2020.11.22 |
---|---|
C ++에서 std :: 벡터 확인 (0) | 2020.11.22 |
pyspark 데이터 프레임에 고유 한 열 값 표시 : python (0) | 2020.11.22 |
Emacs에서 한 번에 여러 줄 편집 (0) | 2020.11.22 |
이클립스 : 숨겨진 문자가 있습니다. 이제 끌 수 없습니다. (0) | 2020.11.22 |