2014年12月30日 星期二

CLLocationManager在iOS8之後,如何新增權限的機制

在iOS8之後,要使用LocationManager之前,要先跟使用者要權限,可以要求的權限分為兩種:

  1. 只允許App在前景使用GPS。
  2. 允許App在進入後景之後仍可使用GPS。
因為我要開發的是iBeacon-based App,所以想當然爾要詢問的是第二種權限。

不過要求方式差不多,所以下面同時介紹兩種方式

一、在你的plist中加入這兩個Key(也可只加入你要用的),Value的部份選string並輸入你想要在要求權限的AlertView中出現的內容。
  • NSLocationAlwaysUsageDescription
  • NSLocationWhenInUseUsageDescription

、在你要使用GPS之前,用
//要求always權限

if ([self respondsToSelector:@selector(requestAlwaysAuthorization)]) {
                [self requestAlwaysAuthorization];
            }
//要求whenInUse權限
            if ([self respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
                [self requestWhenInUseAuthorization];

            }

2014年12月27日 星期六

區域監控的觸發時機

Monitoring of a geographical region begins immediately after registration for authorized apps. However, don’t expect to receive an event right away, because only boundary crossings generate an event. In particular, if the user’s location is already inside the region at registration time, the location manager doesn’t automatically generate an event. Instead, your app must wait for the user to cross the region boundary before an event is generated and sent to the delegate. To check whether the user is already inside the boundary of a region, use the requestStateForRegion: method of the CLLocationManager class.

要跨過區域才會觸發事件。
如果一開始就在區域內部,則不會觸發進入事件,
解決方法是要用requestStateForRegion去主動查看狀態。

P.S.我在寫iBeacon的時,遇到一模一樣的情況。

2014年12月26日 星期五

社群分享功能,在大陸已被做成SDK

//阿里巴巴投資
http://dev.umeng.com/social/ios/sdk-download

//也獲投資
http://sharesdk.mob.com

2014年12月18日 星期四

NSData UIImage 互轉

http://stackoverflow.com/questions/14133366/convert-uiimage-to-nsdata-and-save-with-core-data

If PNG image
UIImage *image = [UIImage imageNamed:@"imageName.png"];
NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(image)];
If JPG image
UIImage *image = [UIImage imageNamed:@"imageName.jpg"];
NSData *imageData = UIImageJPEGRepresentation(image, 1.0);
You can store it in CoreData like so (this is one possible useful solution):
[newManagedObject setValue:imageData forKey:@"image"];
You can load the data from CoreData like this:
NSManagedObject *selectedObject = [[self yourFetchCOntroller] objectAtIndexPath:indexPath];
UIImage *image = [UIImage imageWithData:[selectedObject valueForKey:@"image"]];

// Set the image to your image view  
yourimageView.image = image;

2014年12月16日 星期二

iAP線上改價格


結論:可以直接在itunesconnect改價格,不需要重新審核。


Changing Product Pricing

You can change the pricing for an In-App Purchase product at any time.


To change product pricing
  1. Go to the In-App Purchases page for the app, as described in To view In-App Purchase product configuration information.
  2. Click in the row of the In-App Purchase product you want to edit.
  3. In the In-App Purchase Summary section, click Edit.
  4. Make changes to the summary information.
    • If you want to replace the existing Price Tier, select the new Price Tier, then set the Price Effective Date to Now and the Price End Date to None. Click Add to Schedule.
    • If you want to have the product price change at some date in the future, select the future Price Tier, then set the Price Effective Date and Price End Date to define the interval for the new price. Click Add to Schedule.
  5. Click Save.
    The new values appear in the In-App Purchase Summary and are effective immediately. Apple doesn’t review these changes.

使用目前locale的資訊

//目前系統語言
NSString * language = [[NSLocale preferredLanguages] objectAtIndex:0];

//當地的貨幣格式
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
    [numberFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
    [numberFormatter setLocale:[NSLocale systemLocale]];
    NSString *formattedString = [numberFormatter stringFromNumber:@100];