Saturday, June 16, 2018

Uploading files to iCloud

This is the steps:

1) Enable iCloud service in the project.



2)

In the Info.plist, add the following settings. Please change the first "key" value (follow the same value as it was created in the Apple developer web portal) and also the folder name "My test folder".

<key>NSUbiquitousContainers</key>
<dict>
<key>iCloud.my.myTest180614</key>
<dict>
<key>NSUbiquitousContainerIsDocumentScopePublic</key>
<true/>
<key>NSUbiquitousContainerName</key>
<string>My test folder</string>
<key>NSUbiquitousContainerSupportedFolderLevels</key>
<string>Any</string>
</dict>
</dict>

3) Download the open source library from the following URL. You need to add 4 files in iCloud folder (except the pch file).

https://github.com/iRareMedia/iCloudDocumentSync

4) Next thing is to follow the sample codes in the above open source home page.

Notes: whenever you made changes to the iCloud settings, make sure you increase the app build number. Otherwise, the changes might not reflect in iCloud service.

5) Upon app init, make sure that you run the following codes to init the iCloud service.

    [[iCloud sharedCloud] setupiCloudDocumentSyncWithUbiquityContainer:nil];
    [iCloud sharedCloud].verboseLogging = YES;
    
    BOOL cloudIsAvailable = [[iCloud sharedCloud] checkCloudAvailability];
    
    if (cloudIsAvailable) {
        [self showLog:@"cloud is available"];
    }
    else {
        [self showLog:@"cloud is NOT available"];
    }


6) To upload a file to iCloud:

    [[iCloud sharedCloud] uploadLocalDocumentToCloudWithName:@"my-test-file-local.txt" completion:^(NSError* err) {
        
        if (err == nil) {
            NSLog(@"error..");
        }
        
    }]; 

7) To check the file if it exist in the iCloud:

    NSURL* cloud_doc = [[f URLForUbiquityContainerIdentifier:nil] URLByAppendingPathComponent:@"Documents/my-test-file-local.txt"];

    BOOL b = [f isUbiquitousItemAtURL:cloud_doc];
    if (b) {
        [self showLog:@"=> my-test-file-local.txt- the file exist in the cloud"];
    }
    else {
        [self showLog:@"=> my-test-file-local.txt- the file does NOT exist in the cloud"];
    }

Notes: by default, the file stores in "Documents" folder!! Watch out for this behaviour.

8) To check if all files have been synced to local, make sure you handle the following delegate:

     iCloudDelegate

Handle this callback before start doing anything to the files (from the cloud):

     iCloudFileUpdateDidEnd


Saturday, March 17, 2018

HTML front end with back end with Objective-C


We are in the process of revamping all our iOS apps recently using: HTML+CSS+JavaScript to handle the front end and the back end is handled by Objective-C. The development became very interestingly for both UI designer and the programmer.

Before we took this approach, we were adding new functionality using Swift. After a few version upgrade on Swift/XCode, we found out that compilation error and warning will make us go mad. We don't think we can handle this and we don't like this to happen again and again. So, we decided to migrate all Swift codes that we have written back to Objective-C.

The next thing we are facing is the UI (user interface) and we are tiring in looking for solution on showing the popup and adjust the coordinate the popup appear in the correct position, etc. The storyboard file is particularly another bottleneck and wasted a lot of hours in "waiting for others to complete before they can start work".

With the new approach, the UI designer will work on the HTML+CSS. Once the layout has been confirmed by the team, the UI designer will continue to work on beautifying it and the Javascript & Objective-C programmer will start coding.

This approach reduce the development time as well as adding new features tremendously.

Time wasted in Swift version upgrade:
- New attribute
- Casting with ?, ?? or !....
- Casting between the data type: NSString, NSString?, NSString!....

Time wasted in implementing new idea using Storyboard:
- How to show a popup window.
- How to position the popup window correctly.
- How to partition a "view" into smaller view(s)/component(s) and then aggregate it to what we want.
- Screen flow/navigation.... try this API.. not this one, try another one... ah... obsolete API, look for new API... endless.. :(

With new approach:
 - HTML+CSS - that's my what my UI designer good at.
- Javscript - that's what we are doing for web application since 2008.
- Objective-C - this is not the area that we are good at and we can't afford to spend too much time in researching for the correct code. But, we need is to store the data and load the data. Since we are good in SQL statement and we decided to store the data in SQLite.

Future upgrade for the app...?
- Adding new HTML elements.. small problem.
- Reposition the popup... small problem.
- Revamp the color theme... small problem.. just replace the color and background color in CSS.
- Adding new screen... just add a new HTML file.
- Adding new function... just add a new Javascript to handle and some backend code in Objective C.

Our objective is to minimize the upgrade time and cost. Another thing is that we don't need to use any big framework that is already doing this (you can Google search for it) because those framework upgrade might break and we need to spends many hours in chasing their tail.

We love programming but we don't like to chasing the tail become someone decided to make some API obsolete and replace it by some new API.

:)

Sunday, March 11, 2018

ERROR ITMS-90717 while uploading app

While I was uploading my app to Apple App Store using Xcode 9, the following error appear:

ERROR ITMS-90717: "Invalid App Store Icon. The App Store Icon in the asset catalog in "xx.app" can't be transparent nor contain an alpha channel".

To resolve this error, you need to

1. Remove the alpha (i.e., the transparency attribute) in the image file. This can be done easily.

1.1. Double click on your app icon and the image will appear Preview app.
1.2. Choose File \ Export.
1.3. Untick "Alpha" (as shown below).



1.4. Click Save.

So, you have done the first step.

2. Regenerate all the icon file in various dimension.

3. Replace all the icon files in your project.

4. Last step, update the Images.xcassts file by dragging all appropriate
 icon file.

