ãªããå®éã«ïŒ
MacãšiPhoneããæã¡ã§ãã¢ãã€ã«ã¢ããªãäœæããããšããªãã®ã§ããïŒ ã©ããããããééã£ãŠããŸãã 幞ããªããšã«ãããã§åé¡ãæããã«ãªããŸããããã®åé¡ã¯ãããã¯ã«å®å šã«é©åããéåžžã«æçšã§ãããšåæã«å®è£ ããããšãããã»ã©é£ãããããŸããã ããã§ãObjective-CãšCocoaã«æ²¡é ããŸããã
å 責äºé
iOSåãã®æåã®ã¢ããªã±ãŒã·ã§ã³ã ãã§ãªããObjective-Cã®æåã®ã¢ããªã±ãŒã·ã§ã³ãååãšããŠèŠããŠãããŠãã ããã å®è£ ã®å質ãæå¹æ§ã®ãµããããããšã¯ãããŸããããObjective-CãšiOSã®äžè¬çãªéçºã®ã¢ã€ãã¢ãæäŸããéåžžã«å šäœçãªç°¡åãªäŸãåŸããšèšãããã§ãã ç¹ã«ãã®èšèªããŸã£ããç¥ããªã人ã®ããã«ã
å 責äºé 2
ãã®æçš¿ã¯ããšããšãè±èª- ç±³åœãã¶ïŒMy First iPhone Appã® The Pragmatic Bookshelf Magazineã®èšäºãšããŠå ¬éãããŸããã ããã§çºè¡ããããã·ã¢èªçã¯ãéèªçã®æ£ç¢ºãªç¿»èš³ã§ã¯ãããŸãããå°ãåŸã§å¥ã®ããã¹ããšããŠæžãããããã§ãã
ãã¥ãŒã¹ãã³ïŒ åé¡ããããŸãïŒã
éå»1幎éããã³ãã³ã®å€§äœ¿é€šã§ã¢ã¡ãªã«ã®ãã¶ãäœåºŠãç³è«ããªããã°ãªããŸããã§ããã ç¹ã«ç§ã®å Žåãã管çåŠçããå¿ èŠã ãšèšããããã³ã«ã æžé¡ã¯ããªãããåãå ¥ããããŸããããã¶ã®ä»£ããã«ãããçªå·ãäžãã圌ãã¯å®æçã«èªåã®ãŠã§ããµã€ããèŠããšèšããŸãããªã©ïŒã å ¬åŒPDFã®ãªã³ã¯ãã¯ãªãã¯ããŠããã¡ã€ã«ãéããCTRL-FãæŒããŠãçªå·ïŒãããçªå·ïŒãå ¥åããŠããé²ã¿ãŸãã
èªååã®ã¢ã€ãã¢ããããŸãã-iPhoneçšã®ã¢ããªã±ãŒã·ã§ã³ãäœæããŠãã¢ããªã±ãŒã·ã§ã³çªå·ãäžåºŠå ¥åãããšããã¿ã³ã1åã¯ãªãã¯ããã ãã§ãã¶åŠçã¹ããŒã¿ã¹ãååŸã§ããŸãã ã¢ããªã±ãŒã·ã§ã³ã¯ãPDFãã¡ã€ã«ãããŠã³ããŒãããŠè§£æããã¢ããªã±ãŒã·ã§ã³ã®ããŒã¿ãæœåºã§ããå¿ èŠããããŸãã
Windowsãããå Žåã¯ã©ããªããŸããïŒ
ãã¹ãŠã倱ãããããã§ã¯ãããŸããã Objective-Cã¯ãCygwinãŸãã¯MinGWãä»ããŠWindowsã§å®è¡ã§ããŸãã ããã«ã GNUstepãããžã§ã¯ãã¯ãObjective-Cã䜿çšããŠWindowsã§ã°ã©ãã£ã«ã«ããã°ã©ã ãäœæããããã«AppKitããã³Foundationã©ã€ãã©ãªã䜿çšããããšãå¯èœã«ããŸãã æ®å¿µãªããããã®èšäºã§ã¯ããŸãæ·±ãæãäžããŸããã ã³ãã³ãã©ã€ã³ã¢ããªã±ãŒã·ã§ã³ã®ã¿ãäœæããŸãã PDFãããŠã³ããŒãããŠè§£æã§ããããã«ãªããŸãã WindowsãšMacã®äž¡æ¹ã§ã¢ããªã±ãŒã·ã§ã³ããã«ãã§ããŸãã ãã®åŸããã®ã¢ããªã±ãŒã·ã§ã³ã®ã¢ãžã¥ãŒã«ãã»ãšãã©å€æŽããã«äœ¿çšããŠãiOSçšã®æ¬æ Œçãªããã°ã©ã ãäœæããŸãã ããããæ²ããããªãããã¯Macææè ã®ã¿ã察象ã§ãã ãã¡ãããXcodeã§iPhoneã·ãã¥ã¬ãŒã¿ãŒã®ä»®æ³ãã·ã³ã«Hackintoshãã€ã³ã¹ããŒã«ããŠå®è¡ããããšã¯ã§ããŸãããå®éã®iPhoneã«Hackintoshãã¢ããããŒãããããšã¯ãå®éã®Macãªãã§ã¯æåããŸããã
Windowsã«GNUstepãã€ã³ã¹ããŒã«ãã
次ã®2ã€ã®çŽ æŽãããæçš¿ãèŠã€ããŸããã
- ã Windowsã§Objective-CãåŠã¶ ã-GNUstepãã€ã³ã¹ããŒã«ããŠãæå°éã®ã¢ããªã±ãŒã·ã§ã³ãè©Šãæ¹æ³ã
- ã Windowsã§ã®ClangãšObjective-C ã -Windowsçšã®æ°ããClangã³ã³ãã€ã©ãæ§ç¯ããæ¹æ³ã æ®å¿µãªãããGNUstepã§çŸåšå®è¡ãããŠããGCCã¯ãAppleãå¿ èŠãšããObjective-Cèšèªã¬ãã«ããµããŒãããŠããŸããã ããã«ãAppleã¯ãã°ããããŠããClangã«å®å šã«åãæ¿ããŸããã ãã®ãããWindowsçšã®ã€ã³ã¹ããŒã©ãŒããŸã ãªããããClangããã«ãããå¿ èŠããããŸãã æçš¿ãã1察1ã®æ瀺ã«åŸã£ãã ãã§ããã¹ãŠãåé¡ãªãèµ·ããŸããã
Objective-CãšiOS APIã«ç²ŸéããŠããã ããã°å¹žãã§ã
ç§ã¯Objective-Cã«ã€ããŠã¯äœãç¥ããŸããã§ããããã¡ã¢ãªç®¡çã«å¯Ÿãã圌ã®çããã¢ãããŒãã«ã€ããŠã®ããããé€ããŠã次ã®æ¬ãã¹ã¯ããŒã«ããªããã°ãªããŸããã§ããã
èŠå ïŒ ä»¥äžã®ãªã³ã¯ã«ã¯ãAmazonã®å人ã¢ãã£ãªãšã€ãããã°ã©ã çªå·ãå«ãŸããŠããŸãã ãããã®ãªã³ã¯ãã¯ãªãã¯ããåŸã«è¡ãããå¯èœãªè³Œå ¥ãããç§ã¯ããããªå²åãåŸãããšãã§ããŸãã ãããããªãã«åããªãå Žåã¯ããªã³ã¯ãã¯ãªãã¯ããªãã§ãã ããããŸãã¯ãã«ããã¢ã³ãããŒã¹ãã§æåã§URLããã¯ãªãŒã³ãã«ããŠãã ããã ãç解ããã ãããããšãããããŸãã
1. iOSããã°ã©ãã³ã°ïŒThe Big Nerd Ranch Guideã3 / eïŒBig Nerd Ranch GuidesïŒ
![](https://habrastorage.org/getpro/habr/post_images/7cd/aab/1c0/7cdaab1c0c170613f70b43a5c5bec1bf.gif)
![](https://habrastorage.org/getpro/habr/post_images/7cd/aab/1c0/7cdaab1c0c170613f70b43a5c5bec1bf.gif)
2. Objective-Cããã°ã©ãã³ã°ïŒBig Nerd Ranchã¬ã€ãïŒBig Nerd Ranchã¬ã€ãïŒ
![](https://habrastorage.org/getpro/habr/post_images/7cd/aab/1c0/7cdaab1c0c170613f70b43a5c5bec1bf.gif)
![](https://habrastorage.org/getpro/habr/post_images/fe0/1c8/de0/fe01c8de0868e113ad9e86ec46b50696.jpg)
![](https://habrastorage.org/getpro/habr/post_images/7cd/aab/1c0/7cdaab1c0c170613f70b43a5c5bec1bf.gif)
3. Objective-Cã§ã®ããã°ã©ãã³ã°ïŒç¬¬4çïŒïŒéçºè çšã©ã€ãã©ãªïŒ
![](https://habrastorage.org/getpro/habr/post_images/7cd/aab/1c0/7cdaab1c0c170613f70b43a5c5bec1bf.gif)
![](https://habrastorage.org/getpro/habr/post_images/09b/525/071/09b525071dca43e7d118a141e856c59b.jpg)
![](https://habrastorage.org/getpro/habr/post_images/7cd/aab/1c0/7cdaab1c0c170613f70b43a5c5bec1bf.gif)
ãŸããéæ³ã®ãããªç¡æã®ããã¥ã¡ã³ãã1ã€ãããŸã-C ++ããObjective-CãŸã§ ã
ãã®ãããã¿ã¹ã¯ã¯3ã€ã®äž»èŠãªéšåã«åãããŠããŸãã
- ããŒãµãŒpdf
- PDFã®ããŠã³ããŒãïŒã€ã³ã¿ãŒãã§ã€ã¹ã«ãã€ã³ãããã«äœæããããšãæãŸããïŒ
- iOSã®ã€ã³ã¿ãŒãã§ãŒã¹
Objective-Cã«æ £ããåŸãCãŸãã¯C ++ã®çµéšã®å°ãªãéçºè ãç¹ã«UIã®éçºçµéšãããå ŽåïŒä»¥åã¯Delphi / C ++ BuilderãããããåããŠããïŒãObjective- CãšCocoaã¯ç°¡åã§ãã ã¡ã¢ãªç®¡çã®éåžžã«çããåæåã¢ãã«ïŒç¹ã«C ++ã®RAIIãšJavaã®ã¬ããŒãžã³ã¬ã¯ã¿ãŒïŒã«çŠç¹ãåœãŠãã ãã§ååã§ãã Objective-Cã¯ã¡ã¢ãªèªäœã管çããŸãããæ£ãããªãªãŒã¹ã®ããã«ãªããžã§ã¯ããžã®åç §ã®èšç®ãå¶åŸ¡ããã®ã¯ããªã次第ã§ãã ååãç解ããå¿ èŠããããŸããããããªããšãã¡ã¢ãªãªãŒã¯ãé¿ããããŸããã ããã¯ãŸãã«ç§ãæåã«æã£ãŠãããã®ã§ããã 幞ããªããšã«ãXcodeã®åªãããããã¡ã€ãªã³ã°ããŒã«ã䜿çšãããšãäž»ãªåé¡ãã»ãŒå³åº§ã«ç¹å®ã§ããŸãã
以äžã«ãObjective-CãšCocoaã®åå¿è ãšããŠã®å人çãªäž»èŠ³çãªå°è±¡ã瀺ããŸãã æ¢ã«çµéšãããã°é¢çœããšã¯æããŸããããããã§ãªãå Žåã¯é¢çœããšæããŸãã
ãŸããã¯ã©ã¹ã®ã¡ã³ããŒé¢æ°ã®ååãObjective-Cã§ã©ã®ããã«åœ¢æãããããèŠãã®ã¯èå³æ·±ãã§ãã ããã¯ã»ãšãã©äººéã®èšèªã«äŒŒãŠããŸãã è±èªã§èšããšããããŒã¿ã®äžéšã§éãèŠã€ããŠãå¯å€é åãšããŠå®è£ ããããªã¹ãã«çµæãè¿œå ããŠãã ããããšèšããšãObjective-Cã§ã¯æ¬¡ã®ããã«ãªããŸãã
+ (bool)findInPortion:(NSMutableData *)someData needle:(NSString*)aNeedle andAddTo:(NSMutableArray*)aList { ... }
ãã®ã³ãŒããå·Šããå³ã«äžããäžã«èªããšãã»ãŒå®å šãªæãåŸãããŸãã æ£åŒã«ã¯ããã®ã¡ãœããã®ãã«ããŒã ã¯
findInPortion:needle:andAddTo:
åŒæ°ã«ã¯ååãä»ãããããã®ååã¯ã¡ãœããã®ãã«ããŒã ã®äžéšã§ãã å€æ°åŒæ°ã®ååïŒ
someData
ã
aNeedle
ãããã³
aList
ïŒã
aNeedle
ãšãå®éã«è±èªã§æžãããšãã§ããŸãã ãã¡ãããããã¯ã©ã¡ãããšããã°ãåé·ããªã¢ãããŒãã§ãããã³ãŒããå ¥åããéã®Xcodeã®çŽ æŽãããäºæž¬ã·ã¹ãã ã«ãããããããã¹ãŠã®ã¿ãŒã³ããã°ããç°¡åã«ç«ã¡äžããããšãã§ããŸãã ãŸããé·ãè¡ãåå²ãããšãã®åŸæ¥ã®é 眮ã¯ããã©ã¡ãŒã¿ã®æ£åŒåãšãããè¡šãå€æ°ãåºåãã³ãã³ã§çºçããããšã«æ³šæããŠãã ããã
Objective-Cã«ã¯ãã¡ãœãããåŒã³åºãããã®åç Žããªæ§æããããŸãã ããšãã°ã次ã®ä»£ããã«ïŒ
NSMutableArray* list = NSMutableArray.alloc.init;
ããã¯æžãããŠããŸãïŒ
NSMutableArray* list = [[NSMutableArray alloc] init];
å¥åŠã«èŠããŸãããããã¯ç¿æ £ã®åé¡ã§ãã ç¹°ãè¿ããŸãããå ¥åããã³ãŒãäºæž¬ã·ã¹ãã ã䜿çšãããšãè§æ¬åŒ§ãç©ççã«è©°ã蟌ãããšãªãå ¥åã§ããŸãã
Objective-Cããã³Cocoaã¯ãç¿åŸããå¿ èŠãããããã°ã©ãã³ã°ãã¿ãŒã³ãç©æ¥µçã«äœ¿çšããŸãã ããšãã°ãããªã²ãŒãã Cocoaã®ã©ãã«ã§ããããŸãã ããªã²ãŒãã¯ãã³ãŒã«ããã¯ãå«ãã¯ã©ã¹ã§ãã äžç·ã«ãåã ã®é¢æ°ãŸãã¯ã¡ãœããã®ãã³ãã«ãæž¡ããšãå¿ èŠãªãã¹ãŠã®ã³ãŒã«ããã¯ãå®è£ ãããªããžã§ã¯ãã1ã€ã ãæž¡ãããŸãã ããšãã°ãæšæºã®NSURLConnectionã¯ã©ã¹ã䜿çšããŠPDFãããŠã³ããŒãããŸããã ãã®ã¯ã©ã¹ã«ã¯NSURLConnectionDelegateããªã²ãŒãã®ããããžã§ãã³ã°ãå¿ èŠã§ããããªã²ãŒãã®ã¡ãœããã¯ãããŠã³ããŒãããã»ã¹äžã«ããŸããŸãªã€ãã³ããçºçãããšåŒã³åºãããŸãã
ããã§ãæ°é±éã®å€ã®æ¬ã®åŸãã®åŸ¹å€ã§ãæåã®ã¢ããªã±ãŒã·ã§ã³ã®ã¹ã±ã«ãã³ãã¹ã±ããããŸããã ããããããã¯Marlezonãã¬ãšã®æåã®éšåã«ãããŸããã§ããã 次ã«ãPDF圢åŒãæ±ãå¿ èŠããããŸããã
ããŒãµãŒpdf
ãã§ã«è¿°ã¹ãããã«ã倧䜿通ããã®æ å ±ãå«ããã¡ã€ã«ã¯PDF圢åŒã§ãã ãã®åœ¢åŒã®èª¬æã¯ãAdobe Webãµã€ãã§å ¥æã§ããŸãã ç§ã¯ããã¥ã¡ã³ãã PDF Reference 3çãããŒãžã§ã³1.4 ãã䜿çšããŸããã
PDF解æã¯ãç§ã®éåžžã«ã³ã³ãããã¢ã ã«å®è£ ãããŠããŸãã ããŒã¿ã¯éšåçãªãã®ã§ãããããããã¥ã¡ã³ããéšåçã«åæããŸãã æ°ããããŒã¿ããããã¡ã«è¿œå ãããã®äžã®PDF圢åŒã解æããããšããŸãã ãŸãã
stream
endstream
ãš
endstream
stream
endstream
å²ãŸãããã©ã°ã¡ã³ããæ¢ããŸãã ãã®ãããªåãããã¯ã®å 容ã¯ã
zlib/inflate
ä»ããŠ
zlib/inflate
ã§ãã ãã®åŸãããã¯ãã§ã«çŽç²ãªããã¹ãã§ããããã¡ããPDFããŒã¯ã¢ããèšèªãèæ ®ããŠããããçªå·ãæ¢ããŠããŸãã çªå·ãèŠã€ãã£ããããããå°å·ããŠæ¬¡ã®ãããã¯ã«é²ã¿ãŸãã
ããŒãµãŒã®äž»ãªæé ïŒ
-
stream\r\n
ããã³endstream\r\n
ã¿ã°ã«ãã£ãŠå¶éããããçŸæç¹ã§åä¿¡ããããŒã¿ã«ãããã¯ãããå Žåããããã¡ãŒããåãåããzlib/inflate
ä»ããŠãå±éãããŸãã - æåã®ã¹ãããã§éãããŠããªããããã¯ã¯ããã¹ãã§ãã ã¿ã°
BT\r\n
ïŒããã¹ãã®éå§ïŒããã³ET\r\n
ïŒããã¹ãã®çµäºïŒã§å²ãŸãããã©ã°ã¡ã³ããèŠã€ããå¿ èŠããããŸãã ãã®ãããªãããã¯ããã¹ãŠèŠã€ããããããçµã¿åãããŠæååã®ãªã¹ãã«ããŸãã - æé 2ã§èŠã€ãã£ãåè¡å ã§ããã£ãã§å²ãŸããŠããªãéšåæååãåé€ããŸãã æ¬åŒ§ã§å²ãŸããŠããã®ã¯ãµãŒãã¹æ å ±ã ãã§ãããå¿ èŠãããŸããã
- ãã®ãããPDFãããã¬ãŒã³ããã¹ããæœåºããŸããã è«ççã«ã¯ããã®ãã¡ã€ã«ã®æ å ±ã¯ãã¢ããªã±ãŒã·ã§ã³çªå·ïŒãããçªå·ïŒãã¹ããŒã¿ã¹ãæ¥ä»ã®3ã€ã®åãæã€ããŒãã«ã«ç·šæãããŠããŸãã æ²ããããªããã®äžã§ããããŒãžããããŒã¯ãŸã åºããããŠããŸãã ããããåãé€ãããã«ãçŸåšã®è¡ããããçªå·ïŒ11æ¡ïŒã®ããã«èŠããå Žåãã¹ããŒã¿ã¹è¡ãšæ¥ä»è¡ãåŸã«ç¶ãããã«èŠããŸãã ããããåãåããåã³æ°ãããããçªå·ãåŸ ã£ãŠããŸãã
ç§ãèšã£ãããã«ãåæã¯ç¹å®ã®ãã¡ã€ã«ã«åãããŠèª¿æŽãããŠããã倧䜿通ããããå€æŽãããšããã¹ãŠãå£ããŸãã æ£èŠè¡šçŸã䜿çšããå Žåã§ããã¯ããã«æè»ã«ãªããŸãããèªè ãèªåã§è§£æ±ºã§ããããã«ããŠãããŸãã
è¿œå ã ãã®èšäºã«åãçµãéçšã§ãç¹å¥ãªWebãµãŒãã¹ãäœæããç°¡åãªURLã䜿çšããŠã¢ããªã±ãŒã·ã§ã³ããŒã¿ãåä¿¡ã§ããã¢ã¯ã»ã¹ããPDFã解æããããã®ããããã³ãå šäœããã¯ã©ãŠããã§è¡ããããšããã¢ã€ãã¢ãçãŸããŸããã ããå士ã¯æè¿ããã®ã¢ãããŒãã説æããèšäºãGoogle App Engineã«ããGoã®RESTful WebãµãŒãã¹ ããå ¬éããŸããã èå³ã®ããæ¹ã¯ããã®WebãµãŒãã¹ãä»ããŠåäœããããã«ã¢ããªã±ãŒã·ã§ã³ããä»äžãããŸãã äžè¬çã«ã¯ããªãããŒã§ããæåã«ãWebãµãŒãã¹ã«ã¢ã¯ã»ã¹ããããããåçãããå Žåã¯ããã§çµäºããããã§ãªãå Žåã¯ãPDFãããŠã³ããŒãããŠè§£æããæé ãèªåã§éå§ããŸãã
ã³ãã³ãã©ã€ã³ã¢ããªã±ãŒã·ã§ã³
ãã®ãããPDFãããŠã³ããŒãããŠã¢ããªã±ãŒã·ã§ã³ããæ å ±ãæœåºããã¢ããªã±ãŒã·ã§ã³ãäœæããã»ãšãã©ãã¹ãŠã®ããšãç¥ã£ãŠããŸãã ã¢ããªã±ãŒã·ã§ã³ã¯ã³ãã³ãã©ã€ã³ããåäœããŸãã GNUstepããã³Clangãä»ããŠMacããã³Windowsããæ§ç¯ã§ããŸãã ããã«ããã®ã¢ããªã±ãŒã·ã§ã³ã®ãœãŒã¹ãã¡ã€ã«ã¯ãiOSã®ããŒãžã§ã³ãå€æŽããã«äœ¿çšãããŸãã
ãã¡ã€ã«ïŒ
-
BatchPDFParser.m
ïŒããã³.h
ïŒBatchPDFParser.m
ããŒãµãŒã -
NSURLConnectionDirectDownload.m
ïŒããã³.h
ïŒ-ããŠã³ããŒããNSURLConnection
ïŒåæåãããªã²ãŒããã€ãã³ãã«ãŒãïŒã«ã¯ãããžã£ãŒãããããŸãã -
DirectDownloadDelegate.m
ïŒããã³.h
ïŒ-ããŠã³ããŒãã®ããŸããŸãªãã€ã³ãã§åŒã³åºããåãå ¥ããNSURLConnection
ã®ããªã²ãŒãã -
ViewController.m
ã¯ãViewController.m
ã®ãããã¿ã€ãã§ãã ããã¯ãããŠã³ããŒããšå°æ¥ã®ã°ã©ãã£ã«ã«ã€ã³ã¿ãŒãã§ã€ã¹ã®éã®ãã¬ã€ã€ãŒãã§ãã OSXããã³iOSã¯ãMVCïŒModel-View-ControllerïŒã®æŠå¿µã䜿çšããŸãã ãã³ã³ãããŒã©ãã¯ãã€ã³ã¿ãŒãã§ã€ã¹èŠçŽ ãšã¢ããªã±ãŒã·ã§ã³ã®ããžãã¹ããžãã¯éã®ãªã³ã¯ãæäŸããŸãã çŸåšã®ã³ã³ãããŒã©ãŒã«ã¯ãäž»ã«å®å šãªã°ã©ãã£ã«ã«ããŒãžã§ã³ã§å®è£ ãããã¹ã¿ããå«ãŸããŠããŸãã -
main-cli.m
ãšã³ããªãã€ã³ãã
BatchPDFParser.h
ãã®ãã¡ã€ã«ã«ã¯ãã¢ããªã±ãŒã·ã§ã³ã®ã¹ããŒã¿ã¹ã®æŽæ°ã«é¢ããæ å ±ãå«ã
Batch
ã¯ã©ã¹ã®å®£èšãšã
findInPortion:needle:andAddTo:
ã¡ãœãããå®è£ ãã
findInPortion:needle:andAddTo:
ã¯ã©ã¹ãå«ãŸããŠããŸãïŒã¡ãªã¿ã«ãããã¯éçã¯ã©ã¹ã¡ãœããã§ããè¡
+
å é ãåç §ããŠãã ããïŒã
@interface Batch: NSObject { NSString *batchNumber, *status, *date; } @property (atomic, copy) NSString* batchNumber, *status, *date; @end @interface BatchPDFParser: NSObject + (bool)findInPortion:(NSMutableData *)data needle:(NSString* const)needle andAddTo:(NSMutableArray*)list; @end
BatchPDFParser.m
ãã®ãã¡ã€ã«ã¯ããŒãµãŒPDFãå®è£ ããŸãã
#import <Foundation/Foundation.h> #import "BatchPDFParser.h" #import "zlib.h" @implementation Batch @synthesize batchNumber, status, date; - (void) dealloc { [batchNumber release]; [status release]; [date release]; [super dealloc]; } @end @implementation BatchPDFParser
findInData:fromOffset:needle:
ã¡ãœããã¯ãïŒ
strstr()
åã®
strstr()
ç¹å®ã®ããŒã¿ãããã¯å ã®ãµãã¹ããªã³ã°ãæ€çŽ¢ããŸãã æ€çŽ¢ã¯ããªããã£ãã§ãããããšãã°ILCã¢ã«ãŽãªãºã ãå®è£ ããããšã§é«éåã§ããŸãã
+ (int) findInData:(NSMutableData *)data fromOffset:(size_t)offset needle:(char const * const)needle { int const needleSize = strlen(needle); char const* const bytes = [data mutableBytes]; int const bytesLength = [data length] - needleSize; for (int i = 0; i < bytesLength;) { char const* const current = memchr(bytes + i, needle[0], bytesLength - i); if (current == NULL) return -1; if (memcmp(current, needle, needleSize) == 0) return current - bytes; i = current - bytes + 1; } return -1; }
isBatchNumber:number:
ã¡ãœããã¯ãæååããããçªå·ãã©ããã確èªããŸãïŒ
+ (bool) isBatchNumber:(NSString*)number { long long const value = [number longLongValue]; return value >= 20000000000L && value < 29000000000L; }
findBatchNumberInChunk:needle:andAddTo:
ã¯ã
BT
ããã³
ET
ã¿ã°ã§å²ãŸãããã©ã°ã¡ã³ããæ€çŽ¢ããŸãã ã«ãã³å ã®ããã¹ãã¯ãã€ã©ã€ããããèŠã€ãã£ããã®ã®äžã§ç¹ã«ã¢ããªã±ãŒã·ã§ã³çªå·ãã¹ããŒã¿ã¹æååãæ¥ä»æååããã€ã©ã€ããããŸãã
+ (bool) findBatchNumberInChunk:(char const*)chunk needle:(NSString*)needle andAddTo:(NSMutableArray*)list { enum { waitBT, waitText, insideText } state = waitBT; enum { waitBatchNumber, waitStatus, waitDate } batchParserState = waitBatchNumber; NSMutableString* line = [[NSMutableString alloc] init]; Batch* batch = nil; bool found = NO; while (*chunk) { if (state == waitBT) { if (chunk[0] == 'B' && chunk[1] == 'T') { state = waitText; [line deleteCharactersInRange:NSMakeRange(0, [line length])]; } } else if (state == waitText) { if (chunk[0] == '(') { state = insideText; } else if (chunk[0] == 'E' && chunk[1] == 'T') { if (batchParserState == waitBatchNumber) { if ([self isBatchNumber:line]) { [batch autorelease]; batch = [[Batch alloc] init]; batch.batchNumber = line; batchParserState = waitStatus; } } else if (batchParserState == waitStatus) { batch.status = line; batchParserState = waitDate; } else if (batchParserState == waitDate) { batch.date = line; batchParserState = waitBatchNumber; if ([batch.batchNumber isEqualToString:needle]) { NSString* pair = [NSString stringWithFormat:@"%@\n%@", batch.status, batch.date]; [list addObject:pair]; NSLog(@"Found match: '%@' '%@' '%@'", batch.batchNumber, batch.status, batch.date); found = YES; } } [line autorelease]; line = [[NSMutableString alloc] init]; state = waitBT; } } else if (state == insideText) { if (chunk[0] == ')') { state = waitText; } else { char const c[2] = { chunk[0], 0 }; [line appendString:[NSString stringWithUTF8String:&c[0]]]; } } chunk += 1; } [line release]; [batch release]; return found; }
çŸåšãäž»ãªã¡ãœããã¯
findInPortion:needle:andAddTo:
ããã§ã¯ã
stream\r\n
ããã³
endstream\r\n
ã¿ã°ã§å²ãŸãããã©ã°ã¡ã³ãã匷調衚瀺ãããå 容ã
zlib/inflate
ãä»ããŠå±éãããåæã®ããã«
findBatchNumberInChunk:needle:andAddTo:
ã«
findBatchNumberInChunk:needle:andAddTo:
ããŸãã
+ (bool)findInPortion:(NSMutableData *)portion needle:(NSString*)needle andAddTo:(NSMutableArray*)list { static char const* const streamStartMarker = "stream\x0d\x0a"; static char const* const streamStopMarker = "endstream\x0d\x0a"; bool found = false; while (true) { int const beginPosition = [self findInData:portion fromOffset:0 needle:streamStartMarker]; if (beginPosition == -1) break; int const endPosition = [self findInData:portion fromOffset:beginPosition needle:streamStopMarker]; if (endPosition == -1) break; int const blockLength = endPosition + strlen(streamStopMarker) - beginPosition; char const* const zipped = [portion mutableBytes] + beginPosition + strlen(streamStartMarker); z_stream zstream; memset(&zstream, 0, sizeof(zstream)); int const zippedLength = blockLength - strlen(streamStartMarker) - strlen(streamStopMarker); zstream.avail_in = zippedLength; zstream.avail_out = zstream.avail_in * 10; zstream.next_in = (Bytef*)zipped; char* const unzipped = malloc(zstream.avail_out); zstream.next_out = (Bytef*)unzipped; int const zstatus = inflateInit(&zstream); if (zstatus == Z_OK) { int const inflateStatus = inflate(&zstream, Z_FINISH); if (inflateStatus >= 0) { found = found || [BatchPDFParser findBatchNumberInChunk:unzipped needle:needle andAddTo:list]; } else { NSLog(@"inflate() failed, error %d", inflateStatus); } } else { NSLog(@"Unable to initialize zlib, error %d", zstatus); } free(unzipped); inflateEnd(&zstream); int const cutLength = endPosition + strlen(streamStopMarker); [portion replaceBytesInRange:NSMakeRange(0, cutLength) withBytes:NULL length:0]; } return found; } @end
DirectDownloadViewDelegate.h
NSURLConnectionDelegate
ããªã²ãŒãããããŒïŒ
@protocol DirectDownloadViewDelegate<NSObject> - (void)setProgress: (float)progress; - (void)appendStatus: (NSString*)status; - (void)setCompleteDate: (NSString*)date; @end
DirectDownloadDelegate.h
å®éãããªã²ãŒãèªäœã¯
NSURLConnectionDelegate
ã
#import "DirectDownloadViewDelegate.h" @interface DirectDownloadDelegate : NSObject { NSError *error; BOOL done; BOOL found; NSMutableData *receivedData; float expectedBytes, receivedBytes; id<DirectDownloadViewDelegate> viewDelegate; NSString* needle; } - (id) initWithNeedle:(NSString*)aNeedle andViewDelegate:(id<DirectDownloadViewDelegate>)aViewDelegate; @property (atomic, readonly, getter=isDone) BOOL done; @property (atomic, readonly, getter=isFound) BOOL found; @property (atomic, readonly) NSError *error; @end
DirectDownloadDelegate.m
ãããŠãã®å®è£ ïŒ
#import <Foundation/Foundation.h> #import "DirectDownloadDelegate.h" #import "BatchPDFParser.h" @implementation DirectDownloadDelegate @synthesize error, done, found;
ã³ã³ã¹ãã©ã¯ã¿ãŒ
initWithNeedle:andViewDelegate:
ã¯ãããªã²ãŒããäœæããç»é¢æŽæ°ã¿ã¹ã¯ã«äœ¿çšãããå¥ã®ããªã²ãŒã
DirectDownloadViewDelegate
ã§ãã©ã¡ãŒã¿ãŒåããŸãã ããã§ããšããã§ãæåã«ãã¹ãã©ã¯ã¿
(void) dealloc:
- (id) initWithNeedle:(NSString*)aNeedle andViewDelegate:(id<DirectDownloadViewDelegate>)aViewDelegate { viewDelegate = aViewDelegate; [viewDelegate retain]; needle = [[NSString alloc] initWithString:aNeedle]; receivedData = [[NSMutableData alloc] init]; expectedBytes = receivedBytes = 0.0; found = NO; return self; } - (void) dealloc { [error release]; [receivedData release]; [needle release]; [viewDelegate release]; [super dealloc]; }
connectionDidFinishLoading:
ã¡ãœããã¯ãæ¥ç¶ãå®äºãããšåŒã³åºãããŸãã
- (void) connectionDidFinishLoading:(NSURLConnection *)connection { done = YES; NSLog(@"Connection finished"); }
connection:didFailWithError:
ã¡ãœããã¯ããã¡ã€ã«ã®ããŠã³ããŒãæã«ãšã©ãŒãã¹ããŒããŸãã
- (void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)anError { error = [anError retain]; [self connectionDidFinishLoading:connection]; }
connection:didReceiveData:
ããã£ãã«ããæ°ããããŒã¿ãåä¿¡ããããšåŒã³åºãããŸãã ãã®ãããªåéšåããããã¡ãŒã«è¿œå ããããŠã³ããŒãã®é²è¡ç¶æ³ã€ã³ãžã±ãŒã¿ãŒãæŽæ°ããŠïŒå¥ã®ããªã²ãŒãã§ãã
viewDelegate
ãä»ããŠïŒãããŒã¿ãã©ã°ã¡ã³ããPDF圢åŒã§åé¢ããæåŸã«èŠã€ãã£ããã®ãå°å·ããŸãã
- (void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)someData { receivedBytes += [someData length]; [viewDelegate setProgress:(receivedBytes / expectedBytes)]; [receivedData appendData:someData]; NSMutableArray* list = [[NSMutableArray alloc] init]; bool foundInCurrentPortion = [BatchPDFParser findInPortion:receivedData needle:needle andAddTo:list]; for (id batch in list) { NSLog(@"[%@]", [batch stringByReplacingOccurrencesOfString:@"\n" withString:@"\\n"]); [viewDelegate appendStatus:batch]; } [list release]; found = found || foundInCurrentPortion; }
䜿çšããæåŸã®
NSURLConnectionDelegate
ããªã²ãŒã
NSURLConnectionDelegate
ã¯ã
connection:didReceiveResponse:
ãšåŒã°ã
connection:didReceiveResponse:
ããããŒãå«ãHTTPå¿çãåä¿¡ããããšãã«åŒã³åºãããŸãã ãContent-LengthãããããŒããå°æ¥ã®ãã¡ã€ã«ã®é·ããååŸããŠãåŸã§ããŠã³ããŒãã€ã³ãžã±ãŒã¿ãŒãæŽæ°ã§ããããã«ããŸãã
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSHTTPURLResponse *)someResponse { NSDictionary *headers = [someResponse allHeaderFields]; NSLog(@"[didReceiveResponse] response headers: %@", headers); if (headers) { if ([headers objectForKey: @"Content-Length"]) { NSLog(@"Content-Length: %@", [headers objectForKey: @"Content-Length"]); expectedBytes = [[headers objectForKey: @"Content-Length"] floatValue]; } else { NSLog(@"No Content-Length header found"); } } }
NSURLConnectionDirectDownload.h
ãã®ãã¡ã€ã«ã«ã¯ã
NSURLConnection
ã¯ã©ã¹ã«è¿œå ãã
donwloadAtURL:searching:viewingOn:
ãå«ãŸããŠããŸãã ããã§èå³æ·±ãã®ã¯ãObjective-Cã®ã«ããŽãªã®æŠå¿µãéããŠãæ°ããã¡ãœãããæ¢åã®ã¯ã©ã¹ãšãããã¯ã¹ãã§ããããšã§ãã ããã§ã¯ã
DirectDownload
ã«ããŽãªãè¿œå ããŸãã
@interface NSURLConnection (DirectDownload) + (BOOL) downloadAtURL:(NSURL *)url searching:(NSString*)batchNumber viewingOn:(id)viewDelegate; @end
NSURLConnectionDirectDownload.m
ããŠãPDFããŠã³ããŒãã®æåŸã®éšåã§ãã
donwloadAtURL:searching:viewingOn:
ã¯æ¥ç¶ãäœæããããŠã³ããŒããéå§ããŸãã 次ã«ãããŠã³ããŒããå®äºãããŸã§
NSRunLoop
ã«ãŒãã§åŸ æ©ããŸãã ãã®ã«ãŒãã«ãããã¢ããªã±ãŒã·ã§ã³ã¯ããŠã³ããŒãããã»ã¹äžã®ã€ãã³ãã«å¿çã§ããŸãã ãã®ããŠã³ããŒãã¯ãŸã GUIã«é¢é£ä»ããããŠããªãããšã«æ³šæããŠãã ããã
viewDelegate
ããªã²ãŒãã䜿çšããŠãã¢ããªã±ãŒã·ã§ã³ã®ãé¡ããšéä¿¡ããŸãã
#import <Foundation/Foundation.h> #import "DirectDownloadDelegate.h" @implementation NSURLConnection (DirectDownload) + (BOOL) downloadAtURL:(NSURL *)url searching:(NSString*)batchNumber viewingOn:(id)viewDelegate { NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url]; DirectDownloadDelegate *delegate = [[[DirectDownloadDelegate alloc] initWithNeedle:batchNumber andViewDelegate:viewDelegate] autorelease]; NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:delegate]; [request release]; while ([delegate isDone] == NO) { [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:1.0]]; } if ([delegate isFound] != YES) { [viewDelegate appendStatus:@"This batch number is not found."]; NSLog(@"This batch number is not found."); } NSLog(@"PDF is processed"); [connection release]; NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init]; dateFormatter.dateFormat = @"yyyy/MM/dd HH:mm:ss"; NSString* lastUpdateDate = [dateFormatter stringFromDate:[NSDate date]]; NSLog(@"Last update at: %@", lastUpdateDate); [viewDelegate setCompleteDate:lastUpdateDate]; [dateFormatter release]; NSError *error = [delegate error]; if (error != nil) { NSLog(@"Download error: %@", error); return NO; } return YES; } @end
ViewController.m
æ¢ã«è¿°ã¹ãããã«ãã³ãã³ãã©ã€ã³ã¢ããªã±ãŒã·ã§ã³ã§ã¯ãã³ã³ãããŒã©ãŒã«ã¯ã¹ã¿ãã®ã¿ãå«ãŸããŸããããã¯ãããã°ã©ã ã®ãã«ããŒãžã§ã³ã§åŸã§å®è£ ããŸãã
#import <Foundation/Foundation.h> #import "DirectDownloadViewDelegate.h" #define IBAction void
空ã®
ViewController
ã¹ã¿ã
ViewController
ã
@interface ViewController : NSObject <DirectDownloadViewDelegate> @end #import "NSURLConnectionDirectDownload.h"
ãã¡ã€ã«ãããŠã³ããŒãããã¢ãã¬ã¹ã
static char const* const pdf = "http://photos.state.gov/libraries/unitedkingdom/164203/cons-visa/admin_processing_dates.pdf";
ãããŠãã³ã³ãããŒã©ãŒã¯ã©ã¹ã®ã¢ãã¯å®è£ ã
@implementation ViewController
appendStatus:
ãã¹ãã³ãŒã«ããã¯
appendStatus:
次ã®èŠæ±ã®æŽæ°ãæ€åºããããšãã«åŒã³åºãããŸãã ããã§ãã°ã€ã³ããã ãã§ãã¢ããªã±ãŒã·ã§ã³å šäœã§ç»é¢ãã©ãŒã ãæŽæ°ããŸãã
- (void) appendStatus:(NSString*)status { NSLog(@"appendStatus(): '%@'", [status stringByReplacingOccurrencesOfString:@"\n" withString:@"\\n"]); // Some code is skipped here because not required for the command line mode. }
setProgress:
ãã¹ãã³ãŒã«ããã¯
setProgress:
ããŒã¿ã®æ¬¡ã®ããããåãå ¥ããåŸã«ããŠã³ããŒãã€ã³ãžã±ãŒã¿ãŒãæŽæ°ããå¿ èŠããããšãã«åŒã³åºãããŸãã
- (void) setProgress:(float)progress { // Some code is skipped here because not required for the command line mode. }
setCompleteDate:
ãã¹ãã³ãŒã«ããã¯
setCompleteDate:
PDFåæãå®äºãããš
setCompleteDate:
ãŸãã ããã§ãããã°ã€ã³ããã ãã§ãã
- (void) setCompleteDate:(NSString*)date { NSLog(@"setCompleteDate(): '%@'", date); // Some code is skipped here because not required for the command line mode. }
ããŠããã¹ãŠãå®è¡ããæåŸã®ã¡ãœããã¯
updateBatchStatus:
å®å šãªããã°ã©ã ã§ã¯ããã©ãŒã äžã®ãã¿ã³ãã¯ãªãã¯ãããšåŒã³åºãããŸãã ããã§ã¯ã
main()
ããåŒã³åºãããŸãã
- (bool) updateBatchStatus:(NSString*)batchNumber { NSURL *url = [[[NSURL alloc] initWithString:[NSString stringWithCString:pdf encoding:NSASCIIStringEncoding]] autorelease]; return [NSURLConnection downloadAtURL:url searching:batchNumber viewingOn:self]; } @end
main-cli.m
ã³ãã³ãã©ã€ã³ããå®è¡ããŸãã
#import <Foundation/Foundation.h> #import "DirectDownloadDelegate.h" @interface ViewController : NSObject <DirectDownloadViewDelegate> - (bool) updateBatchStatus:(NSString*)batchNumber; @end int main(int argc, char *argv[]) { @autoreleasepool { ViewController* viewController = [ViewController alloc]; [viewController updateBatchStatus:[NSString stringWithCString:argv[1] encoding:NSASCIIStringEncoding]]; [viewController release]; } return 0; }
ããããã¹ãŠåéããŠå®è¡ããŠã¿ãŸãããã
Macçšã®
Makefile
ïŒ
files = \ ViewController.m \ BatchPDFParser.m \ NSURLConnectionDirectDownload.m \ DirectDownloadDelegate.m main-cli.m all: build run build: clang -o USVisaTest -DTESTING -framework Foundation -lz $(files) run: ./USVisaTest 20121456171
GNUstepã®
GNUmakefile
makefileïŒ
include $(GNUSTEP_MAKEFILES)/common.make TOOL_NAME = USVisa USVisa_OBJC_FILES = \ ../ViewController.m \ ../BatchPDFParser.m \ ../NSURLConnectionDirectDownload.m \ ../DirectDownloadDelegate.m \ ../main-cli.m USVisa_TOOL_LIBS = -lz ADDITIONAL_OBJCFLAGS = -DTESTING CC = clang include $(GNUSTEP_MAKEFILES)/tool.make run: ./obj/USVisa 20121456171
make
ãšå ¥åã
make
ã WindowsïŒ
This is gnustep-make 2.6.2. Type 'mmake print-gnustep-make-help' for help. Making all for tool USVisa... Creating obj/USVisa.obj/../... Compiling file ViewController.m ... Compiling file BatchPDFParser.m ... Compiling file NSURLConnectionDirectDownload.m ... Compiling file DirectDownloadDelegate.m ... Compiling file main-cli.m ... Linking tool USVisa ...
å®è¡ããŠãå®éã®ã¢ããªã±ãŒã·ã§ã³ã確èªã§ããŸãã
make run
ç§ã¯æ¬¡ã®ãã®ãæã«å ¥ããŸããïŒ
This is gnustep-make 2.6.2. Type 'mmake print-gnustep-make-help' for help. ./obj/USVisa 20121456171 2012-06-19 17:27:11.472 USVisa[3420] [didReceiveResponse] response headers: {"Accept-Ranges" = bytes; "Cache-Control" = "max-age=600"; Connection = "keep-alive"; "Content-Length" = 2237242; "Content-Type" = "application/pdf"; Date = "Tue, 19 Jun 2012 16:27:11 GMT"; ETag = "\"4b2ca3e41de5ba4ae45670e776edfc3b:1339778351\""; "Last-Modified" = "Fri, 15 Jun 2012 16:06:15 GMT"; Server = Apache; } 2012-06-19 17:27:11.604 USVisa[3420] Content-Length: 2237242 2012-06-19 17:27:12.093 USVisa[3420] Found match: '20121456171' 'send passport & new travel itinerary' '14-Jun-12' 2012-06-19 17:27:12.104 USVisa[3420] [send passport & new travel itinerary\n14-Jun-12] 2012-06-19 17:27:12.111 USVisa[3420] appendStatus(): 'send passport & new travel itinerary\n14-Jun-12' 2012-06-19 17:27:13.769 USVisa[3420] Connection finished 2012-06-19 17:27:13.774 USVisa[3420] PDF is processed 2012-06-19 17:27:13.961 USVisa[3420] Last update at: 2012/06/19 16:27:13 2012-06-19 17:27:13.972 USVisa[3420] setCompleteDate(): '2012/06/19 16:27:13'
ãããã£ãŠããã¹ãŠãæ©èœããŸãïŒPDFã®ããŠã³ããŒããšããŒãµãŒã ããã§ã¯ãiOSããŒãžã§ã³ã«åãæãããŸãããã æ²ããããªãMacãŠãŒã¶ãŒã®ã¿ã
ç»é¢ã¬ã€ã¢ãŠã
ã¢ããªã±ãŒã·ã§ã³ãéåžžã«ã·ã³ãã«ã«ããŸãããå ¥åãã£ãŒã«ãããã¿ã³ãããã³æŽæ°ã衚瀺ããå Žæãæã€1ã€ã®ãã©ãŒã ã§ãã
![](http://demin.ws/images/blog/usvisa/app/usvisa-application-screenshot.png)
ããŠã³ããŒãã€ã³ãžã±ãŒã¿ãšå転ã¹ã©ã€ããŒãäžæçã«è¡šç€ºãããŸãã
ViewController.h
ããã§ãã³ã³ãããŒã©ãŒãå®å šã«å®è£ ãããŸããã
TESTING
ãã¯ããéããŠãç°¡æçãšå®å šçãåºå¥ããŸããã
#import <Foundation/Foundation.h> #import "DirectDownloadViewDelegate.h" #ifdef TESTING #define IBAction void @interface ViewController : NSObject <DirectDownloadViewDelegate> @end #else #import "ViewController.h" #endif #import "NSURLConnectionDirectDownload.h" static char const* const pdf = "http://photos.state.gov/libraries/unitedkingdom/164203/cons-visa/admin_processing_dates.pdf"; @implementation ViewController #ifndef TESTING @synthesize updateProgressView, batchNumberTextField, statusTextView, lastUpdatedLabel, updateButton; #endif NSString* const PropertiesFilename = @"Properties"; NSString *pathInDocumentDirectory(NSString *fileName) { NSArray *documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentDirectory = [documentDirectories objectAtIndex:0]; return [documentDirectory stringByAppendingPathComponent:fileName]; }
appendStatus:
ãžã®ã³ãŒã«ããã¯
appendStatus:
ãã°ãèšé²ããã ãã§ãªããç»é¢ãã©ãŒã ãæŽæ°ããŸãã
- (void) appendStatus:(NSString*)status { NSLog(@"appendStatus(): '%@'", [status stringByReplacingOccurrencesOfString:@"\n" withString:@"\\n"]); #ifndef TESTING if ([[statusTextView text] length] == 0) [statusTextView setText:@"Status:\n"]; [statusTextView setText:[[statusTextView text] stringByAppendingString:status]]; [statusTextView setText:[[statusTextView text] stringByAppendingString:@"\n"]]; #endif }
setProcess:
ããŠã³ããŒãã€ã³ãžã±ãŒã¿ãŒãæŽæ°ããŸãã
- (void) setProgress:(float)progress { #ifndef TESTING updateProgressView.progress = progress; #endif }
setCompleteDate:
ç»é¢äžã®ããã¹ãããã¯ã¹ã«æŽæ°æ¥ã衚瀺ããŸãã
- (void) setCompleteDate:(NSString*)date { NSLog(@"setCompleteDate(): '%@'", date); #ifndef TESTING [lastUpdatedLabel setText:date]; #endif } - (bool) updateBatchStatus:(NSString*)batchNumber { NSURL *url = [[[NSURL alloc] initWithString:[NSString stringWithCString:pdf encoding:NSASCIIStringEncoding]] autorelease]; return [NSURLConnection downloadAtURL:url searching:batchNumber viewingOn:self]; }
次ã«ãããã€ãã®iOSåºæã®èª²é¡ã
viewDidLoad:
ã¡ãœãã
viewDidLoad:
ç»é¢ãããŒãããã䜿çšå¯èœãªç¶æ ã«ãªã£ããšãã«ã·ã¹ãã ã«ãã£ãŠåŒã³åºãããŸãã Xcode Interface Builderã§ã¯ãã©ãŒã ã®èšèšæã«ããããå€æŽã§ããªããããããã§ã¯æåã§å転ã¹ã©ã€ããŒãäœæãããã¿ã³ãšå ¥åãã£ãŒã«ãã®2ã€ã®èŠçŽ ã®é«ãã調æŽããŸãã
#ifndef TESTING - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. spinnerActivityIndicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge]; [spinnerActivityIndicatorView setColor:[UIColor blueColor]]; CGSize size = [[self view] frame].size; [spinnerActivityIndicatorView setCenter:CGPointMake(size.width / 2, size.height / 2 + 60)]; [self.view addSubview:spinnerActivityIndicatorView]; CGRect rect = [self.updateButton bounds]; rect.size.height += 10; [self.updateButton setBounds:rect]; rect = [self.batchNumberTextField bounds]; rect.size.height += 20; [self.batchNumberTextField setBounds:rect]; #ifdef DEBUG NSLog(@"DEBUG mode"); #endif }
viewDidUnload
, .
- (void)viewDidUnload { [super viewDidUnload]; // Release any retained subviews of the main view. }
shouldAutorotateToInterfaceOrientation:
. , .
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return (interfaceOrientation == UIInterfaceOrientationPortrait); } #endif
launchUpdate:
Update
. , .
- (IBAction)launchUpdate:(id)sender { [self setProgress:0.0]; #ifndef TESTING [updateButton setEnabled: NO]; [updateProgressView setHidden:NO]; NSString* previousStatus = [statusTextView text]; [statusTextView setText:@""]; NSString* batchNumber = [batchNumberTextField text]; [spinnerActivityIndicatorView startAnimating]; BOOL const ok = [self updateBatchStatus:batchNumber]; [spinnerActivityIndicatorView stopAnimating]; if (!ok) { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Internet connectivity problem" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil]; [alert show]; [alert release]; [statusTextView setText:previousStatus]; } [updateProgressView setHidden:YES]; [updateButton setEnabled: YES]; #endif }
saveProperties:
loadProperties:
. , .
- (void) saveProperties { NSDictionary *props = [[NSDictionary alloc] initWithObjectsAndKeys: #ifndef TESTING batchNumberTextField.text, @"batchNumberTextField", statusTextView.text, @"statusTextView", lastUpdatedLabel.text, @"lastUpdatedLabel", #endif nil]; for (NSString* key in props) { NSLog(@"%@ - %@", key, [props objectForKey:key]); } NSString* filename = pathInDocumentDirectory(PropertiesFilename); if ([props writeToFile:filename atomically:YES] == NO) NSLog(@"Unable to save properties into file [%@]", filename); [props release]; } - (void) loadProperties { NSDictionary *props = [[NSDictionary alloc] initWithContentsOfFile:pathInDocumentDirectory(PropertiesFilename)]; for (NSString* key in props) { NSLog(@"%@ - %@", key, [props objectForKey:key]); } #ifndef TESTING [batchNumberTextField setText:[props objectForKey:@"batchNumberTextField"]]; [statusTextView setText:[props objectForKey:@"statusTextView"]]; [lastUpdatedLabel setText:[props objectForKey:@"lastUpdatedLabel"]]; #endif [props release]; } - (IBAction)textFieldReturn:(id)sender { #ifndef TESTING [sender resignFirstResponder]; #endif } -(IBAction)backgroundTouched:(id)sender { #ifndef TESTING [batchNumberTextField resignFirstResponder]; #endif } @end
ããã ãã§ãïŒ . . ( Apple ).
GitHub â usvisa-app . .
:
!
, , . : 57x57 114x114 , 512x512 1024x1024 AppStore.
â The Great Seal of the United States .
![](http://demin.ws/images/blog/usvisa/app/USVisa-icon-114x114.png)
PS
, AppStore «», , , , HTML5, . , . HTML5, . -, , Apple , - â Objective-C, .
PPS
iOS , .