Archive for the ‘iPhone apps’ Category

XCode 14.3 にアップデート後、Archive (リリースビルド)でrsync のエラー

2023年4月5日

rsync: link_stat "…" failed: No such file or directory

https://github.com/CocoaPods/CocoaPods/issues/11808#issuecomment-1480802886

で共有された回避方法に従って、生成されているすべての …-frameworks.sh において -f フラグを readlink に追加してみたところビルドできた。

Camera (iOS)

2018年8月16日

https://developer.apple.com/library/ios/documentation/AudioVideo/Conceptual/CameraAndPhotoLib_TopicsForIOS/Introduction/Introduction.html

iOS – UIToolBar のボタンを右寄せにする

2018年8月16日

ボタンの左にUIBarButtonItemFlexibleSpace(左右外向き矢印)を追加する

MKMapView (iOS) のメモ

2015年5月31日

以下のページに詳しく説明してあるので、その通りにやれば問題ないはず。

About Location Services and Maps
https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/LocationAwarenessPG/Introduction/Introduction.html

Getting the User’s Location
https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/LocationAwarenessPG/CoreLocation/CoreLocation.html#//apple_ref/doc/uid/TP40009497-CH2-SW1

MapKit.Framework はMapKitViewを追加したら勝手に追加されてた気がしますが、またはProject>Capabilitiesからも追加できます。
CoreLocation.Framework はProject>Build Phases から追加

 

ただし、現在位置に移動しようとしたら以下のエラーがでるかもしれません。

Trying to start MapKit location updates without prompting for location authorization. Must call -[CLLocationManager requestWhenInUseAuthorization] or -[CLLocationManager requestAlwaysAuthorization] first.

そのときは Info.plist に以下のエントリを追加します。

   <key>NSLocationAlwaysUsageDescription</key>

    <string>Please allow this app to access your location.</string>

    <key>NSLocationWhenInUseUsageDescription</key>

    <string>Please allow this app to access your location.</string>

カメラロールに画像を保存(iOS)

2014年10月10日

ALAssetsLibrary の writeImageToSavedPhotosAlbum メソッドを使います。

ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
UIImage* img = self.imageView.image;
[library writeImageToSavedPhotosAlbum:img.CGImage orientation:ALAssetOrientationUp completionBlock:^(NSURL *assetURL, NSError *error) {
    if (error) {
        //エラー処理
    }
}];

 

バックグランドでもアプリを動かし続けるには?(iOS)

2012年7月16日

UIApplication.applicationDidEnterBackground  で beginBackgroundTaskWithExpirationHandler を呼び出すようにとの資料があったが

https://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIApplicationDelegate_Protocol/Reference/Reference.html#//apple_ref/occ/intfm/UIApplicationDelegate/applicationDidEnterBackground:

beginBackgroundTaskWithExpirationHandler は”You should not use this method simply to keep your application running after it moves to the background.”と書いてあるので、これを使ってはいけない気がするが。。。では、どうしたらいいのだろう’?

しかし、とりあえず beginBackgroundTaskWithExpirationHandler を呼び出す方法を使ってみたところ、エミュレータではちゃんとバックグランドで動いたし、別に問題ないように見える。

UIPickerViewを使う(iOS)

2012年7月1日

1.xlbファイルにPickerViewを追加

2.UIPickerViewDelegateとUIPickerViewDataSourceを実装、xlbファイルのFileOwnerであるViewControllerに実装しても可。

3.xlb上で、PickerViewのDataSourceとDelegateに関連づける

datasourceとdelegateの設定

datasourceとdelegateの設定

4.ViewControllerにPickerViewのOutletを追加。初期値の設定や選択された値の取得はこのOutletで行う。

Custom Viewを作成(iOS)

2012年6月24日

SingleView でプロジェクトを作成 xlbを開き、既存のViewの上にラベルやボタンなどのコントロールと、CustomView用のUIViewを乗っける。

新規にUIViewを継承するクラスを作成し、さっき追加したCustomView用のObjectに関連づける。

作成したクラスの drawRect に描画のコードを追加。UIBezierPathを使って円弧を塗りつぶす。

ここで、1つひっかかってしまったのは、Pathに弧の部分だけを描いても思ったように塗りつぶせないこと。AndroidのCanvas.drawArcメソッドのuseCenterパラメータがfalseと同じ状態になり、弧の始点と終点を直線で結んだ内部だけが塗りつぶされる。円の中心も含めるには、Pathに明示的に円の中心までの直線を追加する必要がある。なお、塗りつぶし(fill)の場合は、Pathの始点と終点が自動的に直線で結ばれる(closeされる)が、線を描く(stroke)場合は、明示的に始点と終点を結ぶ線を追加するか、あるいはclosePathメソッドを呼び出さないと、開放されたPathのままで描かれる。