To check if all icon files have been updated, open the following directory in Finder. Ensure that all icon files' date/time matches the date/time that was done in step 2.

    [your app] \ Images.xcassets \  AppIcon.appiconset

5. Build and upload your app.

ERROR ITMS-90029 while uploading using Xcode 9

While I was uploading my app and encountered this error:

ERROR ITMS-90029: "Storyboard file 'Main~iphone.storyboardc' was not found. Please ensure that the specified file is included in the bundle with any required device modifiers appended to the filename".

What happened..? My app supports iPad only and how come it prompted an error on missing iPhone storyboard..?

I guess, there is some problem caused by upgrading the project. Why "Main~iphone.storyboardc"? I opened the .plist file and found this:

    Main storyboard file base name: "Main" <=== this value shown in the error message.
    Main storyboard file base name: "StoryboardV2" <== this is correct value.

Anyway, the problem can be rectify by

1. Opening the Target > your app. Look for "Devices" in the Deployment Info section. For my case, my value is "iPad".

2. Changing the "iPad" value to "Universal".

3. Set the Main Interface to any value.

4. Click on any source code file to force the new values to be saved to .plist.

5. Open the Devices in the Deployment Info section again.

6. Change the values back to "iPad" and the correct Main Interface value.

7. Archive and upload my app to App Store and the error has gone.







Sunday, July 30, 2017

Local or remote user notification

To use the local or remote user notification, first you need to add the reference to the UserNotifications framework.

   @import UserNotifications;

The most important is this code before using the user notification framework - you must get the user consent

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
    center.delegate = self;

    [center requestAuthorizationWithOptions:UNAuthorizationOptionAlert | UNAuthorizationOptionBadge | UNAuthorizationOptionSound
                         completionHandler:^(BOOL granted, NSError* err) {
                             if (granted) {
                                 NSLog(@"granted notif");
                             }
                             
                         }];
    
    return YES;

}

For the "center.delegate = self" to work, you need to implement this protocol in the AppDelegate class:

    UNUserNotificationCenterDelegate

To schedule a local user notification, please use the following sample code which I found in the Internet:

- (IBAction)schedule_click:(id)sender {
    
    UNMutableNotificationContent *localNotification = [UNMutableNotificationContent new];
    localNotification.userInfo = [NSDictionary dictionaryWithObjectsAndKeys:@"User1",@"Username", nil];
    localNotification.badge = [NSNumber numberWithInt:1];
    
    localNotification.sound = [UNNotificationSound defaultSound];
    
    // load the pre-defined mp3
    //localNotification.sound = [UNNotificationSound soundNamed:@"my.mp3"];
    
    localNotification.categoryIdentifier = @"myReminder";
    localNotification.body = @"Reminder to test";
    
    NSDate *date = [[NSDate date] dateByAddingTimeInterval:60];
    
    NSCalendar *gregorian = [[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    
    /*NSCalendarUnitDay – Will set the repeatInterval to daily */
    NSDateComponents *dateComponents = [gregorian components:NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitTimeZone fromDate:date];
    
    UNCalendarNotificationTrigger *datetrigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:dateComponents repeats:YES];
    UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"Some Unique Value"
                                                                          content:localNotification
                                                                          trigger:datetrigger];
    
    UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
    [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
        NSLog(@"added notif. Error= %@",error);
    }];

}


Say something... text to speech in iOS

In iOS, there is a text to speech framework which is able to say the text that you have typed.

The reference that you need in order to use the text to speech classes:

    @import AVFoundation;

Sample code:

    AVSpeechSynthesizer* speaker = [[AVSpeechSynthesizer alloc] init];
    AVSpeechUtterance* u = [[AVSpeechUtterance alloc] initWithString:@"Good morning. Boss."];

    u.voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"en-US"];
    

    [speaker speakUtterance:u];


To get the list of the supported languages.

    NSArray<AVSpeechSynthesisVoice *> * v  = [AVSpeechSynthesisVoice speechVoices];
    
    AVSpeechSynthesisVoice* v2;
    for (int i = 0; i < v.count; i++) {
        v2 = [v objectAtIndex:i];
        NSLog(@"voice lang id=%@, code=%@", v2.identifier, v2.language);
    }

Please take note that there is duplicate entry for the same language code. This is because it has male and female voices and the identifier to the voice is different (i.e., unique). You need to call voiceWithIentifier method instead of voiceWithLanguage.

Tuesday, April 29, 2014

Creating multiple apps from one project

If you are planning to publishing multiple apps from one project (for example, light version and premium version), this will be the article that can help you out.

1. Click Manage Scheme.

2. Untick the "autocreate schemes" option. By unchecking this option, you can assign a proper scheme name.

3. Click on the project Targets. Two finger click on the "taget item" that you want to duplicate.

4. A new plist file will be added to the project. Rename the plist file to whatever you want. In our case, we use "premium version". Do the same for the "Bundle display name" and also "Bundle identifier".

  Note: you need two app ID. One for the light version and another for premium version.

5. Goto the project targets again. Rename the target to "premium". In Build Settings, search for "Product name" and update it as well.

6. Search "plist" in Build Settings and change it to the appropriate plist file name.

7. Click on the "scheme" and choose New Scheme.

8. Choose the "premium" from the target and then key in "premium" as the scheme name.

9. The final step will be setup the preprocessor macros. You need to edit both Debug and the Release.

  • For premium version, add "IS_PREMIUM_VERSION".
  • For light version, add "IS_LIGHT_VERSION".

10. In the coding, you have to do this:

 
     #ifdef IS_PREMIUM_VERSION
         lbl.text = @"this is premium Verison";
     #else
         lbl.text = @"this is Light version";
     #endif