標籤:des style blog http io ar color os 使用
參考:http://www.2cto.com/kf/201401/274753.html
http://blog.sina.com.cn/s/blog_a170e5c80101gsdj.html
http://blog.csdn.net/perfect_promise/article/details/7793735
在IOS中,實現一個應用啟動另外一個應用,使用UIApplication的openURL:方法就可實現,這裡以test跳到test02為例。(需要先建立這兩個工程)
註冊自訂URL協議(在test中)
首先被啟動的應用需要向iPhone註冊一個自訂URL協議。這是在info.plist檔案進行的。
1. 右鍵,選擇“Add Row”
2. Key值選擇“URL types”
3. 開啟“Item 0″,然後為該key增加一個URL identifier。可以是任何值,但建議用“反網域名稱”(例如 “com.fcplayer.test”)。
4. 在“Item 0”下再加一行。
5. 選擇“URL Schemes” 作為Key。
6. 輸入你的URL協議名 (例如“test://” 應寫做“test”)。如果有必要,你可以在這裡加入多個協議。
操作如下:
7. 兩個項目的URL identifier 不能一樣,而且在URL Schemes的Item 0不能相同,這一點需要注意。
訪問自訂URL(在test02中)
在主應用程式中通過訪問自訂URL啟動另外一個應用:(test已經安裝,這段代碼要寫在另一個應用裡面,比如test02)
- (void)viewDidLoad {
[super viewDidLoad];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(100, 200, 100, 50);
btn.backgroundColor = [UIColor redColor];
[btn addTarget:self action:@selector(onBtnClick) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
}
- (void)onBtnClick{
NSURL * urlStr = [NSURL URLWithString:@"test://x=100"];//後面為參數
if ([[UIApplication sharedApplication] canOpenURL:urlStr]) {
NSLog(@"can go to test");
[[UIApplication sharedApplication] openURL:urlStr];
}else{
NSLog(@"can not go to test!!!!!");
}
}
自訂處理URL(在test中)
有些時候我們除了啟動還需向另外一個應用發送參數,這是也可以通過自訂的URL來實現,如:
test://config=1&abar=2
通常,我們會從參數中解析出URL以便在視圖中顯示或者儲存到UserPreference。下面的例子把URL儲存為User Preference的url變數中或者列印出來:
這時我們在被啟動應用中就必須進行自訂處理,在delegate中實現該訊息(Cocos2d加在AppDelegate中),例如:
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{
if(!url) {
return NO;
}
NSString *URLString = [url absoluteString];
NSLog(@"%@",URLString);
return YES;
}
調用地圖電話等方法
openURL的使用方法:
view plaincopy toclipboardprint?
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:appString]];
其中系統的appString有:
1.Map http://maps.google.com/maps?q=Shanghai
2.Email mailto://[email protected]
3.Tel tel://10086
4.Msg sms://10086
openURL能協助你運行Maps,SMS,Browser,Phone甚至其他的應用程式。這是Iphone開發中我經常需要用到的一段代碼,它僅僅只有一行而已。
- (IBAction)openMaps {
//開啟地圖
NSString*addressText = @"beijing";
//@"1Infinite Loop, Cupertino, CA 95014";
addressText =[addressTextstringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
NSString*urlText = [NSStringstringWithFormat:@"http://maps.google.com/maps?q=%@",addressText];
NSLog(@"urlText=============== %@", urlText);
[[UIApplicationsharedApplication] openURL:[NSURL URLWithString:urlText]];
}
- (IBAction)openEmail {
//開啟mail // Fire off an email to apple support
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:@"mailto://[email protected]"]];
}
- (IBAction)openPhone {
//撥打到電話
// CallGoogle 411
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel://8004664411"]];
}
- (IBAction)openSms {
//開啟簡訊
// Text toGoogle SMS
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"sms://466453"]];
}
-(IBAction)openBrowser {
//開啟瀏覽器
// Lanuch any iPhone developers fav site
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://itunesconnect.apple.com"]];
}
iphone程式內調用Google地圖
使用CLLocationManager類,MKMapView。並且實現<MKMapViewDelegate,CLLocationManagerDelegate>
//初始化CLLocationManager,CLLocationManager獲得當前地理座標
locmanager=[[CLLocationManager alloc]init];
[locmanager setDelegate:self];
//設定精確度
[locmanager setDesiredAccuracy:kCLLocationAccuracyBest];
[locmanager startUpdatingLocation];
執行完以後,會自動調用代理方法:
iOS OpenURL用法簡介