my app make a simple file called log.txt
the URL of this file (viewed in xcode) is file://localhost/var/mobile/Applications/NUMBER OF THE APPLICATION/Documents/log.txt
So I can see this file in the finder ...
I wanted to add the "open in" feature to my app to provide the user to share this file (via mail or imessage) or open this file in another compatible app.
Here is what I do :
-(void) openDocumentIn {
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:docFile]; //docFile is the path
//NSLog(#"%#",fileURL); // -> shows the URL in the xcode log window
UIDocumentInteractionController *documentController = [UIDocumentInteractionController interactionControllerWithURL:fileURL];
documentController.delegate = self;
documentController.UTI = #"public.text";
[documentController presentOpenInMenuFromRect:CGRectZero
inView:self.view
animated:YES];
}
Then the call to this function :
-(IBAction)share:(id)sender {
[self openDocumentIn];
}
When I run the app, I click on this "share" button, but nothing appends except showing me the path of the URL in the log window ...
I missed something ...
Thanks
EDIT : finally, it works on my real iphone ... there was no text viewer in the simulator !!! --'
EDIT 2 : it shows the apps that are available (pages, bump ...) but crashes finally :((( ! see here for the crash picture
Its a memory management issue. The main reason it crashes is because the object is not retained. Thats why if you declare it in the .h file and write an #property for retain when you do assign it the object gets retained.
So in your interface file (.h) you should have
#property (retain)UIDocumentInteractionController *documentController;
Then in your .m (implementation file) you can do
#synthesize documentController;
- (void)openDocumentIn{
// Some code here
self.documentController = [UIDocumentInteractionController interactionControllerWithURL:fileURL];
documentController.delegate = self;
documentController.UTI = #"public.text";
[documentController presentOpenInMenuFromRect:CGRectZero
inView:self.view
animated:YES];
// Some more stuff
}
Here is how it works for me :
I just put the declaration "UIDocumentInteractionController *documentController" in the .h file and it works !
I really don't know why, but ....
Related
This question already has answers here:
UIDocumentInteractionController no longer works in iOS6
(2 answers)
Closed 9 years ago.
In my app I use UIDocumentInteractionController to open a pdf document into acrobat reader (or any others viewers) but since iOS6 it doesn't work anymore.
I have try a lot of things, the last is from here :
UIDocumentInteractionController *docController = [[UIDocumentInteractionController alloc]init];
docController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:documentsDirectory]];
docControler.UTI = #"com.adobe.pdf";
docController.delegate = self;
CGrect navRect = self.view.frame;
[docController presentOpenInMenuFromRect:navRect inView:self.view animated:YES];
When this code is running, my app totally freeze.
I have try with "presentOpenInMenuFromBarButtonItem" but I have the same issue.
assign the controller to a strongly referenced property:
#property (nonatomic, strong) UIDocumentInteractionController *docController;
I've used UIDocumentInteractionController successfully in iOS 6. Sample code:
self.documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:myPDFPath]];
[self.documentInteractionController presentOpenInMenuFromBarButtonItem:self.actionButton animated:YES];
You need to retain a reference to the UIDocumentInteractionController or it will be released before it's job is done.
Also, it looks like you're using the path of the documents folder, not the path to a particular file. I don't think this will work - pass the path to a specific file.
Im sorry, i feel kind of dumb, but im having trouble implementing the following code for to download multiple files from the server.I have set up the MultipleDownload.h and MultipleDownload.m files as a new Objective-C class within my app. But not sure how call it from my updateView.m to perfrom the file downloads. As per instructions it says i need to initialize and start downloading with the following lines. I cant figure out where to put that code to start downloading files from urls. Do i have to setup a method within that MultipleDownload.m code and call that method it from another object (updateView.m) to initiate the download? or i do i put those lines into one of the methods in (updateView.m)? I honestly tried both and for some reason i keep getting errors, It says that urls. If i put it in updateView.m it says that self.urls and self.downloads is undeclared identifier. I tried to declare NSMutableArray *urls and MultipleDownload *downloads within my updateView.m and it doesnt seen to work either. Any input would be appreciated.
the MultipleDownload.m and MultipleDownload.h code is located on github:
http://github.com/leonho/iphone-libs/tree/master
To initialize and start the download:
self.urls = [NSMutableArray arrayWithObjects:
#"http://maps.google.com/maps/geo?output=json&q=Lai+Chi+Kok,Hong+Kong",
#"http://maps.google.com/maps/geo?output=json&q=Central,Hong+Kong",
#"http://maps.google.com/maps/geo?output=json&q=Wan+Chai,Hong+Kong",
nil];
self.downloads = [[MultipleDownload alloc] initWithUrls: urls];
self.downloads.delegate = self;
What you do is in updateView.h
create #properties for urls (type NSMutableArray) and downloads (type MultiDownload)
then in updateView.m you add a these functions
//Function to start download
- (void) startDownload
{
self.urls = [NSMutableArray arrayWithObjects:
#"YourURLS",
#"YourURLS",
#"YourURLS",
nil];
self.downloads = [[MultipleDownload alloc] initWithUrls: urls];
self.downloads.delegate = self;
}
//download finished for 1 item
- (void) didFinishDownload:(NSNumber*)idx {
NSLog(#"%d download: %#", [idx intValue], [downloads dataAsStringAtIndex: [idx intValue]]);
}
//download finished for all items
- (void) didFinishAllDownload {
NSLog(#"Finished all download!");
[downloads release];
}
I also suggest that if you have problem understanding self.urls and self.downloads to read some more info about objective c and properties, good luck
In some of my screens in my program the localized strings work and in some it doesn't.
(Xcode 4.2)
What I've done:
Added a Localizable.strings in the folder "Resources"
in my .h file, inside the #interface ClassViewController : UIViewController {} I added:
IBOutlet UILable *labelName;
Also in the .h file, I added
property (nonatomic, assign) IBOutlet UILabel *labelName;
In the .m file, I added :
#synthetize labelName;
Still in the .m file, I added inside "-(foid)dealloc" :
[labelName release];
In -(void)viewDidLoad I added :
self.labelName.text = [NSString stringWithFormat:#"KEY"];
Finally, in the xib file (with the Interface manager), I linked the label object with the variable.
So, as I said, this method works in some screen and not in others. Any idea?
Solution:
That what a stupid mistake. the line to enter the text should be:
NSLocalizedString(#"KEY", nil);
Use instead:
self.labelName.text = NSLocalizedString(#"KEY", "");
From the Documentation:
Name: NSLocalizedString
Description: NSString *NSLocalizedString(NSString *key, NSString *comment)
Availability: iOS (2.0 and later)
Abstract: Returns a localized version of a string.
As Vince pointed out, you need to use NSLocalizedString function. So in this case the code in 6 would be:
self.labelName.text = NSLocalizedString(#"The key for this label",#"Some comment");
That should work for you.
In XCode-4.2 you can do the following:
Click on "target"
Then select "Build Phase"
Select "Add Build Phase" & choose "Add Copy files".
After select "copy files(int item)".( where 'int' may be 1or 2 or 3 etc. depending upon how many items previously have been created).Set "Destination" to "Resources".
Now click on '+' & choose your created '.string' file(eg. localization.string).
It works, hope it will help you.
What do you imagine this line does?
self.labelName.text = [NSString stringWithFormat:#"KEY"];
Because it's functionally equivalent to writing this:
self.labelName.text = #"KEY";
But I suspect that what you meant to write was this instead:
self.labelName.text = NSLocalizedString(#"KEY", #"description");
I m using ASIHTTP Request sample source code for downloading urls.
My question is that how to pause and resume the downloading files.
Please Help.
Deprecation Notice
ASIHTTP was deprecated around 2011, if you want network connectivity now, you should look up AFNetworking for ObjC or Alamofire for Swift, or stick to the native support:
For native support, in iOS 7 Apple added the NSURLSession, which can start download tasks (NSURLSessionDownloadTask), and these can be cancelled and restarted, check the method downloadTaskWithResumeData(_:) from NSURLSession, that has a very good explanation.
ASIHTTP is no longer needed, if you gotta are keeping a legacy app running, well, good luck.
Original Answer
Posted on 2011
ASI itself can resume a download from a file using [myASIRequest setAllowResumeForFileDownloads:YES];, check out the How-to-Use for an example.
Edit: Ok, there's no easy way to pause a download (a [myRequest pause] would be ideal). After reading and trying for a while, turns out that cancelling the request and resending it again is the only way.
For example, here's an example class I made:
#interface TestViewController : UIViewController <ASIHTTPRequestDelegate>
{
ASIHTTPRequest *requestImage;
UIImageView *imageViewDownload;
}
#property (nonatomic, retain) IBOutlet UIImageView *imageViewDownload;
- (IBAction)didPressPauseButton:(id)sender;
- (IBAction)didPressResumeButton:(id)sender;
To start (or restart) the download:
- (IBAction)didPressResumeButton:(id)sender
{
if (requestImage)
return;
NSLog(#"Resumed");
// Request
requestImage = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:#"http://www.myheadhealth.com/_IMAGES/cheese.jpg"]];
// Using a temporary destination path just for show.
// Better pick a suitable path for your download.
NSString *destinationDownloadPath = [NSTemporaryDirectory() stringByAppendingPathComponent:#"cheese.jpg"];
NSString *temporaryDownloadPath = [NSTemporaryDirectory() stringByAppendingPathComponent:#"cheese.jpg-part"];
requestImage.downloadDestinationPath = destinationDownloadPath;
requestImage.temporaryFileDownloadPath = temporaryDownloadPath;
requestImage.allowResumeForFileDownloads = YES;
requestImage.delegate = self;
[requestImage startAsynchronous];
}
In the exmaple above, I'm loading a random image of cheese I found, and storing a temporary download, and a full download.
Canceling the request will keep the contents of the temporary file intact, in fact, I made an UIImageView, and when pressing "Cancel" I could see a part of the downloaded file. If you want to try it out:
- (void)didPressPauseButton:(id)sender
{
if (requestImage)
{
// An imageView I made to check out the contents of the temporary file.
imageViewDownload.image = [UIImage imageWithContentsOfFile:requestImage.temporaryFileDownloadPath];
[requestImage clearDelegatesAndCancel];
requestImage = nil;
NSLog(#"Canceled");
}
}
- (void)requestFinished:(ASIHTTPRequest *)request
{
imageViewDownload.image = [UIImage imageWithContentsOfFile:request.downloadDestinationPath];
requestImage = nil;
}
From the below link I download the .zip file it contains some example. In that I played the OAuthSampleTouch application.
http://code.google.com/p/gdata-objectivec-client/downloads/list
This is the file I downloaded :
gdata-objectivec-client-1.11.0.zip
I gone through it but I am getting confusion how to pass input that is email and password and then how to connect through google form my iphone app.
Newly edited code:
This is code from OAuthSampleTouch example in the class "OAuthSampleRootViewControllerTouch.m" Please refer http://code.google.com/p/gdata-objectivec-client/downloads/list in that OAuthSampleTouch application.
- (void)signInToGoogle
{
[self signOut];
NSString *keychainAppServiceName = nil;
if ([self shouldSaveInKeychain])
{
keychainAppServiceName = kAppServiceName;
}
// For GData applications, the scope is available as
// NSString *scope = [[service class] authorizationScope];
NSString *scope = #"http://www.google.com/m8/feeds/";
// ### Important ###
// GDataOAuthViewControllerTouch is not designed to be reused. Make a new
// one each time you are going to show it.
// Display the autentication view.
GDataOAuthViewControllerTouch *viewController = [[[GDataOAuthViewControllerTouch alloc]
initWithScope:scope
language:nil
appServiceName:keychainAppServiceName
delegate:self
finishedSelector:#selector(viewController:finishedWithAuth:error:)] autorelease];
// You can set the title of the navigationItem of the controller here, if you want.
// Optional: display some html briefly before the sign-in page loads
NSString *html = #"<html><body bgcolor=silver><div align=center>Loading sign-in page...</div></body></html>";
[viewController setInitialHTMLString:html];
[[self navigationController] pushViewController:viewController animated:YES];
}
This is code from OAuthSampleTouch example in the class "OAuthSampleRootViewControllerTouch.m" Please check it. What is the scope and from where I need to pass credentials. If I run this I am getting the log as Authentication error:
Please help me, I am trying it from three days.
Any help is much appreciated.
Thank you,
Madan Mohan.
Search for this method
(GDataServiceGoogleContact *)contactService
Here you will be giving username and password for google account..