2013年4月25日木曜日

PopUp Viewのサイズ指定


PopUp Viewの大きさは、UIViewのほうに記述する。
PopUp Viewに記述しても効かない。

    if (setPop != nil)
    {
        [setPop dismissPopoverAnimated:YES];
    }
    UIViewController *vc = [[UIViewController alloc] init] ;
    vc.view.frame = CGRectMake(0, 0, 300, 500);
    vc.contentSizeForViewInPopover = vc.view.frame.size;
    
    setPop = [[UIPopoverController alloc] initWithContentViewController:vc];
    [setPop presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES];

Print Menu表示(HTMLテキスト)


HTMLテキストを印刷する

    UIPrintInteractionController *pic = [UIPrintInteractionController sharedPrintController];
    pic.delegate = self;
    UIPrintInfo *printInfo = [UIPrintInfo printInfo];
    printInfo.outputType = UIPrintInfoOutputGeneral;
    printInfo.jobName = @"document"; //self.documentName;
    pic.printInfo = printInfo;
    UIMarkupTextPrintFormatter *htmlFormatter = [[UIMarkupTextPrintFormatter alloc]initWithMarkupText:@"<html><body>samaple HTML</body></html>"]; //self.htmlString];
    htmlFormatter.startPage = 0;
    htmlFormatter.contentInsets = UIEdgeInsetsMake(72.0, 72.0, 72.0, 72.0); // 1インチの余白
    pic.printFormatter = htmlFormatter;
    pic.showsPageRange = YES;
    
    void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) =^(UIPrintInteractionController *pic, BOOL completed, NSError *error) {
        if (!completed && error) {
            NSLog(@"Printing could not complete because of error: %@", error);
        }
    };
    
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
        [pic presentFromBarButtonItem:sender animated:YES completionHandler:completionHandler];
        
    } else {
        [pic presentAnimated:YES completionHandler:completionHandler];
    }

 アプリケーションメニューの表示

外部アプリケーションのメニュ=を表示し、ファイルをアプリに送る


UIDocumentInteractionControllerは、iOS4から使えるが、facebook,twitter,Printなどは出てこない。
iOS6なら、UIActivityViewControllerを使う方よい。

ここでは、UIDocumentInteractionControllerのサンプルを載せる。




UIDocumentInteractionController *docInterCon;



    //データへのURL
    NSBundle *bundle = [NSBundle mainBundle];
    NSString *dataFilePath = [bundle pathForResource:@"file" ofType:@".text"];
    NSURL *url = [NSURL fileURLWithPath:dataFilePath];
    
    //
    self.docInterCon = [UIDocumentInteractionController interactionControllerWithURL:url];
    self.docInterCon.delegate = self;
        
    //アプリケーションのメニューを表示
    BOOL isValid;
    isValid = [self.docInterCon presentOptionsMenuFromBarButtonItem:_barbtn animated:YES];
    if (!isValid){
        NSLog(@"アプリケーションがありません");
    }

2013年4月18日木曜日

ローカルにファイル保存


    //Get Directory path
    NSString *bundleID = [[NSBundle mainBundle] bundleIdentifier];
    NSFileManager *fm = [NSFileManager defaultManager];
    NSURL *dirPath = nil;
    
    NSArray *appSupportDir = [fm URLsForDirectory:NSApplicationSupportDirectory inDomains:NSUserDomainMask];
    
    if ([appSupportDir count ] >0)
    {
        dirPath= [[appSupportDir objectAtIndex:0]URLByAppendingPathComponent:bundleID];
        NSError *theErr = nil;
        if (![fm createDirectoryAtURL:dirPath withIntermediateDirectories:YES attributes:nil error:&theErr])
        {
            //error process
        }
    }
    
    //Write
    dirPath = [dirPath URLByAppendingPathComponent:@"aaa.txt"];
    
    BOOL result = [str writeToURL:dirPath atomically:YES encoding:NSUTF8StringEncoding error:&err];
    
    if (result) NSLog(@"Success write");
    
    //Read
    NSString *str1 = [NSString stringWithContentsOfURL:dirPath encoding:NSUTF8StringEncoding error:&err];
    
    NSLog(@"Read: %@",str1);

