2017-06-23 46 views

回答

0

使用此为Facebook和Instagram没有任何集成
selectedIndexCollectionCells是要共享包含图像阵列 其他在Facebook上可以使用多个分享功能(使用Photos Framework)

- (IBAction)shareButtonAction:(id)sender { 
     if ([selectedIndexCollectionCells count] > 1){ 
      UIAlertController *alert1 = [UIAlertController alertControllerWithTitle:@"Share!" message:@"Please Select only 1 Image to share" preferredStyle:UIAlertControllerStyleAlert]; 
      UIAlertAction *firstAction = [UIAlertAction actionWithTitle:@"Okay" style:UIAlertActionStyleDefault handler:nil]; 
      [alert1 addAction:firstAction]; 
      [self presentViewController:alert1 animated:YES completion:nil]; 
     } 
     else if ([selectedIndexCollectionCells count] != 0) { 
      UIAlertController *alert1 = [UIAlertController alertControllerWithTitle:@"Social Media Platforms" message:@"Where Would you Like to Share" preferredStyle:UIAlertControllerStyleAlert]; 
      UIAlertAction *firstAction = [UIAlertAction actionWithTitle:@"Instagram" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action){ 
       [self savePostsPhotoBeforeSharing]; 
      }]; 
      UIAlertAction *secondAction = [UIAlertAction actionWithTitle:@"Facebook" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action){ 
       SLComposeViewController *controllerSLC = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook]; 
       [controllerSLC setInitialText:@""]; 
       [controllerSLC addURL:[NSURL URLWithString:@""]]; 
       [controllerSLC addImage:choosenImage]; 
       [self presentViewController:controllerSLC animated:YES completion:Nil]; 
      }]; 
      UIAlertAction *ThirdAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault handler:nil]; 
      [alert1 addAction:firstAction]; 
      [alert1 addAction:secondAction]; 
      [alert1 addAction:ThirdAction]; 
      [self presentViewController:alert1 animated:YES completion:nil]; 
     } 
     else { 
      UIAlertController *alert1 = [UIAlertController alertControllerWithTitle:@"Share!" message:@"Please Select Image To Share" preferredStyle:UIAlertControllerStyleAlert]; 
      UIAlertAction *firstAction = [UIAlertAction actionWithTitle:@"Okay" style:UIAlertActionStyleDefault handler:nil];   
      [alert1 addAction:firstAction]; 
      [self presentViewController:alert1 animated:YES completion:nil]; 
     } 
    } 


-(void)savePostsPhotoBeforeSharing { 
    UIImageWriteToSavedPhotosAlbum(choosenImage, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL); 
} 
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo: (void *) contextInfo; { 
    [self sharePostOnInstagram]; 
} 
-(void)sharePostOnInstagram 
{ 
    PHFetchOptions *fetchOptions = [PHFetchOptions new]; 
    fetchOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:NO],]; 
    __block PHAsset *assetToShare; 
    PHFetchResult *result = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options:fetchOptions]; 
    [result enumerateObjectsUsingBlock:^(PHAsset *asset, NSUInteger idx, BOOL *stop) { 
     assetToShare = asset; 
    }]; 

    if([assetToShare isKindOfClass:[PHAsset class]]) 
    { 
     NSString *localIdentifier = assetToShare.localIdentifier; 
     NSString *urlString = [NSString stringWithFormat:@"instagram://library?LocalIdentifier=%@",localIdentifier]; 
     NSURL *instagramURL = [NSURL URLWithString:urlString]; 
     if ([[UIApplication sharedApplication] canOpenURL: instagramURL]) { 
      [[UIApplication sharedApplication] openURL: instagramURL]; 
     } else { 
      UIAlertController *removedSuccessFullyAlert = [UIAlertController alertControllerWithTitle:@"Error!!" message:@"No Instagram Application Found!" preferredStyle:UIAlertControllerStyleAlert]; 
      UIAlertAction *firstAction = [UIAlertAction actionWithTitle:@"Okay" style:UIAlertActionStyleDefault handler:nil]; 
      [removedSuccessFullyAlert addAction:firstAction]; 
      [self presentViewController:removedSuccessFullyAlert animated:YES completion:nil]; 
     } 
    } 
} 
相关问题