Share video to Instagram feed from iOS

前端 未结 3 1615
無奈伤痛
無奈伤痛 2021-02-04 08:23

I\'ve been trying to create a sharing experience for our app where Instagram launches giving these two options:

Facebook has a pretty lean documentation about i

3条回答
  •  忘了有多久
    2021-02-04 08:46

    - (void)postMedia:(NSString *)media Type:(BOOL)isVideo {
    
        [SVProgressHUD showWithStatus:LS(@"Downloading...")];
    
        //download the file in a seperate thread.
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
    
            NSURL *url = [NSURL URLWithString:media];
            NSData *urlData = [NSData dataWithContentsOfURL:url];
            if ( urlData ) {
    
                NSString *filePath = [NSTemporaryDirectory() stringByAppendingPathComponent:isVideo?@"instagramShare.mp4":@"instagramShare.jpg"];
                NSURL *outputFileURL = [NSURL URLWithString:filePath];
    
                dispatch_async(dispatch_get_main_queue(), ^{
    
                    if (![urlData writeToFile:filePath atomically:YES]) {
                        [SVProgressHUD showErrorWithStatus:LS(@"Failed. Please try again.")];
                        return;
                    }
    
                    // Check authorization status.
                    [PHPhotoLibrary requestAuthorization:^( PHAuthorizationStatus status ) {
                        if ( status == PHAuthorizationStatusAuthorized ) {
    
                            // Save the movie file to the photo library and cleanup.
                            [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
                                // In iOS 9 and later, it's possible to move the file into the photo library without duplicating the file data.
                                // This avoids using double the disk space during save, which can make a difference on devices with limited free disk space.                            
                                PHAssetResourceCreationOptions *options = [[PHAssetResourceCreationOptions alloc] init];
                                options.shouldMoveFile = YES;
                                PHAssetCreationRequest *changeRequest = [PHAssetCreationRequest creationRequestForAsset];
                                if (isVideo)
                                    [changeRequest addResourceWithType:PHAssetResourceTypeVideo fileURL:outputFileURL options:options];
                                else
                                    [changeRequest addResourceWithType:PHAssetResourceTypePhoto fileURL:outputFileURL options:options];
    
                            } completionHandler:^( BOOL success, NSError *error ) {
    
                                if ( success ) {
    
                                    [SVProgressHUD dismiss];
    
                                    PHFetchOptions *fetchOptions = [[PHFetchOptions alloc] init];
                                    fetchOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:NO]];
                                    PHFetchResult *fetchResult;
                                    if (isVideo)
                                        fetchResult = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeVideo options:fetchOptions];
                                    else
                                        fetchResult = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options:fetchOptions];
                                    PHObject *lastAsset = fetchResult.firstObject;
                                    if (lastAsset != nil) {
                                        NSString *localIdentifier = lastAsset.localIdentifier;
                                        NSString *u = [NSString stringWithFormat:@"instagram://library?LocalIdentifier=%@", localIdentifier];
                                        NSURL *url = [NSURL URLWithString:u];
                                        dispatch_async(dispatch_get_main_queue(), ^{
                                            if ([[UIApplication sharedApplication] canOpenURL:url]) {
                                                [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
                                            } else {
    
                                                NSString *urlStr = @"https://itunes.apple.com/in/app/instagram/id389801252?mt=8";
                                                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlStr] options:@{} completionHandler:nil];
                                            }
                                        });
                                    }
                                }
                                else {
                                    [SVProgressHUD showErrorWithStatus:LS(@"Failed. Please try again.")];
                                }
                            }];
                        }                   
                    }];
                });
            }
            else {
                [SVProgressHUD showErrorWithStatus:LS(@"Failed. Please try again.")];
            }
        });
    }
    

提交回复
热议问题