円弧を塗りつぶすコード例

    //draw filled arc for tic
    UIBezierPath *pathTic1 = [UIBezierPath bezierPathWithArcCenter:c radius:r startAngle:st1 endAngle:en1 clockwise:true];
    //Close the path.
    [pathTic1 addLineToPoint:c]; 

    // Set the render colors
    [[UIColor redColor] setFill];

    // Fill the path
    [pathTic1 fill];

iPhone/iPadでPicasaを使う(画像のプレビューまで)

2012年5月13日

Picasaを使う(iPhone)でPicasaWebAlbumのAPIでアルバム一覧を取得するところまでやったので、今回は画像のプレビューまで実装してみた。(ソース)

iPhone版の画面

Login画面(iPhone)

Login画面(iPhone)

Album一覧(iPhone)

Album一覧(iPhone)

写真一覧(iPhone)

写真一覧(iPhone)

(more…)

NSManagedObjectModelでTableViewのデータを管理する

2011年12月23日

xcdatamodel でデータモデルを定義

xcdatamodelにデータモデルを定義

TableViewControllerもこれにあわせて変更

まずは、Viewに関連付けるEntityを指定

- (NSFetchedResultsController *)fetchedResultsController
{
    if (__fetchedResultsController != nil)
    {
        return __fetchedResultsController;
    }

    /*
     Set up the fetched results controller.
    */
    // Create the fetch request for the entity.
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    // Edit the entity name as appropriate.(*defined in xcdatamodel)
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Album" inManagedObjectContext:self.managedObjectContext];
    [fetchRequest setEntity:entity];

    // Set the batch size to a suitable number.
    [fetchRequest setFetchBatchSize:20];

    // Edit the sort key as appropriate.(*defined in xcdatamodel)
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:NO];
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];

    [fetchRequest setSortDescriptors:sortDescriptors];

    // Edit the section name key path and cache name if appropriate.
    // nil for section name key path means "no sections".
    NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:@"Root"];
    aFetchedResultsController.delegate = self;
    self.fetchedResultsController = aFetchedResultsController;

    [aFetchedResultsController release];
    [fetchRequest release];
    [sortDescriptor release];
    [sortDescriptors release];

    NSError *error = nil;
    if (![self.fetchedResultsController performFetch:&error])
        {
        /*
         Replace this implementation with code to handle the error appropriately.

         abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button.
         */
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }

    return __fetchedResultsController;
}

次にセルデータの表示のためのコードを編集

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    // Configure the cell.
    [self configureCell:cell atIndexPath:indexPath];
    return cell;
}
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
    NSManagedObject *managedObject = [self.fetchedResultsController objectAtIndexPath:indexPath];
    cell.textLabel.text = [[managedObject valueForKey:@"name"] description];
}

TableView の ManagedObjectContextにデータを設定する

このコードはTableViewの外でもよい。ここで、mFetchedResultsController は、TableViewの fetchedResultsController で取得した NSFetchedResultsController 。

既存データをクリア

Entity毎にクリアできるメソッドが見つからなかったので、1つずつ削除してみた。

    //clear existing data
    NSArray *fetchedObjects = [mFetchedResultsController fetchedObjects];
    NSEnumerator *enumerator = [fetchedObjects objectEnumerator];
    NSManagedObject *object;
    while ((object = [enumerator nextObject])) {
        [[mFetchedResultsController managedObjectContext] deleteObject:object];        
    }    

データをInsertする。

            //
            // insert into managedObjectContext
            //
            // Create a new instance of the entity managed by the fetched results controller.
            NSManagedObjectContext *context = [mFetchedResultsController managedObjectContext];
            NSEntityDescription *entity = [[mFetchedResultsController fetchRequest] entity];
            NSManagedObject *newManagedObject = [NSEntityDescription insertNewObjectForEntityForName:[entity name] inManagedObjectContext:context];

            [newManagedObject setValue:title forKey:@"name"];

            // Save the context.
            NSError *error = nil;
            if (![context save:&error])
            {
                /*
                 Replace this implementation with code to handle the error appropriately.

                 abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button.
                 */
                NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
                abort();
            }
            //

persistentStoreCoordinatorでエラーになるとき

データモデルを変更した後、AppDelegate の persistentStoreCoordinatorでエラーになるときは、自動生成されるコメントに書いてあるように、一度既存のデータファイルを削除してみる。

//deleting the existing store
 [[NSFileManager defaultManager] removeItemAtURL:storeURL error:nil];