画像ファイルとテキストファイルをダウンロードして保存

ネットワーク経由で画像ファイルとテキストファイルをダウンロードして保存する


NSString *urlstring;
NSURL *url;
NSError *err = nil;
NSURL *path;

//serverIp1 サーバーのURL (http://xxxx.jp/xxxx/)
//dirPath  保存先のURL
//imageFile 画像ファイル名
//textFile テキストファイル名

//Download ImageFile
urlstring = [serverIp1 stringByAppendingString:imageFile];

url = [NSURL URLWithString:urlstring];

NSData *data = [NSData dataWithContentsOfURL:url options:nil error:&err];

//Write
path = [dirPath URLByAppendingPathComponent:ImageFile];

BOOL result = [data writeToURL:path options:NSDataWritingAtomic error:&err];
if (result == NO) break;

//Download Text file
urlstring = [serverIp1 stringByAppendingString:textFile];

url = [NSURL URLWithString:urlstring];

NSString *str = [NSString stringWithContentsOfURL:url encoding:NSShiftJISStringEncoding error:&err];

//Write
path = [dirPath URLByAppendingPathComponent:textFile];

result = [str writeToURL:path atomically:YES encoding:NSUTF8StringEncoding error:&err];
if (result == NO) break;

HTTP同期通信

通信時間が許されれば、同期通信のほうがプログラムは簡単。
レスポンスを処理したいときにも同期の方が便利。

textをPOSTし 、レスポンスをcontentsに。

NSURL *url = [NSURL URLWithString:urlstr];

NSData *myRequestData = [text dataUsingEncoding:NSUTF8StringEncoding];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: url];
[request setHTTPMethod: @"POST"];
[request setValue:@"text/html; charset=utf-8"  forHTTPHeaderField:@"content-type"];
[request setHTTPBody: myRequestData];

result = NULL;
contents = NULL;

//同期通信
NSURLResponse *resp;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&resp error:nil];
//convert to UTF8 text
contents = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

ApplicationSupportDirectoryのパスを得る

ApplicationSupportDirectoryのパスを得る
アプリ内でファイルを保存する場所として使う。


//Get Directory path
NSString *bundleID = [[NSBundle mainBundle] bundleIdentifier];
NSFileManager *fm = [NSFileManager defaultManager];
dirPath = nil;

NSArray *appSupportDir = [fm URLsForDirectory:NSApplicationSupportDirectory inDomains:NSUserDomainMask];

if ([appSupportDir count ] >0)
{
    dirPath= [[appSupportDir objectAtIndex:0]URLByAppendingPathComponent:bundleID];
    NSError *theErr = nil;
    if (![fm createDirectoryAtURL:dirPath withIntermediateDirectories:YES attributes:nil error:&theErr])
    {
        dirPath = nil;
    }
}

プログラムからViewの移動

プログラム中からViewを移動(遷移)させたいとき

stroyboardを使ったviewへ移動するとき


#import "View1.h"

    View1 *vc1;
    vc1 = [self.storyboard instantiateViewControllerWithIdentifier:@"view1"];
    [self presentViewController:vc1 animated:YES completion:nil];


Identifier (@"view2")は、storyboardで、storyboard IDとして入力しておくこと。


xibファイルがあるとき

#import "View2.h"


    View2 *vc1 = [[View2 alloc]initWithNibName:@"View2" bundle:[NSBundle mainBundle]]  ;
    [self presentViewController:vc1 animated:YES completion:nil];


NIbNameは、xibファイル名


戻る場合


    [self dismissViewControllerAnimated:YES completion:nil];



Navigation Controllerを使っている場合

[[self navigationController] pushViewController:vc1 animated:YES];


1つ上の階層へ戻る
[self.navigationController popViewControllerAnimated:YES];