iPhone/iPad app 不像其它系統有共用的儲存空間, 每支app的儲存空間均是私有的, 因此要將私有檔案給其它app使用就需透過 UIActivityViewController 來達成
NSString *tmpPath = NSTemporaryDirectory(); //暫存目錄
//NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
//NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:@"%@/%@", tmpPath, @'檔案所在位置'];
[urlData writeToFile:filePath atomically:YES];
NSString *textToShare = @"檔案分享!";
NSURL *myWebsite = [NSURL fileURLWithPath:filePath isDirectory:NO];//用於本地檔案
//NSURL *myWebsite = [NSURL URLWithString:request.URL.absoluteString];//用於網址
NSArray *objectsToShare = @[textToShare, myWebsite];
UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:objectsToShare applicationActivities:nil];
NSArray *excludeActivities = @[UIActivityTypeAirDrop, UIActivityTypeCopyToPasteboard, UIActivityTypeOpenInIBooks,
UIActivityTypePrint, UIActivityTypeMail, UIActivityTypeMessage, UIActivityTypeAssignToContact,
UIActivityTypeSaveToCameraRoll, UIActivityTypeAddToReadingList, UIActivityTypePostToFlickr, UIActivityTypePostToVimeo,
UIActivityTypePostToFacebook,UIActivityTypePostToTwitter,UIActivityTypePostToWeibo,UIActivityTypePostToTencentWeibo];
activityVC.excludedActivityTypes = excludeActivities;//不提供的類型
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {//if iPhone
[self presentViewController:activityVC animated:YES completion:nil];
} else {//if iPad
activityVC.popoverPresentationController.sourceView = webView;
activityVC.popoverPresentationController.sourceRect = CGRectMake(self.view.frame.size.width/2, self.view.frame.size.height/5, 0, 0);
[self presentViewController:activityVC animated:YES completion:nil];
}
► swift 網路檔案
let urlScheme:String = request.url!.absoluteString.removingPercentEncoding!;
//檔案下載
let url = URL(string: urlScheme);
UIApplication.shared.isNetworkActivityIndicatorVisible = true;
do {
let urlData = try Data(contentsOf: url!, options: NSData.ReadingOptions.uncached);
UIApplication.shared.isNetworkActivityIndicatorVisible = false;
let tmpPath = NSTemporaryDirectory();
let filePath = "\(tmpPath)/\((url?.lastPathComponent)!)";
try urlData.write(to: URL(fileURLWithPath: filePath), options: .atomic);
let textToShare = "檔案分享!";
let myWebsite:NSURL = NSURL(fileURLWithPath:filePath, isDirectory:false);//
let objectsToShare:NSArray = [textToShare, myWebsite];
let activityVC = UIActivityViewController(activityItems:objectsToShare as! [Any], applicationActivities:nil);
let excludeActivities:NSArray = [UIActivityType.airDrop, UIActivityType.copyToPasteboard, UIActivityType.openInIBooks,
UIActivityType.print, UIActivityType.mail, UIActivityType.message, UIActivityType.assignToContact,
UIActivityType.saveToCameraRoll, UIActivityType.addToReadingList, UIActivityType.postToFlickr, UIActivityType.postToVimeo,
UIActivityType.postToFacebook,UIActivityType.postToTwitter,UIActivityType.postToWeibo,UIActivityType.postToTencentWeibo];
activityVC.excludedActivityTypes = excludeActivities as? [UIActivityType];
//if iPhone
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiom.phone) {
self.present(activityVC, animated:true, completion:nil);
} else {
activityVC.popoverPresentationController?.sourceView = webView;
activityVC.popoverPresentationController?.sourceRect = CGRect(x:(self.view.frame.size.width)/2, y:(self.view.frame.size.height)/5, width:0, height:0);
self.present(activityVC, animated:true, completion:nil);
}
} catch {
UIApplication.shared.isNetworkActivityIndicatorVisible = false;
}