2014-02-28 7 views
7

In meinem UIActivityViewController verwende ich den Beendigungshandler, um eine "erfolgreich freigegebene" Benachrichtigung auszuführen. Es funktioniert, aber mein einziges Problem ist, es zeigt immer noch die Benachrichtigung, wenn der Benutzer auf Abbrechen drückt.UIActivityViewController-Vervollständigungshandler ruft weiterhin Aktion auf, wenn Benutzer Abbrechen drückt

Hier ist mein Abschluss-Handler-Code,

[controller setCompletionHandler:^(NSString *activityType, BOOL completed) { 


    CWStatusBarNotification *notification = [CWStatusBarNotification new]; 
    [notification displayNotificationWithMessage:@"✓ Successfully Shared Centre!" 
              forDuration:3.0f]; 

    notification.notificationLabelBackgroundColor = [UIColor colorWithRed:38.0f/255.0f green:81.0f/255.0f blue:123.0f/255.0f alpha:1.0f]; 
    notification.notificationLabelTextColor = [UIColor whiteColor]; 



}]; 

Danke für die Hilfe!

+2

Vorschlag: Keine Meldung "Erfolgreich geteilt" anzeigen. Es ist wahrscheinlich nervig. – Daniel

+0

Mein Designer will es, ich stimme nicht zu, aber er ist der Kopf:/ –

Antwort

13

Das ist, was das completed Argument ist für:

[controller setCompletionHandler:^(NSString *activityType, BOOL completed) { 
    if (!completed) return; 

    CWStatusBarNotification *notification = [CWStatusBarNotification new]; 
    [notification displayNotificationWithMessage:@"✓ Successfully Shared Centre!" 
            forDuration:3.0f]; 

    notification.notificationLabelBackgroundColor = [UIColor colorWithRed:38.0f/255.0f green:81.0f/255.0f blue:123.0f/255.0f alpha:1.0f]; 
    notification.notificationLabelTextColor = [UIColor whiteColor]; 
}]; 
1

Der completed Parameter ist NO. Der Benutzer bricht ab.

[controller setCompletionHandler:^(NSString *activityType, BOOL completed) { 
    if (completed) { 
     CWStatusBarNotification *notification = [CWStatusBarNotification new]; 
     [notification displayNotificationWithMessage:@"✓ Successfully Shared Centre!" 
              forDuration:3.0f]; 

     notification.notificationLabelBackgroundColor = [UIColor colorWithRed:38.0f/255.0f green:81.0f/255.0f blue:123.0f/255.0f alpha:1.0f]; 
     notification.notificationLabelTextColor = [UIColor whiteColor]; 
    } 
}]; 
18

Hinweis: Die completionHandler Eigenschaft wird in iOS8 veraltet, so ist es nicht mehr möglich, das Ergebnis einer Aktie Aktion zu kennen. https://developer.apple.com/documentation/uikit/uiactivityviewcontroller/1622010-completionhandler

Update: Wie adruzh sagte auf iOS8 gibt es einen neuen completionHandler, dass Apple in der Dokumentation zu erwähnen vergaß:

[activityController setCompletionWithItemsHandler: 
    ^(NSString *activityType, BOOL completed, NSArray *returnedItems, NSError *activityError) { 
}]; 

https://developer.apple.com/documentation/uikit/uiactivityviewcontroller/1622022-completionwithitemshandler

+2

Sieht aus wie ein Fehler in der Dokumentation - es gibt einen neuen Handler, es ist in UIKit Änderungen – adruzh

2

Für die Swifties gibt, ist hier, wie Sie würden dies in Swift zusammen mit einer Share-Service-Erkennung codieren:

activityViewController.completionHandler = {(activityType, completed:Bool) in 
    if !completed { 
     //cancelled 
     return 
    } 

    //shared successfully 

    //below is how you would detect for different sharing services 
    var activity:String = "other" 
    if activityType == UIActivityTypePostToTwitter { 
     activity = "twitter" 
    } 
    if activityType == UIActivityTypeMail { 
     activity = "mail" 
    } 
    //more code here if you like 
} 
+0

verwiesen 'completionHandler' ist in iOS 8 veraltet. Verwenden Sie für iOS 8 'completionWithItemsHandler'. – Crashalot

3

Für Swift, ist das, was für uns gearbeitet:

... 

    // Configure UIActivityViewController 
    let activityViewController = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil) 
    activityViewController.excludedActivityTypes = [UIActivityTypeAirDrop, 
     UIActivityTypeAddToReadingList, 
     UIActivityTypeAssignToContact, 
     UIActivityTypePrint, 
     UIActivityTypeCopyToPasteboard] 

    // Show UIActivityViewController 
    presentViewController(activityViewController, animated: true, completion: nil) 

    // Define completion handler 
    activityViewController.completionWithItemsHandler = doneSharingHandler 

    ... 

func doneSharingHandler(activityType: String!, completed: Bool, returnedItems: [AnyObject]!, error: NSError!) { 
    // Return if cancelled 
    if (!completed) { 
     return 
    } 

    // If here, log which activity occurred 
    println("Shared video activity: \(activityType)") 
} 
1

SWIFT 2.0, iOS 8.0>, sollten Sie Abschluss-Handler wie folgt verwenden:

self.presentViewController(activityVC, animated: true, completion: nil) 

activityVC.completionWithItemsHandler = {(activityType, completed:Bool, returnedItems:[AnyObject]?, error: NSError?) in 
    //do some action 
} 

meine Antwort siehe hier: https://stackoverflow.com/a/34581940/1109892

1

Swift 3

func completionHandler(activityType: UIActivityType?, shared: Bool, items: [Any]?, error: Error?) { 
     if (shared) { 
      print("Cool user shared some stuff") 
     } 
     else { 
      print("Bad user canceled sharing :(") 
     } 
    } 

    activityController.completionWithItemsHandler = completionHandler 
Verwandte Themen