ios - Can I save a file on the iPhone? -
i have android app porting iphone , important feature requires opening simple text file user downloads. on android, usual way user file email attachment, way user can download file iphone app can open work. there way on iphone?
i'm not quite sure how you're handling text file, following methods can retrieve text file attached email when user selects open attachment mail app in app.
first need register app being able open text files. this, go app's info.plist file , add following section:
<key>cfbundledocumenttypes</key> <array> <dict> <key>cfbundletypename</key> <string>text document</string> <key>cfbundletyperole</key> <string>viewer</string> <key>lshandlerrank</key> <string>alternate</string> <key>lsitemcontenttypes</key> <array> <string>public.text</string> </array> </dict> </array> this tell ios app can open text files. where-ever there button says, "open in..." (ex. in safari or mail) , user wants open text file, app show in list.
you'll have handle opening of text file in appdelegate:
-(bool)application:(uiapplication *)application openurl:(nsurl *)url sourceapplication:(nsstring *)sourceapplication annotation:(id)annotation { if (url != nil && [url isfileurl]) { //removes un-needed part of file path file name left nsstring *newstring = [[url absolutestring] substringwithrange:nsmakerange(96, [[url absolutestring] length]-96)]; //send filename user defaults app can file name later nsuserdefaults *defaults = [nsuserdefaults standarduserdefaults]; [defaults setobject:newstring forkey:@"fileurlfromapp"]; //save defaults [[nsuserdefaults standarduserdefaults] synchronize]; //post notification app knows method fire [[nsnotificationcenter defaultcenter] postnotificationname:@"fileurlfromapp" object:nil]; } else { } return yes; } you'll have register notification in viewcontroller.m:
[[nsnotificationcenter defaultcenter] addobserver:self selector:@selector(fileurlfromapp) name:@"fileurlfromapp" object:nil]; then can create method need , retrieve file:
- (void) fileurlfromapp { //get stored file path nsarray *paths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes); nsstring *documentsdirectory = [paths objectatindex:0]; nsuserdefaults *defaults = [nsuserdefaults standarduserdefaults]; nsstring *filepath = [defaults objectforkey:@"fileurlfromapp"]; nsstring *finalfilepath = [documentsdirectory stringbyappendingpathcomponent:filepath]; //parse data //remove "%20" filepath nsstring *strippedcontent = [finalfilepath stringbyreplacingpercentescapesusingencoding:nsutf8stringencoding]; //get data file nsstring* content = [nsstring stringwithcontentsoffile:strippedcontent encoding:nsutf8stringencoding error:null]; the above method give contents of text file in nsstring called content
and should work! best of luck!
Comments
Post a Comment