您的位置:首页 > 移动开发 > IOS开发

ios 技巧总结(不断更新)

2014-12-12 14:07 183 查看
1. 数据格式化输出 1000000 -> 1,000,000

数据格式化输出 1000000 -> 1,000,000

NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];

[numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];

NSString *numberString = [numberFormatter stringFromNumber:@(1000000)];

_salesCountLabel.text = [NSString stringWithFormat:NSLocalizedString(@"Yesterday you sold %@ apps", nil), numberString];

2.

方式一:跳转打开

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.google.com"]];

方式二:应用内打开

    通过Modal view方式弹出App Store商品详情页面。我按照文档说明,写了个例子。部分代码如下:

- (void)openAppWithIdentifier:(NSString *)appId {
SKStoreProductViewController *storeProductVC = [[SKStoreProductViewController alloc] init];
storeProductVC.delegate = self;

NSDictionary *dict = [NSDictionary dictionaryWithObject:appId forKey:SKStoreProductParameterITunesItemIdentifier];
[storeProductVC loadProductWithParameters:dict completionBlock:^(BOOL result, NSError *error) {
if (result) {
[self presentViewController:storeProductVC animated:YES completion:nil];
}
}];
}

另外,需要实现SKStoreProductViewControllerDelegate如下代理方法:

#pragma mark - SKStoreProductViewControllerDelegate
- (void)productViewControllerDidFinish:(SKStoreProductViewController *)viewController {
[viewController dismissViewControllerAnimated:YES completion:^{
[viewController release];
}];
}

可以这样调用:

[self openAppWithIdentifier:@"383037733"];

这段代码即实现了上面图示的效果。

注:项目需要添加StoreKit框架,仅在iOS 6.0以上的设备中支持上述实现。

Framework
/System/Library/Frameworks/StoreKit.framework
Availability
Available in iOS 6.0 and later.


如果需要兼容6.0以下的设备,可以使用下面的代码(这种方式会跳出当前应用):

- (void)outerOpenAppWithIdentifier:(NSString *)appId {
NSString *urlStr = [NSString stringWithFormat:@"itms-apps://itunes.apple.com/us/app/id%@?mt=8", appId];
NSURL *url = [NSURL URLWithString:urlStr];
[[UIApplication sharedApplication] openURL:url];
}
-——————————————————————————————

-——————————————————————————————

3.

iOS 应用中打开其他应用 (转)

我们来讨论一下,在iOS开发中,如何实现从app1打开app2。

基本的思路就是,可以为app2定义一个URL,在app1中通过打开这个URL来打开app2,在此过程中,可以传送一些参数。下面来讨论一下具体的实现过程。



2. 在app1的代码中打开刚才定义的URL,代码如下:

NSURL *url = [NSURL URLWithString:@"myapp://test?para1=1¶2=2"];
[[UIApplication sharedApplication] openURL:url];


当然,这个URL的形式可以是其他形式的,只要以"myapp://"开始即可。

这样,就可以在app1中打开app2.

打开之后,会调用app2的AppDelegate的

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation


由于URL是自己定义的,所以会存在URL重复的情况。经过测试发现,如果试图打开这个URL,那么会打开先安装的那个应用。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: